From 32d3f2f0b9ba799d732255599e1731074a9e6e57 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Sun, 25 Nov 2018 11:09:49 +0000
Subject: [PATCH] Fixes in sci and prefix formatting and unit tests for these two classes added.
---
aoi_gen/tests/formatter_test.py | 111 ++++++++++++++++++++++++++++++++++++
aoi_gen/Variable.py | 56 ++++++++++--------
2 files changed, 140 insertions(+), 27 deletions(-)
diff --git a/aoi_gen/Variable.py b/aoi_gen/Variable.py
index 29009b7..745bb27 100644
--- a/aoi_gen/Variable.py
+++ b/aoi_gen/Variable.py
@@ -108,10 +108,11 @@
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):
@@ -127,30 +128,27 @@
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):
@@ -206,17 +204,19 @@
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(".", ",\!")
)
@@ -268,18 +268,22 @@
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(".", ",\!")
)
diff --git a/aoi_gen/tests/formatter_test.py b/aoi_gen/tests/formatter_test.py
index 73d74d2..df823f0 100644
--- a/aoi_gen/tests/formatter_test.py
+++ b/aoi_gen/tests/formatter_test.py
@@ -13,7 +13,6 @@
sci2.toFormat(case)
def test_inputStrings(self):
- # test eng formatter
testcases = [
1.235678910111213,
1.2,
@@ -26,6 +25,8 @@
-1.123e6,
9.8123e255,
0,
+ 45,
+
]
sci2_solutions = [
"1,\!2",
@@ -39,6 +40,7 @@
"-1,\!1 \cdot 10^{6}",
"9,\!8 \cdot 10^{255}",
"0,\!0",
+ "4,\!5 \cdot 10^{1}"
]
sci3_solutions = [
@@ -53,8 +55,38 @@
"-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):
@@ -64,6 +96,13 @@
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):
@@ -79,6 +118,33 @@
-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",
@@ -115,6 +181,14 @@
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,
@@ -128,6 +202,7 @@
-1.123e6,
9.8123e255,
0,
+ 45,
]
sci2_solutions = [
"$1,\!2\,$",
@@ -141,6 +216,7 @@
"$-1,\!1 \cdot 10^{6}\,$",
"$9,\!8 \cdot 10^{255}\,$",
"$0,\!0\,$",
+ "$4,\!5 \cdot 10^{1}\,$",
]
sci3_solutions = [
@@ -155,6 +231,7 @@
"$-1,\!12 \cdot 10^{6}\,$",
"$9,\!81 \cdot 10^{255}\,$",
"$0,\!00\,$",
+ "$4,\!50 \cdot 10^{1}\,$",
]
sci2_rounded = [
@@ -169,7 +246,26 @@
-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())
@@ -180,3 +276,16 @@
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()
+ )
+
--
Gitblit v1.8.0