Parser of berki style problems and generator of latex file
Samo Penic
2018-11-24 e0e8d373e6ebd897010753d0259468b68509a5bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import random
from . import Exceptions
 
 
class VariableGeneratorFactory:
    @staticmethod
    def get_generator(definition):
        spl = definition.split()
        type = spl[0]
        arglist = spl[1:]
 
        if type == "FixedVals":
            return FixedVariableGenerator(arglist)
        if type == "RandLinear":
            return RandLinearVariableGenerator(arglist)
 
        else:
            return None
 
 
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."
            )
 
        try:
            self.pool = [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