Unit test for formating. And preliminary sci formating function.
2 files modified
4 files added
| New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <project version="4"> |
| | | <component name="VcsDirectoryMappings"> |
| | | <mapping directory="$PROJECT_DIR$" vcs="Git" /> |
| | | </component> |
| | | </project> |
| | |
| | | #from .Problem import Problem |
| | | from .Generators import GeneratorFactory |
| | | from .Generators import VariableGeneratorFactory |
| | | |
| | | class BerkiParse: |
| | | def __init__(self, text=None): |
| | |
| | | def prepareGenerators(self): |
| | | variableGenerator={} |
| | | for key,value in self.parsedVariables.items(): |
| | | variableGenerator[key]= GeneratorFactory.get_generator(value["generator_type"]) |
| | | variableGenerator[key]= VariableGeneratorFactory.get_generator(value["generator_type"]) |
| | | return variableGenerator |
| | | |
| | | |
| New file |
| | |
| | | class FormatExceptionWrongParameters(Exception): |
| | | pass |
| | | |
| | | |
| | | class FloatFormatterFactory: |
| | | @staticmethod |
| | | def get_formatter(formatstring): |
| | | spl = formatstring.split() |
| | | type = spl[0] |
| | | arglist = spl[1:] |
| | | |
| | | if type == "sci": |
| | | return SciFloatFormatter(arglist, formatstring) |
| | | |
| | | else: |
| | | return None |
| | | |
| | | |
| | | class SciFloatFormatter(FloatFormatterFactory): |
| | | def __init__(self, formatparameters, formatstring=None): |
| | | if len(formatparameters) != 1: |
| | | raise FormatExceptionWrongParameters( |
| | | {"message": "Sci format accept only one argument", "text": formatstring} |
| | | ) |
| | | self.precision = int(formatparameters[0]) |
| | | |
| | | def toFormat(self, num): |
| | | try: |
| | | num = float(num) |
| | | except ValueError: |
| | | raise ValueError |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | return "${:.2f}$".format(num) |
| | |
| | | import random |
| | | |
| | | |
| | | class GeneratorFactory: |
| | | class VariableGeneratorFactory: |
| | | @staticmethod |
| | | def get_generator(definition): |
| | | spl = definition.split() |
| | |
| | | arglist = spl[1:] |
| | | |
| | | if type == "FixedVals": |
| | | return FixedGenerator(arglist) |
| | | return FixedVariableGenerator(arglist) |
| | | |
| | | else: |
| | | return None |
| | | |
| | | |
| | | class FixedGenerator(GeneratorFactory): |
| | | class FixedVariableGenerator(VariableGeneratorFactory): |
| | | def __init__(self, arglist): |
| | | self.pool = [float(i) for i in arglist] |
| | | |
| New file |
| | |
| | | import unittest |
| | | from tools import Formatter |
| | | |
| | | |
| | | class FormatTest(unittest.TestCase): |
| | | def test_Exceptions(self): |
| | | exception_cases = [None, "hello"] |
| | | exception_results = [ValueError, ValueError] |
| | | |
| | | sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2") |
| | | for case, result in zip(exception_cases, exception_results): |
| | | with self.assertRaises(result): |
| | | sci2.toFormat(case) |
| | | |
| | | def test_inputStrings(self): |
| | | # test eng formatter |
| | | testcases = [ |
| | | 1.235678910111213, |
| | | 1.2, |
| | | 3, |
| | | "3.5", |
| | | "0.05", |
| | | -1.2343567, |
| | | -3, |
| | | 1.123e-6, |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1.24$", |
| | | "$1.20$", |
| | | "$3.00$", |
| | | "$3.50$", |
| | | "$0.05$", |
| | | "$-1.23$", |
| | | "$-3.00$", |
| | | "$1.23 \cdot 10^{-6}$", |
| | | "$-1.23 \cdot 10^{6}$", |
| | | "$9.91 \cdot 10^{255}$", |
| | | "$0.00$", |
| | | ] |
| | | |
| | | sci2 = Formatter.FloatFormatterFactory().get_formatter("sci 2") |
| | | for case, result in zip(testcases, sci2_solutions): |
| | | self.assertEqual(sci2.toFormat(case), result) |