Parser of berki style problems and generator of latex file
Samo Penic
2018-10-24 d89217143df830240d8dc9e93ed28331d25545fe
Beutifying
5 files modified
98 ■■■■■ changed files
tools/BerkiParse.py 29 ●●●●● patch | view | raw | blame | history
tools/Formatter.py 26 ●●●● patch | view | raw | blame | history
tools/Generators.py 21 ●●●●● patch | view | raw | blame | history
tools/Problem.py 12 ●●●●● patch | view | raw | blame | history
tools/tests/formatter_test.py 10 ●●●● patch | view | raw | blame | history
tools/BerkiParse.py
@@ -1,5 +1,6 @@
#from .Problem import Problem
# from .Problem import Problem
from .Generators import VariableGeneratorFactory
class BerkiParse:
    def __init__(self, text=None):
@@ -7,10 +8,10 @@
        self.introduction = None
        self.subproblems = []
        self.solutions = None
        self.parsedSolutions=None
        self.parsedSolutions = None
        self.variables = []
        self.parsedVariables = None
        self.variableGenerator= None
        self.variableGenerator = None
        self._Error = []
        self._Warning = []
        self._Info = []
@@ -61,10 +62,10 @@
        self.introduction = self.findIntroduction()
        self.subproblems = self.findSubproblems()
        self.solutions = self.findSolutions()
        self.parsedSolutions=self.parseSolutions()
        self.parsedSolutions = self.parseSolutions()
        self.variables = self.findVariables()
        self.parsedVariables = self.parseVariables()
        self.variableGenerator=self.prepareGenerators()
        self.variableGenerator = self.prepareGenerators()
    def parseSolutions(self):
        parsedSolutions = []
@@ -106,13 +107,19 @@
            parsedVariables[name] = retval
        return parsedVariables
    def prepareGenerators(self):
        variableGenerator={}
        for key,value in self.parsedVariables.items():
            variableGenerator[key]= VariableGeneratorFactory.get_generator(value["generator_type"])
        variableGenerator = {}
        for key, value in self.parsedVariables.items():
            variableGenerator[key] = VariableGeneratorFactory.get_generator(
                value["generator_type"]
            )
        return variableGenerator
    def get_parsed_sections(self):
        return (self.introduction,self.subproblems, self.parsedVariables, self.parsedSolutions, self.variableGenerator)
        return (
            self.introduction,
            self.subproblems,
            self.parsedVariables,
            self.parsedSolutions,
            self.variableGenerator,
        )
tools/Formatter.py
@@ -1,6 +1,7 @@
from . import Exceptions
class FloatFormatterFactory:
class FormatterFactory:
    @staticmethod
    def get_formatter(formatstring):
        spl = formatstring.split()
@@ -8,14 +9,14 @@
        arglist = spl[1:]
        if type == "sci":
            return SciFloatFormatter(arglist, formatstring)
            return SciFloatFormatter(arglist)
        else:
            return None
class SciFloatFormatter(FloatFormatterFactory):
    def __init__(self, formatparameters, formatstring=None):
class SciFloatFormatter(FormatterFactory):
    def __init__(self, formatparameters):
        if len(formatparameters) != 1:
            raise Exceptions.WrongParameters("Sci format accept only one argument")
        self.precision = int(formatparameters[0])
@@ -28,4 +29,19 @@
        except TypeError:
            raise ValueError
        return "${:.2f}$".format(num)
        return ("${:." + str(self.precision) + "f}$").format(num)
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 toFormat(self, num):
        try:
            num = float(num)
        except (ValueError, TypeError):
            raise ValueError
        # TODO: Change formatting string
        return ("${:." + str(self.precision) + "f}$").format(num)
tools/Generators.py
@@ -1,6 +1,7 @@
import random
from . import Exceptions
class VariableGeneratorFactory:
    @staticmethod
    def get_generator(definition):
@@ -20,8 +21,10 @@
class FixedVariableGenerator(VariableGeneratorFactory):
    def __init__(self, arglist):
        if len(arglist)==0:
            raise Exceptions.WrongParameters("In FixedVals you must provide at least one element to chose from.")
        if len(arglist) == 0:
            raise Exceptions.WrongParameters(
                "In FixedVals you must provide at least one element to chose from."
            )
        try:
            self.pool = [float(i) for i in arglist]
@@ -38,18 +41,20 @@
class RandLinearVariableGenerator(VariableGeneratorFactory):
    def __init__(self, arglist):
        try:
            self.min=float(arglist[0])
            self.max=float(arglist[1])
            self.min = float(arglist[0])
            self.max = float(arglist[1])
        except (ValueError, TypeError):
            raise ValueError
        if len(arglist)!=2:
        if len(arglist) != 2:
            raise Exceptions.WrongParameters("RandLinear accepts exactly 2 arguments.")
        if self.min>=self.max:
            raise ValueError("In RandLinear, first argument must be lower than second argument.")
        if self.min >= self.max:
            raise ValueError(
                "In RandLinear, first argument must be lower than second argument."
            )
    def __next__(self):
        return random.uniform(self.min, self.max)
    def __iter__(self):
        return self
        return self
tools/Problem.py
@@ -1,20 +1,23 @@
from .BerkiParse import BerkiParse
class Problem:
    def __init__(self, parser=None):
        self.introduction = None
        self.subproblems = None
        self.parsedSolutions = None
        self.parsedVariables = None
        self.variableGenerator=None
        self.varDict={}
        self.variableGenerator = None
        self.varDict = {}
        if parser is not None:
            self.introduction, self.subproblems, self.parsedVariables, self.parsedSolutions, self.variableGenerator=parser.get_parsed_sections()
            self.introduction, self.subproblems, self.parsedVariables, self.parsedSolutions, self.variableGenerator = (
                parser.get_parsed_sections()
            )
            self.generateVariables()
    def generateVariables(self):
        for key in self.parsedVariables:
            self.varDict[key]=next(self.variableGenerator[key])
            self.varDict[key] = next(self.variableGenerator[key])
    def generateSolutions(self):
        pass
@@ -24,4 +27,3 @@
            return True
        else:
            return False
tools/tests/formatter_test.py
@@ -7,7 +7,7 @@
        exception_cases = [None, "hello"]
        exception_results = [ValueError, ValueError]
        sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2")
        sci2 = Formatter.FormatterFactory().get_formatter("sci 2")
        for case, result in zip(exception_cases, exception_results):
            with self.assertRaises(result):
                sci2.toFormat(case)
@@ -26,8 +26,8 @@
            -1.123e6,
            9.8123e255,
            0,
            "-NaN"
            "+NaN"
            "-NaN",
            "+NaN",
        ]
        sci2_solutions = [
            "$1.24$",
@@ -42,9 +42,9 @@
            "$9.91 \cdot 10^{255}$",
            "$0.00$",
            "ni rezultata",
            "ni rezultata"
            "ni rezultata",
        ]
        sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2")
        sci2 = Formatter.FormatterFactory().get_formatter("sci 2")
        for case, result in zip(testcases, sci2_solutions):
            self.assertEqual(sci2.toFormat(case), result)