Parser of berki style problems and generator of latex file
Samo Penic
2018-11-25 32d3f2f0b9ba799d732255599e1731074a9e6e57
Fixes in sci and prefix formatting and unit tests for these two classes added.
2 files modified
145 ■■■■ changed files
aoi_gen/Variable.py 34 ●●●●● patch | view | raw | blame | history
aoi_gen/tests/formatter_test.py 111 ●●●●● patch | view | raw | blame | history
aoi_gen/Variable.py
@@ -108,10 +108,11 @@
        pass
    def toFormat(self, string):
        return string.replace("\"", "")
        return string.replace('"', "")
    def getValue(self, string):
        return "\"{}\"".format(str(string.replace("\"", "")))
        return '"{}"'.format(str(string.replace('"', "")))
class DecFloatFormatter(FormatterFactory):
    def __init__(self, formatparameters):
@@ -131,26 +132,23 @@
        places=self.fexp(num)
        decimal_places=self.precision-places-1
        if(decimal_places<0):
        if decimal_places < 0:
            decimal_places=0
        format_str="{:"+str(places)+"."+str(decimal_places)+"f}"
        return format_str.format(num).replace(".", ",\!")
    def getValue(self, num):
        num=float(("{:."+str(self.precision-1)+"e}").format(num))
        places=self.fexp(num)
        decimal_places=self.precision-places-1
        if(decimal_places<0):
        if decimal_places < 0:
            decimal_places=0
        format_str="{:"+str(places)+"."+str(decimal_places)+"f}"
        val=format_str.format(num)
        return float(val)
class SciFloatFormatter(FormatterFactory):
@@ -206,17 +204,19 @@
            raise ValueError
        except TypeError:
            raise ValueError
        # Clip precision firstly
        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
        exp = self.fexp(num)
        man = self.fman(num)
        (exp, man) = self.realign3(exp, man)
        decimal_places = self.precision - self.fexp(man) - 1
        if decimal_places < 0:
            decimal_places = 0
        if exp == 0:
            return (
                ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!")
            )
            return ("{:." + str(decimal_places) + "f}").format(man).replace(".", ",\!")
        else:
            return (
                ("{:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}")
                ("{:." + str(decimal_places) + "f} \cdot 10^{{{}}}")
                .format(man, int(exp))
                .replace(".", ",\!")
            )
@@ -268,18 +268,22 @@
            raise ValueError
        except TypeError:
            raise ValueError
        # Clip precision firstly
        num = float(("{:." + str(self.precision - 1) + "e}").format(num))
        exp = self.fexp(num)
        man = self.fman(num)
        (exp, man) = self.realign3(exp, man)
        decimal_places = self.precision - self.fexp(man) - 1
        if decimal_places < 0:
            decimal_places = 0
        if exp == 0:
            return (
                ("{:." + str(self.precision - 1) + "f}\,").format(man).replace(".", ",\!")
                ("{:." + str(decimal_places) + "f}\,").format(man).replace(".", ",\!")
            )
        else:
            prefix = self.exp2prefix(exp)
            return (
                ("{:." + str(self.precision - 1) + "f}\,\mathrm{{{}}}")
                ("{:." + str(decimal_places) + "f}\,\mathrm{{{}}}")
                .format(man, prefix)
                .replace(".", ",\!")
            )
aoi_gen/tests/formatter_test.py
@@ -13,7 +13,6 @@
                sci2.toFormat(case)
    def test_inputStrings(self):
        # test eng formatter
        testcases = [
            1.235678910111213,
            1.2,
@@ -26,6 +25,8 @@
            -1.123e6,
            9.8123e255,
            0,
            45,
        ]
        sci2_solutions = [
            "1,\!2",
@@ -39,6 +40,7 @@
            "-1,\!1 \cdot 10^{6}",
            "9,\!8 \cdot 10^{255}",
            "0,\!0",
            "4,\!5 \cdot 10^{1}"
        ]
        sci3_solutions = [
@@ -53,8 +55,38 @@
            "-1,\!12 \cdot 10^{6}",
            "9,\!81 \cdot 10^{255}",
            "0,\!00",
            "4,\!50 \cdot 10^{1}"
        ]
        eng2_solutions = [
            "1,\!2",
            "1,\!2",
            "3,\!0",
            "3,\!5",
            "50 \cdot 10^{-3}",
            "-1,\!2",
            "-3,\!0",
            "1,\!1 \cdot 10^{-6}",
            "-1,\!1 \cdot 10^{6}",
            "9,\!8 \cdot 10^{255}",
            "0,\!0",
            "45"
        ]
        eng1_solutions = [
            "1",
            "1",
            "3",
            "4",
            "50 \cdot 10^{-3}",
            "-1",
            "-3",
            "1 \cdot 10^{-6}",
            "-1 \cdot 10^{6}",
            "10 \cdot 10^{255}",
            "0",
            "40"
        ]
        sci2 = Variable.FormatterFactory().get_formatter("sci 2")
        for case, result in zip(testcases, sci2_solutions):
@@ -64,6 +96,13 @@
        for case, result in zip(testcases, sci3_solutions):
            self.assertEqual(result, sci3.toFormat(case))
        eng1 = Variable.FormatterFactory().get_formatter("eng 1")
        for case, result in zip(testcases, eng1_solutions):
            self.assertEqual(result, eng1.toFormat(case))
        eng2 = Variable.FormatterFactory().get_formatter("eng 2")
        for case, result in zip(testcases, eng2_solutions):
            self.assertEqual(result, eng2.toFormat(case))
    def test_additionalDecTests(self):
@@ -79,6 +118,33 @@
            -1.123e6,
            0,
            32,
        ]
        prefix2_solutions =[
            "1,\!2\,",
            "1,\!2\,",
            "3,\!0\,",
            "3,\!5\,",
            "50\,\mathrm{m}",
            "-1,\!2\,",
            "-3,\!0\,",
            "1,\!1\,\mathrm{\\upmu}",
            "-1,\!1\,\mathrm{M}",
            "0,\!0\,",
            "32\,"
        ]
        prefix1_solutions = [
            "1\,",
            "1\,",
            "3\,",
            "4\,",
            "50\,\mathrm{m}",
            "-1\,",
            "-3\,",
            "1\,\mathrm{\\upmu}",
            "-1\,\mathrm{M}",
            "0\,",
            "30\,"
        ]
        dec2_solutions = [
            "1,\!2",
@@ -115,6 +181,14 @@
        for case, result in zip(testcases, dec1_solutions):
            self.assertEqual(result, dec1.toFormat(case))
        prefix2 = Variable.FormatterFactory().get_formatter("prefix 2")
        for case, result in zip(testcases, prefix2_solutions):
            self.assertEqual(result, prefix2.toFormat(case))
        prefix1 = Variable.FormatterFactory().get_formatter("prefix 1")
        for case, result in zip(testcases, prefix1_solutions):
            self.assertEqual(result, prefix1.toFormat(case))
    def test_valueClass(self):
        testcases = [
            1.235678910111213,
@@ -128,6 +202,7 @@
            -1.123e6,
            9.8123e255,
            0,
            45,
        ]
        sci2_solutions = [
            "$1,\!2\,$",
@@ -141,6 +216,7 @@
            "$-1,\!1 \cdot 10^{6}\,$",
            "$9,\!8 \cdot 10^{255}\,$",
            "$0,\!0\,$",
            "$4,\!5 \cdot 10^{1}\,$",
        ]
        sci3_solutions = [
@@ -155,6 +231,7 @@
            "$-1,\!12 \cdot 10^{6}\,$",
            "$9,\!81 \cdot 10^{255}\,$",
            "$0,\!00\,$",
            "$4,\!50 \cdot 10^{1}\,$",
        ]
        sci2_rounded = [
@@ -169,7 +246,26 @@
            -1.1e6,
            9.8e255,
            0,
            45
        ]
        sci1_rounded = [
            1,
            1,
            3,
            4,
            50e-3,
            -1,
            -3,
            1e-6,
            -1e6,
            10e255,
            0,
            40
        ]
        eng1_rounded=sci1_rounded
        eng2_rounded=sci2_rounded
        for case, result in zip(testcases, sci2_solutions):
            self.assertEqual(result, Variable.Variable(case, "sci 2").format_as_tex())
@@ -180,3 +276,16 @@
            self.assertEqual(
                result, Variable.Variable(case, "sci 2").get_formatted_value()
            )
        for case, result in zip(testcases, sci1_rounded):
            self.assertEqual(
                result, Variable.Variable(case, "sci 1").get_formatted_value()
            )
        for case, result in zip(testcases, eng1_rounded):
            self.assertEqual(
                result, Variable.Variable(case, "eng 1").get_formatted_value()
            )
        for case, result in zip(testcases, eng2_rounded):
            self.assertEqual(
                result, Variable.Variable(case, "eng 2").get_formatted_value()
            )