Parser of berki style problems and generator of latex file
Samo Penic
2018-11-13 da1010f84bf6d224cca629d8d44072f144e3bf7e
In formatting, space is always added after numerical values.
3 files modified
66 ■■■■■ changed files
aoi_gen/Problem.py 2 ●●● patch | view | raw | blame | history
aoi_gen/Variable.py 62 ●●●●● patch | view | raw | blame | history
testcases/dvovod31.txt 2 ●●● patch | view | raw | blame | history
aoi_gen/Problem.py
@@ -151,7 +151,7 @@
    def substitute_variables(self, text):
        for key, var in self.varDict.items():
            text = re.sub("\/\*\/" + key + "\/\*\/", var.format_without_dollar(), text)
            text = re.sub("\/\*\/" + key + "\/\*\/", var.format_as_tex(dollar=""), text)
        return text
    def substitute_octave(self, text):
aoi_gen/Variable.py
@@ -31,28 +31,37 @@
        formatter = FormatterFactory.get_formatter(formatting)
        self.formatted_value = formatter.getValue(self.value)
    def format_as_tex(self, formatting=None, glyph=None, unit=None):
    def format_as_tex(self, formatting=None, glyph=None, unit=None, dollar="$"):
        if formatting is None:
            formatting = self.formatting
        formatter = FormatterFactory.get_formatter(formatting)
        if formatting.split()[0] == "prefix":
            leading_space = ""
        else:
            leading_space = "~"
        if formatting == "str" or formatting == "string":
            return formatter.toFormat(self.value)
        elif glyph is None and unit is None:
            return ("${}$").format(formatter.toFormat(self.value))
        elif glyph is None and unit is not None:
            return ("${}~\mathrm{{{}}}$").format(formatter.toFormat(self.value), unit)
        elif glyph is not None and unit is None:
            return ("${}={}$").format(glyph, formatter.toFormat(self.value))
        else:
            return ("${}={}~\mathrm{{{}}}$").format(
                glyph, formatter.toFormat(self.value), unit
            return ("{}{}{}{}").format(
                dollar, formatter.toFormat(self.value), leading_space, dollar
            )
    def format_without_dollar(self, formatting=None):
        if formatting is None:
            formatting = self.formatting
        formatter = FormatterFactory.get_formatter(formatting)
        return formatter.toFormat(self.value)
        elif glyph is None and unit is not None:
            return ("{}{}{}\mathrm{{{}}}{}{}").format(
                dollar, formatter.toFormat(self.value), leading_space, unit, dollar
            )
        elif glyph is not None and unit is None:
            return ("{}{}={}{}{}").format(
                dollar, glyph, formatter.toFormat(self.value), leading_space, dollar
            )
        else:
            return ("{}{}={}{}\mathrm{{{}}}{}").format(
                dollar,
                glyph,
                formatter.toFormat(self.value),
                leading_space,
                unit,
                dollar,
            )
    def get_formatted_value(self):
        return self.formatted_value
@@ -81,7 +90,7 @@
        elif type == "prefix":
            return PrefixFloatFormatter(arglist)
        elif type == "dec" or type == "decimal":
            return EngFloatFormatter(arglist) #fallback to engineering
            return EngFloatFormatter(arglist)  # fallback to engineering
        else:
            return None
@@ -197,20 +206,21 @@
            "6": "M",
            "3": "k",
            "-3": "m",
            "-6":"\\upmu",
            "-9":"n",
            "-12":"p",
            "-15":"f",
            "-18":"a",
            "-21":"z",
            "-24":"y"
            "-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!")
            raise Exceptions.PrefixError(
                "Could not change exponent " + str(exp) + " into prefix form!"
            )
        return prefix
    def toFormat(self, num):
        try:
@@ -225,7 +235,7 @@
        (exp, man) = self.realign3(exp, man)
        if exp == 0:
            return (
                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
                ("{:." + str(self.precision - 1) + "f}~").format(man).replace(".", ",\!")
            )
        else:
            prefix = self.exp2prefix(exp)
testcases/dvovod31.txt
@@ -43,7 +43,7 @@
#StartRezultat
TeX:        E(\mathrm{A})
izpis:        sci 3
izpis:        prefix 3
enota:        V/m
formula:    eps0=8.854e-12;EA=q/(pi*eps0*h)*abs(-1+1/(1+(d/h)^2));EA
napacna:    q/(2*pi*eps0*h)*abs(-1+1/(1+(d/h)^2))