Fixes in sci and prefix formatting and unit tests for these two classes added.
| | |
| | | pass |
| | | |
| | | def toFormat(self, string): |
| | | return string.replace("\"", "") |
| | | return string.replace('"', "") |
| | | |
| | | def getValue(self, string): |
| | | return "\"{}\"".format(str(string.replace("\"", ""))) |
| | | return '"{}"'.format(str(string.replace('"', ""))) |
| | | |
| | | |
| | | class DecFloatFormatter(FormatterFactory): |
| | | def __init__(self, formatparameters): |
| | |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | num=float(("{:."+str(self.precision-1)+"e}").format(num)) |
| | | places=self.fexp(num) |
| | | num = float(("{:." + str(self.precision - 1) + "e}").format(num)) |
| | | places = self.fexp(num) |
| | | |
| | | decimal_places=self.precision-places-1 |
| | | if(decimal_places<0): |
| | | decimal_places=0 |
| | | format_str="{:"+str(places)+"."+str(decimal_places)+"f}" |
| | | decimal_places = self.precision - places - 1 |
| | | if decimal_places < 0: |
| | | decimal_places = 0 |
| | | format_str = "{:" + str(places) + "." + str(decimal_places) + "f}" |
| | | |
| | | return format_str.format(num).replace(".", ",\!") |
| | | |
| | | |
| | | def getValue(self, num): |
| | | |
| | | num = float(("{:." + str(self.precision - 1) + "e}").format(num)) |
| | | places = self.fexp(num) |
| | | |
| | | num=float(("{:."+str(self.precision-1)+"e}").format(num)) |
| | | places=self.fexp(num) |
| | | |
| | | decimal_places=self.precision-places-1 |
| | | if(decimal_places<0): |
| | | decimal_places=0 |
| | | format_str="{:"+str(places)+"."+str(decimal_places)+"f}" |
| | | val=format_str.format(num) |
| | | decimal_places = self.precision - places - 1 |
| | | if decimal_places < 0: |
| | | decimal_places = 0 |
| | | format_str = "{:" + str(places) + "." + str(decimal_places) + "f}" |
| | | val = format_str.format(num) |
| | | return float(val) |
| | | |
| | | |
| | | |
| | | class SciFloatFormatter(FormatterFactory): |
| | |
| | | raise ValueError |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | # Clip precision firstly |
| | | num = float(("{:." + str(self.precision - 1) + "e}").format(num)) |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | (exp, man) = self.realign3(exp, man) |
| | | decimal_places = self.precision - self.fexp(man) - 1 |
| | | if decimal_places < 0: |
| | | decimal_places = 0 |
| | | if exp == 0: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}").format(man).replace(".", ",\!") |
| | | ) |
| | | return ("{:." + str(decimal_places) + "f}").format(man).replace(".", ",\!") |
| | | else: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f} \cdot 10^{{{}}}") |
| | | ("{:." + str(decimal_places) + "f} \cdot 10^{{{}}}") |
| | | .format(man, int(exp)) |
| | | .replace(".", ",\!") |
| | | ) |
| | |
| | | raise ValueError |
| | | except TypeError: |
| | | raise ValueError |
| | | |
| | | # Clip precision firstly |
| | | num = float(("{:." + str(self.precision - 1) + "e}").format(num)) |
| | | exp = self.fexp(num) |
| | | man = self.fman(num) |
| | | (exp, man) = self.realign3(exp, man) |
| | | decimal_places = self.precision - self.fexp(man) - 1 |
| | | if decimal_places < 0: |
| | | decimal_places = 0 |
| | | if exp == 0: |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}\,").format(man).replace(".", ",\!") |
| | | ("{:." + str(decimal_places) + "f}\,").format(man).replace(".", ",\!") |
| | | ) |
| | | else: |
| | | prefix = self.exp2prefix(exp) |
| | | return ( |
| | | ("{:." + str(self.precision - 1) + "f}\,\mathrm{{{}}}") |
| | | ("{:." + str(decimal_places) + "f}\,\mathrm{{{}}}") |
| | | .format(man, prefix) |
| | | .replace(".", ",\!") |
| | | ) |
| | |
| | | sci2.toFormat(case) |
| | | |
| | | def test_inputStrings(self): |
| | | # test eng formatter |
| | | testcases = [ |
| | | 1.235678910111213, |
| | | 1.2, |
| | |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | 45, |
| | | |
| | | ] |
| | | sci2_solutions = [ |
| | | "1,\!2", |
| | |
| | | "-1,\!1 \cdot 10^{6}", |
| | | "9,\!8 \cdot 10^{255}", |
| | | "0,\!0", |
| | | "4,\!5 \cdot 10^{1}" |
| | | ] |
| | | |
| | | sci3_solutions = [ |
| | |
| | | "-1,\!12 \cdot 10^{6}", |
| | | "9,\!81 \cdot 10^{255}", |
| | | "0,\!00", |
| | | "4,\!50 \cdot 10^{1}" |
| | | ] |
| | | |
| | | eng2_solutions = [ |
| | | "1,\!2", |
| | | "1,\!2", |
| | | "3,\!0", |
| | | "3,\!5", |
| | | "50 \cdot 10^{-3}", |
| | | "-1,\!2", |
| | | "-3,\!0", |
| | | "1,\!1 \cdot 10^{-6}", |
| | | "-1,\!1 \cdot 10^{6}", |
| | | "9,\!8 \cdot 10^{255}", |
| | | "0,\!0", |
| | | "45" |
| | | ] |
| | | |
| | | eng1_solutions = [ |
| | | "1", |
| | | "1", |
| | | "3", |
| | | "4", |
| | | "50 \cdot 10^{-3}", |
| | | "-1", |
| | | "-3", |
| | | "1 \cdot 10^{-6}", |
| | | "-1 \cdot 10^{6}", |
| | | "10 \cdot 10^{255}", |
| | | "0", |
| | | "40" |
| | | ] |
| | | |
| | | sci2 = Variable.FormatterFactory().get_formatter("sci 2") |
| | | for case, result in zip(testcases, sci2_solutions): |
| | |
| | | for case, result in zip(testcases, sci3_solutions): |
| | | self.assertEqual(result, sci3.toFormat(case)) |
| | | |
| | | eng1 = Variable.FormatterFactory().get_formatter("eng 1") |
| | | for case, result in zip(testcases, eng1_solutions): |
| | | self.assertEqual(result, eng1.toFormat(case)) |
| | | |
| | | eng2 = Variable.FormatterFactory().get_formatter("eng 2") |
| | | for case, result in zip(testcases, eng2_solutions): |
| | | self.assertEqual(result, eng2.toFormat(case)) |
| | | |
| | | |
| | | def test_additionalDecTests(self): |
| | |
| | | -1.123e6, |
| | | 0, |
| | | 32, |
| | | ] |
| | | |
| | | prefix2_solutions =[ |
| | | "1,\!2\,", |
| | | "1,\!2\,", |
| | | "3,\!0\,", |
| | | "3,\!5\,", |
| | | "50\,\mathrm{m}", |
| | | "-1,\!2\,", |
| | | "-3,\!0\,", |
| | | "1,\!1\,\mathrm{\\upmu}", |
| | | "-1,\!1\,\mathrm{M}", |
| | | "0,\!0\,", |
| | | "32\," |
| | | ] |
| | | prefix1_solutions = [ |
| | | "1\,", |
| | | "1\,", |
| | | "3\,", |
| | | "4\,", |
| | | "50\,\mathrm{m}", |
| | | "-1\,", |
| | | "-3\,", |
| | | "1\,\mathrm{\\upmu}", |
| | | "-1\,\mathrm{M}", |
| | | "0\,", |
| | | "30\," |
| | | ] |
| | | dec2_solutions = [ |
| | | "1,\!2", |
| | |
| | | for case, result in zip(testcases, dec1_solutions): |
| | | self.assertEqual(result, dec1.toFormat(case)) |
| | | |
| | | prefix2 = Variable.FormatterFactory().get_formatter("prefix 2") |
| | | for case, result in zip(testcases, prefix2_solutions): |
| | | self.assertEqual(result, prefix2.toFormat(case)) |
| | | |
| | | prefix1 = Variable.FormatterFactory().get_formatter("prefix 1") |
| | | for case, result in zip(testcases, prefix1_solutions): |
| | | self.assertEqual(result, prefix1.toFormat(case)) |
| | | |
| | | def test_valueClass(self): |
| | | testcases = [ |
| | | 1.235678910111213, |
| | |
| | | -1.123e6, |
| | | 9.8123e255, |
| | | 0, |
| | | 45, |
| | | ] |
| | | sci2_solutions = [ |
| | | "$1,\!2\,$", |
| | |
| | | "$-1,\!1 \cdot 10^{6}\,$", |
| | | "$9,\!8 \cdot 10^{255}\,$", |
| | | "$0,\!0\,$", |
| | | "$4,\!5 \cdot 10^{1}\,$", |
| | | ] |
| | | |
| | | sci3_solutions = [ |
| | |
| | | "$-1,\!12 \cdot 10^{6}\,$", |
| | | "$9,\!81 \cdot 10^{255}\,$", |
| | | "$0,\!00\,$", |
| | | "$4,\!50 \cdot 10^{1}\,$", |
| | | ] |
| | | |
| | | sci2_rounded = [ |
| | |
| | | -1.1e6, |
| | | 9.8e255, |
| | | 0, |
| | | 45 |
| | | ] |
| | | |
| | | sci1_rounded = [ |
| | | 1, |
| | | 1, |
| | | 3, |
| | | 4, |
| | | 50e-3, |
| | | -1, |
| | | -3, |
| | | 1e-6, |
| | | -1e6, |
| | | 10e255, |
| | | 0, |
| | | 40 |
| | | ] |
| | | eng1_rounded=sci1_rounded |
| | | eng2_rounded=sci2_rounded |
| | | |
| | | for case, result in zip(testcases, sci2_solutions): |
| | | self.assertEqual(result, Variable.Variable(case, "sci 2").format_as_tex()) |
| | | |
| | |
| | | self.assertEqual( |
| | | result, Variable.Variable(case, "sci 2").get_formatted_value() |
| | | ) |
| | | for case, result in zip(testcases, sci1_rounded): |
| | | self.assertEqual( |
| | | result, Variable.Variable(case, "sci 1").get_formatted_value() |
| | | ) |
| | | for case, result in zip(testcases, eng1_rounded): |
| | | self.assertEqual( |
| | | result, Variable.Variable(case, "eng 1").get_formatted_value() |
| | | ) |
| | | for case, result in zip(testcases, eng2_rounded): |
| | | self.assertEqual( |
| | | result, Variable.Variable(case, "eng 2").get_formatted_value() |
| | | ) |
| | | |