Added Variable class, that doesn't pass the test yet.
| | |
| | | from . import Exceptions |
| | | from math import floor, log10 |
| | | |
| | | STRING = 1 |
| | | FLOAT = 2 |
| | | |
| | | |
| | | class Variable: |
| | | def __init__(self, value=None, formatting="sci 3"): |
| | | try: |
| | | self.value = float(value) |
| | | self.value = FLOAT |
| | | except (ValueError, TypeError): |
| | | self.type = STRING |
| | | self.formatted_value = None |
| | | self.type = STRING |
| | | if formatting is not None: |
| | | self.format_as(formatting) |
| | | self.formatting = formatting |
| | | # self.formatter=FormatterFactory(formatting) |
| | | |
| | | def format_as(self, formatting=None): |
| | | if self.type != STRING: |
| | | if formatting is None: |
| | | formatting = self.formatting |
| | | formatter = FormatterFactory.get_formatter(formatting) |
| | | self.formatted_value = formatter.toFormat(self.value) |
| | | |
| | | def format_as_tex(self, formatting=None): |
| | | if self.type != STRING: |
| | | if formatting is None: |
| | | formatting = self.formatting |
| | | |
| | | formatter = FormatterFactory.get_formatter(formatting) |
| | | return formatter.toFormat(self.formatted_value) |
| | | else: |
| | | return self.value |
| | | |
| | | def get_formatted_value(self): |
| | | return self.formatted_value if self.type != STRING else self.value |
| | | |
| | | |
| | | class FormatterFactory: |
| | | @staticmethod |
| | | def get_formatter(formatstring): |
| | |
| | | |
| | | @staticmethod |
| | | def fman(f): |
| | | return f/10**FormatterFactory.fexp(f) |
| | | return f / 10 ** FormatterFactory.fexp(f) |
| | | |
| | | |
| | | class SciFloatFormatter(FormatterFactory): |
| | |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | exp=self.fexp(num) |
| | | man=self.fman(num) |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | |
| | | return ("${:." + str(self.precision-1) + "f} \cdot 10^{{{}}}$").format(man, int(exp)) |
| | | return ("${:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}$").format( |
| | | man, int(exp) |
| | | ) |
| | | |
| | | |
| | | class EngFloatFormatter(FormatterFactory): |
| | |
| | | sci3 = Formatter.FormatterFactory().get_formatter("sci 3") |
| | | for case, result in zip(testcases,sci3_solutions): |
| | | self.assertEqual(result, sci3.toFormat(case)) |
| | | |
| | | def test_valueClass(self): |
| | | testcases = [ |
| | | 1.235678910111213, |
| | | 1.2, |
| | | 3, |
| | | "3.5", |
| | | "0.05", |
| | | -1.2343567, |
| | | -3, |
| | | 1.123e-6, |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1.2 \cdot 10^{0}$", |
| | | "$1.2 \cdot 10^{0}$", |
| | | "$3.0 \cdot 10^{0}$", |
| | | "$3.5 \cdot 10^{0}$", |
| | | "$5.0 \cdot 10^{-2}$", |
| | | "$-1.2 \cdot 10^{0}$", |
| | | "$-3.0 \cdot 10^{0}$", |
| | | "$1.1 \cdot 10^{-6}$", |
| | | "$-1.1 \cdot 10^{6}$", |
| | | "$9.8 \cdot 10^{255}$", |
| | | "$0.0 \cdot 10^{0}$", |
| | | ] |
| | | |
| | | sci3_solutions = [ |
| | | "$1.24 \cdot 10^{0}$", |
| | | "$1.20 \cdot 10^{0}$", |
| | | "$3.00 \cdot 10^{0}$", |
| | | "$3.50 \cdot 10^{0}$", |
| | | "$5.00 \cdot 10^{-2}$", |
| | | "$-1.23 \cdot 10^{0}$", |
| | | "$-3.00 \cdot 10^{0}$", |
| | | "$1.12 \cdot 10^{-6}$", |
| | | "$-1.12 \cdot 10^{6}$", |
| | | "$9.81 \cdot 10^{255}$", |
| | | "$0.00 \cdot 10^{0}$", |
| | | ] |
| | | |
| | | for case,result in zip (testcases, sci2_solutions): |
| | | self.assertEqual(result, Formatter.Variable(case, "sci 2").format_as_tex()) |
| | | |
| | | |
| | | for case,result in zip (testcases, sci3_solutions): |
| | | self.assertEqual(result, Formatter.Variable(case, "sci 3").format_as_tex()) |