| | |
| | | #from .Problem import Problem |
| | | # from .Problem import Problem |
| | | from .Generators import VariableGeneratorFactory |
| | | |
| | | |
| | | class BerkiParse: |
| | | def __init__(self, text=None): |
| | |
| | | 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 = [] |
| | |
| | | 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 = [] |
| | |
| | | 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, |
| | | ) |
| | |
| | | from . import Exceptions |
| | | |
| | | class FloatFormatterFactory: |
| | | |
| | | class FormatterFactory: |
| | | @staticmethod |
| | | def get_formatter(formatstring): |
| | | spl = formatstring.split() |
| | |
| | | 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]) |
| | |
| | | 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) |
| | |
| | | import random |
| | | from . import Exceptions |
| | | |
| | | |
| | | class VariableGeneratorFactory: |
| | | @staticmethod |
| | | def get_generator(definition): |
| | |
| | | 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] |
| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | return True |
| | | else: |
| | | return False |
| | | |
| | |
| | | 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) |
| | |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | "-NaN" |
| | | "+NaN" |
| | | "-NaN", |
| | | "+NaN", |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1.24$", |
| | |
| | | "$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) |