Parser of berki style problems and generator of latex file
Samo Penic
2018-10-23 91fb1533c66e7145133a8afdfc55abebddbc3763
Exceptions are in separate tools file and Additional generator added.
3 files modified
1 files added
47 ■■■■ changed files
tools/Exceptions.py 3 ●●●●● patch | view | raw | blame | history
tools/Formatter.py 8 ●●●● patch | view | raw | blame | history
tools/Generators.py 32 ●●●●● patch | view | raw | blame | history
tools/tests/formatter_test.py 4 ●●●● patch | view | raw | blame | history
tools/Exceptions.py
New file
@@ -0,0 +1,3 @@
class WrongParameters(Exception):
    pass
tools/Formatter.py
@@ -1,6 +1,4 @@
class FormatExceptionWrongParameters(Exception):
    pass
from . import Exceptions
class FloatFormatterFactory:
    @staticmethod
@@ -19,9 +17,7 @@
class SciFloatFormatter(FloatFormatterFactory):
    def __init__(self, formatparameters, formatstring=None):
        if len(formatparameters) != 1:
            raise FormatExceptionWrongParameters(
                {"message": "Sci format accept only one argument", "text": formatstring}
            )
            raise Exceptions.WrongParameters("Sci format accept only one argument")
        self.precision = int(formatparameters[0])
    def toFormat(self, num):
tools/Generators.py
@@ -1,4 +1,5 @@
import random
from . import Exceptions
class VariableGeneratorFactory:
    @staticmethod
@@ -9,6 +10,8 @@
        if type == "FixedVals":
            return FixedVariableGenerator(arglist)
        if type == "RandLinear":
            return RandLinearVariableGenerator(arglist)
        else:
            return None
@@ -16,10 +19,37 @@
class FixedVariableGenerator(VariableGeneratorFactory):
    def __init__(self, arglist):
        self.pool = [float(i) for i in arglist]
        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]
        except (ValueError, TypeError):
            raise ValueError
    def __next__(self):
        return random.choice(self.pool)
    def __iter__(self):
        return self
class RandLinearVariableGenerator(VariableGeneratorFactory):
    def __init__(self, arglist):
        try:
            self.min=float(arglist[0])
            self.max=float(arglist[1])
        except (ValueError, TypeError):
            raise ValueError
        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.")
    def __next__(self):
        return random.uniform(self.min, self.max)
    def __iter__(self):
        return self
tools/tests/formatter_test.py
@@ -26,6 +26,8 @@
            -1.123e6,
            9.8123e255,
            0,
            "-NaN"
            "+NaN"
        ]
        sci2_solutions = [
            "$1.24$",
@@ -39,6 +41,8 @@
            "$-1.23 \cdot 10^{6}$",
            "$9.91 \cdot 10^{255}$",
            "$0.00$",
            "ni rezultata",
            "ni rezultata"
        ]
        sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2")