Parser of berki style problems and generator of latex file
Samo Penic
2018-10-23 4048230288f363de0c9212d1e27c19920c89afeb
Unit test for formating. And preliminary sci formating function.
2 files modified
4 files added
98 ■■■■■ changed files
.idea/vcs.xml 6 ●●●●● patch | view | raw | blame | history
tools/BerkiParse.py 4 ●●●● patch | view | raw | blame | history
tools/Formatter.py 35 ●●●●● patch | view | raw | blame | history
tools/Generators.py 7 ●●●●● patch | view | raw | blame | history
tools/tests/__init__.py patch | view | raw | blame | history
tools/tests/formatter_test.py 46 ●●●●● patch | view | raw | blame | history
.idea/vcs.xml
New file
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="$PROJECT_DIR$" vcs="Git" />
  </component>
</project>
tools/BerkiParse.py
@@ -1,5 +1,5 @@
#from .Problem import Problem
from .Generators import GeneratorFactory
from .Generators import VariableGeneratorFactory
class BerkiParse:
    def __init__(self, text=None):
@@ -110,7 +110,7 @@
    def prepareGenerators(self):
        variableGenerator={}
        for key,value in self.parsedVariables.items():
            variableGenerator[key]= GeneratorFactory.get_generator(value["generator_type"])
            variableGenerator[key]= VariableGeneratorFactory.get_generator(value["generator_type"])
        return variableGenerator
tools/Formatter.py
New file
@@ -0,0 +1,35 @@
class FormatExceptionWrongParameters(Exception):
    pass
class FloatFormatterFactory:
    @staticmethod
    def get_formatter(formatstring):
        spl = formatstring.split()
        type = spl[0]
        arglist = spl[1:]
        if type == "sci":
            return SciFloatFormatter(arglist, formatstring)
        else:
            return None
class SciFloatFormatter(FloatFormatterFactory):
    def __init__(self, formatparameters, formatstring=None):
        if len(formatparameters) != 1:
            raise FormatExceptionWrongParameters(
                {"message": "Sci format accept only one argument", "text": formatstring}
            )
        self.precision = int(formatparameters[0])
    def toFormat(self, num):
        try:
            num = float(num)
        except ValueError:
            raise ValueError
        except TypeError:
            raise ValueError
        return "${:.2f}$".format(num)
tools/Generators.py
@@ -1,7 +1,6 @@
import random
class GeneratorFactory:
class VariableGeneratorFactory:
    @staticmethod
    def get_generator(definition):
        spl = definition.split()
@@ -9,13 +8,13 @@
        arglist = spl[1:]
        if type == "FixedVals":
            return FixedGenerator(arglist)
            return FixedVariableGenerator(arglist)
        else:
            return None
class FixedGenerator(GeneratorFactory):
class FixedVariableGenerator(VariableGeneratorFactory):
    def __init__(self, arglist):
        self.pool = [float(i) for i in arglist]
tools/tests/__init__.py
tools/tests/formatter_test.py
New file
@@ -0,0 +1,46 @@
import unittest
from tools import Formatter
class FormatTest(unittest.TestCase):
    def test_Exceptions(self):
        exception_cases = [None, "hello"]
        exception_results = [ValueError, ValueError]
        sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2")
        for case, result in zip(exception_cases, exception_results):
            with self.assertRaises(result):
                sci2.toFormat(case)
    def test_inputStrings(self):
        # test eng formatter
        testcases = [
            1.235678910111213,
            1.2,
            3,
            "3.5",
            "0.05",
            -1.2343567,
            -3,
            1.123e-6,
            -1.123e6,
            9.8123e255,
            0,
        ]
        sci2_solutions = [
            "$1.24$",
            "$1.20$",
            "$3.00$",
            "$3.50$",
            "$0.05$",
            "$-1.23$",
            "$-3.00$",
            "$1.23 \cdot 10^{-6}$",
            "$-1.23 \cdot 10^{6}$",
            "$9.91 \cdot 10^{255}$",
            "$0.00$",
        ]
        sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2")
        for case, result in zip(testcases, sci2_solutions):
            self.assertEqual(sci2.toFormat(case), result)