Parser of berki style problems and generator of latex file
Samo Penic
2018-10-24 7b5760c1458c0fcbbda89ed4e417daa199e79cc0
Added Variable class, that doesn't pass the test yet.
2 files modified
98 ■■■■■ changed files
tools/Formatter.py 49 ●●●●● patch | view | raw | blame | history
tools/tests/formatter_test.py 49 ●●●●● patch | view | raw | blame | history
tools/Formatter.py
@@ -1,6 +1,45 @@
from . import Exceptions
from math import floor, log10
STRING = 1
FLOAT = 2
class Variable:
    def __init__(self, value=None, formatting="sci 3"):
        try:
            self.value = float(value)
            self.value = FLOAT
        except (ValueError, TypeError):
            self.type = STRING
        self.formatted_value = None
        self.type = STRING
        if formatting is not None:
            self.format_as(formatting)
        self.formatting = formatting
        # self.formatter=FormatterFactory(formatting)
    def format_as(self, formatting=None):
        if self.type != STRING:
            if formatting is None:
                formatting = self.formatting
            formatter = FormatterFactory.get_formatter(formatting)
            self.formatted_value = formatter.toFormat(self.value)
    def format_as_tex(self, formatting=None):
        if self.type != STRING:
            if formatting is None:
                formatting = self.formatting
            formatter = FormatterFactory.get_formatter(formatting)
            return formatter.toFormat(self.formatted_value)
        else:
            return self.value
    def get_formatted_value(self):
        return self.formatted_value if self.type != STRING else self.value
class FormatterFactory:
    @staticmethod
    def get_formatter(formatstring):
@@ -20,7 +59,7 @@
    @staticmethod
    def fman(f):
        return f/10**FormatterFactory.fexp(f)
        return f / 10 ** FormatterFactory.fexp(f)
class SciFloatFormatter(FormatterFactory):
@@ -37,10 +76,12 @@
        except TypeError:
            raise ValueError
        exp=self.fexp(num)
        man=self.fman(num)
        exp = self.fexp(num)
        man = self.fman(num)
        return ("${:." + str(self.precision-1) + "f} \cdot 10^{{{}}}$").format(man, int(exp))
        return ("${:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}$").format(
            man, int(exp)
        )
class EngFloatFormatter(FormatterFactory):
tools/tests/formatter_test.py
@@ -61,3 +61,52 @@
        sci3 = Formatter.FormatterFactory().get_formatter("sci 3")
        for case, result in zip(testcases,sci3_solutions):
            self.assertEqual(result, sci3.toFormat(case))
    def test_valueClass(self):
        testcases = [
            1.235678910111213,
            1.2,
            3,
            "3.5",
            "0.05",
            -1.2343567,
            -3,
            1.123e-6,
            -1.123e6,
            9.8123e255,
            0,
        ]
        sci2_solutions = [
            "$1.2 \cdot 10^{0}$",
            "$1.2 \cdot 10^{0}$",
            "$3.0 \cdot 10^{0}$",
            "$3.5 \cdot 10^{0}$",
            "$5.0 \cdot 10^{-2}$",
            "$-1.2 \cdot 10^{0}$",
            "$-3.0 \cdot 10^{0}$",
            "$1.1 \cdot 10^{-6}$",
            "$-1.1 \cdot 10^{6}$",
            "$9.8 \cdot 10^{255}$",
            "$0.0 \cdot 10^{0}$",
        ]
        sci3_solutions = [
            "$1.24 \cdot 10^{0}$",
            "$1.20 \cdot 10^{0}$",
            "$3.00 \cdot 10^{0}$",
            "$3.50 \cdot 10^{0}$",
            "$5.00 \cdot 10^{-2}$",
            "$-1.23 \cdot 10^{0}$",
            "$-3.00 \cdot 10^{0}$",
            "$1.12 \cdot 10^{-6}$",
            "$-1.12 \cdot 10^{6}$",
            "$9.81 \cdot 10^{255}$",
            "$0.00 \cdot 10^{0}$",
        ]
        for case,result in zip (testcases, sci2_solutions):
            self.assertEqual(result, Formatter.Variable(case, "sci 2").format_as_tex())
        for case,result in zip (testcases, sci3_solutions):
            self.assertEqual(result, Formatter.Variable(case, "sci 3").format_as_tex())