aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ee/formatting/__init__.py47
-rw-r--r--test/test_formatting.py38
-rw-r--r--test/tests.py48
-rw-r--r--tox.ini1
4 files changed, 58 insertions, 76 deletions
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py
index 3c592ef..a1e27a4 100644
--- a/src/ee/formatting/__init__.py
+++ b/src/ee/formatting/__init__.py
@@ -1,4 +1,3 @@
-#import sympy as sp
import math
from ee import EeError
@@ -7,23 +6,15 @@ __all__ = [
'eng_str',
]
-#class Symbols(object):
-# def __init__(self):
-# self.tau = sp.symbols('tau'),
-# self.r = sp.symbols('r'),
-# self.c = sp.symbols('c')
-#
-#_s = Symbols()
-
class ESeries(object):
- def __init__(self, series):
- self.series = series
+ def __init__(self, series):
+ self.series = series
- def closest(self, value):
- e = math.floor(math.log10(value))
- value = float(value/(10**e))
+ def closest(self, value):
+ e = math.floor(math.log10(value))
+ value = float(value/(10**e))
- return min(self.series, key=lambda v: abs(v - value)) * 10**e
+ return min(self.series, key=lambda v: abs(v - value)) * 10**e
# https://en.wikipedia.org/wiki/E-series_of_preferred_numbers
_e_series_24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]
@@ -63,23 +54,23 @@ e96 = ESeries(_e_series_96)
e192 = ESeries(_e_series_192)
def e_series_find_closest(value):
- e = math.floor(math.log10(value))
- value = float(value/(10**e))
+ e = math.floor(math.log10(value))
+ value = float(value/(10**e))
- return min(series, key=lambda v: abs(v - value)) * 10**e
+ return min(series, key=lambda v: abs(v - value)) * 10**e
def eng_str(value):
- big = ['', ' k', ' M', ' G', ' T']
- e_floor = math.floor(math.log10(value))
+ big = ['', ' k', ' M', ' G', ' T']
+ e_floor = math.floor(math.log10(value))
- div3 = int(math.floor(e_floor/3))
- suffix = big[div3]
- scale = 10**(div3*3)
- scaled = value/scale
+ div3 = int(math.floor(e_floor/3))
+ suffix = big[div3]
+ scale = 10**(div3*3)
+ scaled = value/scale
- if scaled == int(scaled):
- return "{:1.0f}{}".format(scaled, suffix)
- else:
- return "{:1.1f}{}".format(scaled, suffix)
+ if scaled == int(scaled):
+ return "{:1.0f}{}".format(scaled, suffix)
+ else:
+ return "{:1.1f}{}".format(scaled, suffix)
# return "scaled={:>9}, div3={}, floor(e)={}, scale={}, suffix={}".format(str(scaled), div3, e_floor, scale, suffix)
diff --git a/test/test_formatting.py b/test/test_formatting.py
new file mode 100644
index 0000000..560daee
--- /dev/null
+++ b/test/test_formatting.py
@@ -0,0 +1,38 @@
+import pytest
+from ee.formatting import eng_str
+
+@pytest.mark.parametrize("input,expected", [
+ (5.5, "5.5"),
+ (55, "55"),
+ (550, "550"),
+ (5500, "5.5 k"),
+ (55000, "55 k"),
+ (550000, "550 k"),
+ (5500000, "5.5 M"),
+ ])
+def test_eng_str(input, expected):
+ assert eng_str(input) == expected
+
+@pytest.mark.parametrize("input,expected", [
+ (100, ''), (101, ''), (102, ''), (103, ''), (104, ''), (105, ''), (106, ''), (107, ''), (108, ''), (109, ''),
+ (110, ''), (111, ''), (112, ''), (113, ''), (114, ''), (115, ''), (116, ''), (117, ''), (118, ''), (119, ''),
+ (120, ''), (121, ''), (122, ''), (123, ''), (124, ''), (125, ''), (126, ''), (127, ''), (128, ''), (129, ''),
+ (130, ''), (131, ''), (132, ''), (133, ''), (134, ''), (135, ''), (136, ''), (137, ''), (138, ''), (139, ''),
+ ])
+def xx_test_eng_str2(input, expected):
+ assert eng_str(input) == expected
+
+@pytest.mark.parametrize("input,expected", [
+ (10, '10'),
+ (11, '11'),
+ (12, '12'),
+ (13, '13'),
+ (14, '14'),
+ (15, '15'),
+ (16, '16'),
+ (17, '17'),
+ (18, '18'),
+ (19, '19'),
+ ])
+def xx_test_eng_str3(input, expected):
+ assert eng_str(input) == expected
diff --git a/test/tests.py b/test/tests.py
deleted file mode 100644
index a1c4ad7..0000000
--- a/test/tests.py
+++ /dev/null
@@ -1,48 +0,0 @@
-def test_formatting():
- import ee.formatting
- import sys
-# for x in sys.path:
-# print(x)
- print('pre')
- print(dir(ee.formatting))
- print('post')
- ee.formatting.eng_str(10)
- from ee.formatting import eng_str
-
- test_cases = [
- 10,
- 11,
- 12,
- 13,
- 14,
- 15,
- 16,
- 17,
- 18,
- 19,
- ]
- test_cases = [
- 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
- 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
- 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
- 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
- ]
- test_cases = [
- [5.5, "5.5"],
- [55, "55"],
- [550, "550"],
- [5500, "5.5 k"],
- [55000, "55 k"],
- [550000, "550 k"],
- [5500000, "5.5 M"],
- ]
-
- for [tc, expected] in test_cases:
- actual = eng_str(tc)
-# status = if expected == actual: 'PASS' else 'FAIL'
- status = '' if expected == actual else 'FAIL'
- print("{:5} {:10} => expected={:>10}, actual={:>10}".format(status, tc, expected, actual))
-
-# for tc in test_cases:
-# print("{:10} => e12={:10}".format(tc, e_series_find_closest(tc)))
-
diff --git a/tox.ini b/tox.ini
index 416365c..2fc2fe3 100644
--- a/tox.ini
+++ b/tox.ini
@@ -6,4 +6,5 @@ deps=
commands=
pytest \
--basetemp={envtmpdir} \
+ --maxfail=1000 \
{posargs}