aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ee/__init__.py25
-rw-r--r--src/ee/formatting/__init__.py2
-rw-r--r--test/test_formatting.py35
3 files changed, 41 insertions, 21 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index bb45ce6..b4da410 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -1,6 +1,8 @@
+import math
import numpy as np
import re
from functools import total_ordering
+from ee.formatting import eng_str
__all__ = [
"EeException",
@@ -21,17 +23,18 @@ class EeVal(object):
raise EeException("Could not parse value: " + str(s))
gs = m.groups()
exp = gs[1].strip()
- self._value = gs[0]
- self._exp = EeVal.exponents.get(gs[1], 0)
- self._unit = gs[2]
+ unit = gs[2]
+ self._value = int(gs[0])
+ self._exp = EeVal.exponents.get(exp, 0)
+ self._unit = unit if len(unit) > 0 else None
@property
def value(self):
- return self._value
+ return self.__float__()
@property
def unit(self):
- return self._exp
+ return self._unit
def __eq__(self, other):
return ((self._value, self._unit) == (other._value, other._unit))
@@ -40,13 +43,17 @@ class EeVal(object):
return ((self._value, self._unit) < (other._value, other._unit))
def __str__(self):
- return str(self._value) + (" " + str(self._unit) if self._unit else "")
+# return str(self._value) + (" " + str(self._unit) if self._unit else "")
+ return eng_str(self.__float__(), self._unit)
+
+ def __float__(self):
+ return self._value * math.pow(10, self._exp)
EeVal.exponents = {
- 'f': -12,
- 'p': -9,
- 'n': -6,
+ 'f': -15,
+ 'p': -12,
+ 'n': -9,
'u': -6,
'\u00B5': -6, # The micro symbol
'm': -3,
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py
index c2b5688..4527a96 100644
--- a/src/ee/formatting/__init__.py
+++ b/src/ee/formatting/__init__.py
@@ -72,7 +72,7 @@ def eng_str(value, unit=None):
s = '0' + (' ' + unit if unit is not None else '')
else:
big = ['k', 'M', 'G', 'T']
- small = ['m', 'u', 'p']
+ small = ['m', 'u', 'n', 'p']
if unit is not None:
big = [' ' + unit] + [' ' + x + unit for x in big]
diff --git a/test/test_formatting.py b/test/test_formatting.py
index 177912d..88fb8ed 100644
--- a/test/test_formatting.py
+++ b/test/test_formatting.py
@@ -19,7 +19,7 @@ from ee import EeVal
(0.00055, "550 u"),
(0.000055, "55 u"),
(0.0000055, "5.5 u"),
- (0.00000055, "550 p"),
+ (0.00000055, "550 n"),
])
def test_eng_str(input, expected):
assert expected == eng_str(input)
@@ -63,14 +63,27 @@ def xx_test_eng_str3(input, expected):
assert expected == eng_str(input)
-@pytest.mark.parametrize("s, expected", [
- ('1', EeVal("1")),
- ('10', EeVal("10")),
- ('10k', EeVal("10e3")),
- ('10 k', EeVal("10e3")),
- ('10 k', EeVal("10e3")),
- ('1p', EeVal("1e-9")),
- ('100n', EeVal("100e-6")),
+@pytest.mark.parametrize("s, num_value, str_value, unit", [
+ ("1", 1, "1", None),
+ ("10", 10, "10", None),
+ ("10k", 10 * 1000, "10 k", None),
+ ("10 k", 10 * 1000, "10 k", None),
+ ("10 k", 10 * 1000, "10 k", None),
+ ("1000 m", 1, "1", None),
+ ("100 m", 0.1, "100 m", None),
+ ("100 n", 0.0000001, "100 n", None),
+ ("1 m", 0.001, "1 m", None),
+ ("1 u", 0.000001, "1 u", None),
+ ("1 µ", 0.000001, "1 u", None),
+ ("1 n", 0.000000001, "1 n", None),
+ ("10 n", 0.00000001, "10 n", None),
+ ("1p", 1e-12, "1 p", None),
])
-def test_EeVal(s, expected):
- assert expected == EeVal(s)
+def test_EeVal(s, num_value, str_value, unit):
+ num_value = float(num_value)
+ v = EeVal(s)
+ assert float(v) == v.value
+ epsilon = num_value * 0.01
+ assert (num_value + epsilon) >= float(v) and (num_value - epsilon) <= float(v)
+ assert str_value == str(v)
+ assert unit == v.unit