Exceptions are in separate tools file and Additional generator added.
3 files modified
1 files added
| New file |
| | |
| | | class WrongParameters(Exception): |
| | | pass |
| | | |
| | |
| | | class FormatExceptionWrongParameters(Exception): |
| | | pass |
| | | |
| | | from . import Exceptions |
| | | |
| | | class FloatFormatterFactory: |
| | | @staticmethod |
| | |
| | | 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): |
| | |
| | | import random |
| | | from . import Exceptions |
| | | |
| | | class VariableGeneratorFactory: |
| | | @staticmethod |
| | |
| | | |
| | | if type == "FixedVals": |
| | | return FixedVariableGenerator(arglist) |
| | | if type == "RandLinear": |
| | | return RandLinearVariableGenerator(arglist) |
| | | |
| | | else: |
| | | return None |
| | |
| | | |
| | | 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 |
| | |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | "-NaN" |
| | | "+NaN" |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1.24$", |
| | |
| | | "$-1.23 \cdot 10^{6}$", |
| | | "$9.91 \cdot 10^{255}$", |
| | | "$0.00$", |
| | | "ni rezultata", |
| | | "ni rezultata" |
| | | ] |
| | | |
| | | sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2") |