aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-22 08:23:51 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-22 14:14:55 +0200
commit1335bef6c3d0329678b2680dfc7435ad11db25df (patch)
tree7ae16609a30feb22b54b839fafc0e26e5620c22e
parentaeb51e89c1103836b2fd991a0ac789354465b55e (diff)
downloadee-python-1335bef6c3d0329678b2680dfc7435ad11db25df.tar.gz
ee-python-1335bef6c3d0329678b2680dfc7435ad11db25df.tar.bz2
ee-python-1335bef6c3d0329678b2680dfc7435ad11db25df.tar.xz
ee-python-1335bef6c3d0329678b2680dfc7435ad11db25df.zip
o EeVal: implememting unit parsing too.
-rw-r--r--src/ee/__init__.py4
-rw-r--r--test/test_formatting.py11
2 files changed, 13 insertions, 2 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index b4da410..e48fa6a 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -43,7 +43,6 @@ 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 eng_str(self.__float__(), self._unit)
def __float__(self):
@@ -66,7 +65,8 @@ EeVal.exponents = {
'E': 18,
}
EeVal.units = ['F', 'Ohm', '\u2126', 'H']
-EeVal.r = re.compile("([0-9]+) *([" + "".join(EeVal.exponents.keys()) + "]?)(" + "|".join(EeVal.units) + "?)")
+EeVal.r = re.compile("([0-9]+) *([" + "".join(EeVal.exponents.keys()) + "]?) *(" + "|".join(EeVal.units) + "?)")
+print("EeVal.r={}".format(EeVal.r))
class LtSpiceRaw(object):
diff --git a/test/test_formatting.py b/test/test_formatting.py
index 88fb8ed..6687e96 100644
--- a/test/test_formatting.py
+++ b/test/test_formatting.py
@@ -87,3 +87,14 @@ def test_EeVal(s, num_value, str_value, unit):
assert (num_value + epsilon) >= float(v) and (num_value - epsilon) <= float(v)
assert str_value == str(v)
assert unit == v.unit
+
+@pytest.mark.parametrize("s, str_value, unit", [
+ ("10nF", "10 nF", "F"),
+ ("10n F", "10 nF", "F"),
+ ("10 n F", "10 nF", "F"),
+ ])
+def test_EeVal_with_units(s, str_value, unit):
+ v = EeVal(s)
+ assert unit == v.unit
+ assert float(v) == v.value
+ assert str_value == str(v)