Parser of berki style problems and generator of latex file
Samo Penic
2018-10-31 ce4fc9389dc3007ed39f6e94a4b7ae99f0fbe614
Added Eng and Dec formatters
4 files modified
150 ■■■■ changed files
testcases/dvovod3.txt 4 ●●●● patch | view | raw | blame | history
tools/Exceptions.py 3 ●●●●● patch | view | raw | blame | history
tools/Template.py 44 ●●●● patch | view | raw | blame | history
tools/Variable.py 99 ●●●● patch | view | raw | blame | history
testcases/dvovod3.txt
@@ -40,7 +40,7 @@
#StartRezultat
TeX:        U
izpis:        sci 3
izpis:        eng 3
enota:        V
formula:    eps0=8.854e-12;D=sqrt(d^2+4*h^2);U=q*log(2*h*d/(r0*D))/pi/eps0;U
napacna:    pi*eps0/log(2*h*d/(r0*D))/q^2
@@ -85,7 +85,7 @@
#StartSkalar
ime:    r0
izpis:    sci 1
izpis:    dec 1
nacin:    FixedVals 5e-3 6e-3 8e-3
#EndSkalar
tools/Exceptions.py
@@ -9,3 +9,6 @@
class AnswerProximityError(Exception):
    pass
class PrefixError(Exception):
    pass
tools/Template.py
@@ -37,10 +37,18 @@
                retstr += Template(self.template["subproblem"]).substitute(
                    {
                        "text": sp,
                        "ans1": sol["shuffled"][0][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']), #sol["correct"][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']),
                        "ans2": sol["shuffled"][1][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']),
                        "ans3": sol["shuffled"][2][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']),
                        "ans4": sol["shuffled"][3][0].format_as_tex(glyph=sol['glyph'], unit=sol['unit']),
                        "ans1": sol["shuffled"][0][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans2": sol["shuffled"][1][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans3": sol["shuffled"][2][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                        "ans4": sol["shuffled"][3][0].format_as_tex(
                            glyph=sol["glyph"], unit=sol["unit"]
                        ),
                    }
                )
            retstr += Template(self.template["subproblem_end"]).substitute()
@@ -49,10 +57,30 @@
                {
                    "problem_number": 0,
                    "text": problem_dict["introduction"],
                    "ans1": problem_dict["solutions"][0]["shuffled"][0][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]),
                    "ans2": problem_dict["solutions"][0]["shuffled"][1][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]),
                    "ans3": problem_dict["solutions"][0]["shuffled"][2][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"]),
                    "ans4": problem_dict["solutions"][0]["shuffled"][3][0].format_as_tex(glyph=problem_dict["solutions"][0]["glyph"],unit=problem_dict["solutions"][0]["unit"])
                    "ans1": problem_dict["solutions"][0]["shuffled"][0][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans2": problem_dict["solutions"][0]["shuffled"][1][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans3": problem_dict["solutions"][0]["shuffled"][2][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                    "ans4": problem_dict["solutions"][0]["shuffled"][3][
                        0
                    ].format_as_tex(
                        glyph=problem_dict["solutions"][0]["glyph"],
                        unit=problem_dict["solutions"][0]["unit"],
                    ),
                }
            )
        return retstr
tools/Variable.py
@@ -20,7 +20,7 @@
        self.formatting = formatting
    def is_float(self):
        if self.type==FLOAT:
        if self.type == FLOAT:
            return True
        else:
            return False
@@ -137,25 +137,98 @@
        return float(man)
class DecFloatFormatter(SciFloatFormatter):
    pass
class EngFloatFormatter(SciFloatFormatter):
    pass
class EngFloatFormatter1(FormatterFactory):
class EngFloatFormatter(FormatterFactory):
    def __init__(self, formatparameters):
        if len(formatparameters) != 1:
            raise Exceptions.WrongParameters("Eng format accept only one argument")
        self.precision = int(formatparameters[0])
    def realign3(self, exp, man):
        mul = exp % 3
        man = man * 10 ** mul
        exp = exp - mul
        return (exp, man)
    def toFormat(self, num):
        try:
            num = float(num)
        except (ValueError, TypeError):
        except ValueError:
            raise ValueError
        # TODO: Change formatting string
        except TypeError:
            raise ValueError
        return ("{:." + str(self.precision) + "f}").format(num)
        exp = self.fexp(num)
        man = self.fman(num)
        (exp, man) = self.realign3(exp, man)
        if exp == 0:
            return (
                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
            )
        else:
            return (
                ("{:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}")
                .format(man, int(exp))
                .replace(".", ",\!")
            )
    def getValue(self, num):
        exp = self.fexp(num)
        man = self.fman(num)
        man = ("{:." + str(self.precision - 1) + "f}e{}").format(man, int(exp))
        return float(man)
class DecFloatFormatter(EngFloatFormatter):
    def __init__(self, formatparameters):
        if len(formatparameters) != 1:
            raise Exceptions.WrongParameters("Dec format accept only one argument")
        self.precision = int(formatparameters[0])
    def exp2prefix(self, exp):
        prefixes = {
            "24": "Y",
            "21": "Z",
            "18": "E",
            "15": "P",
            "12": "T",
            "9": "G",
            "6": "M",
            "3": "k",
            "-3": "m",
            "-6":"\\upmu",
            "-9":"n",
            "-12":"p",
            "-15":"f",
            "-18":"a",
            "-21":"z",
            "-24":"y"
        }
        try:
            prefix = prefixes[str(exp)]
        except KeyError:
            raise Exceptions.PrefixError("Could not change exponent " + str(exp) + " into prefix form!")
        return prefix
    def toFormat(self, num):
        try:
            num = float(num)
        except ValueError:
            raise ValueError
        except TypeError:
            raise ValueError
        exp = self.fexp(num)
        man = self.fman(num)
        (exp, man) = self.realign3(exp, man)
        if exp == 0:
            return (
                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
            )
        else:
            prefix = self.exp2prefix(exp)
            return (
                ("{:." + str(self.precision - 1) + "f}~\mathrm{{{}}}")
                .format(man, prefix)
                .replace(".", ",\!")
            )