aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-09 21:26:17 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-09 21:26:17 +0200
commit542ec502c9b9619c005bc73e562229e4547ab90b (patch)
tree037e6fdaccdbfb373a91d971ff6fa8de02fe504f
parent6887e773e17beb5a6c2df0c7397d5223eebde37e (diff)
downloadee-python-542ec502c9b9619c005bc73e562229e4547ab90b.tar.gz
ee-python-542ec502c9b9619c005bc73e562229e4547ab90b.tar.bz2
ee-python-542ec502c9b9619c005bc73e562229e4547ab90b.tar.xz
ee-python-542ec502c9b9619c005bc73e562229e4547ab90b.zip
o Imporing some old parsing code into a EeVal type. Not quite done yet.
-rw-r--r--src/ee/__init__.py57
-rw-r--r--src/ee/formatting/__init__.py1
-rw-r--r--test/test_formatting.py14
3 files changed, 70 insertions, 2 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index b1b825b..bb45ce6 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -1,13 +1,66 @@
import numpy as np
+import re
+from functools import total_ordering
__all__ = [
- 'EeException',
- 'read_ltspice_raw'
+ "EeException",
+ "EeVal",
+ "read_ltspice_raw"
]
class EeException(Exception):
pass
+@total_ordering
+class EeVal(object):
+ from decimal import Decimal
+
+ def __init__(self, s):
+ m = EeVal.r.match(s)
+ if not m:
+ 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]
+
+ @property
+ def value(self):
+ return self._value
+
+ @property
+ def unit(self):
+ return self._exp
+
+ def __eq__(self, other):
+ return ((self._value, self._unit) == (other._value, other._unit))
+
+ def __lt__(self, other):
+ return ((self._value, self._unit) < (other._value, other._unit))
+
+ def __str__(self):
+ return str(self._value) + (" " + str(self._unit) if self._unit else "")
+
+
+EeVal.exponents = {
+ 'f': -12,
+ 'p': -9,
+ 'n': -6,
+ 'u': -6,
+ '\u00B5': -6, # The micro symbol
+ 'm': -3,
+
+ 'k': 3,
+ 'M': 6,
+ 'G': 9,
+ 'T': 12,
+ 'P': 15,
+ 'E': 18,
+}
+EeVal.units = ['F', 'Ohm', '\u2126', 'H']
+EeVal.r = re.compile("([0-9]+) *([" + "".join(EeVal.exponents.keys()) + "]?)(" + "|".join(EeVal.units) + "?)")
+
class LtSpiceRaw(object):
def __init__(self, variables, values_first, values_rest):
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py
index 33673a1..c2b5688 100644
--- a/src/ee/formatting/__init__.py
+++ b/src/ee/formatting/__init__.py
@@ -1,4 +1,5 @@
import math
+import re
__all__ = [
'e6', 'e12', 'e24', 'e48', 'e96', 'e192',
diff --git a/test/test_formatting.py b/test/test_formatting.py
index 18f582c..177912d 100644
--- a/test/test_formatting.py
+++ b/test/test_formatting.py
@@ -1,6 +1,7 @@
import pytest
import numpy as np
from ee.formatting import eng_str
+from ee import EeVal
@pytest.mark.parametrize("input,expected", [
@@ -60,3 +61,16 @@ def xx_test_eng_str2(input, expected):
])
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")),
+ ])
+def test_EeVal(s, expected):
+ assert expected == EeVal(s)