aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ee/__init__.py29
-rw-r--r--src/ee/_utils.py17
-rw-r--r--test/test_EeVal.py1
3 files changed, 41 insertions, 6 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index 2fad46d..7f228f8 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -18,7 +18,9 @@ class EeException(Exception):
@total_ordering
class EeVal(object):
from decimal import Decimal
- units = ['F', 'Ohm', '\u2126', 'H']
+ units = ['F',
+ 'Ohm', '\u2126', # Ohm symbol
+ 'H']
exponents = {
'f': -15,
'p': -12,
@@ -37,7 +39,18 @@ class EeVal(object):
r = re.compile(
"([0-9]+\\.[0-9]+|\\.?[0-9]+|[0-9]+\\.?) *([" + "".join(exponents.keys()) + "]?) *(" + "|".join(units) + "?)")
- def __init__(self, s):
+ def __init__(self, s, value=None, exp=None, unit=None):
+ # This is where I regret having a string in the API at once.
+ if s:
+ val = EeVal.parse(s)
+ (self._value, self._exp, self._unit) = (val._value, val._exp, val._unit)
+ else:
+ if value is None or exp is None:
+ raise EeException("Bad arguments, value and exp has to be set.")
+ (self._value, self._exp, self._unit) = (value, exp, unit)
+
+ @staticmethod
+ def parse(s) -> "EeVal":
m = EeVal.r.match(s)
if not m:
raise EeException("Could not parse value: " + str(s))
@@ -50,9 +63,7 @@ class EeVal(object):
e = math.ceil(math.log10(value))
exp = exp + e
value = value * math.pow(10, -e)
- self._value = float(value)
- self._exp = exp
- self._unit = unit if len(unit) > 0 else None
+ return EeVal(None, value = float(value), exp = exp, unit = unit if len(unit) > 0 else None)
@property
def value(self):
@@ -62,11 +73,17 @@ class EeVal(object):
def unit(self):
return self._unit
+ def set(self, value=None, exp=None, unit=None):
+ return EeVal(None,
+ value=value if value else self._value,
+ exp=exp if exp else self._exp,
+ unit=unit if unit else self._unit)
+
def __hash__(self):
return hash((self.__float__(), self._unit))
def __eq__(self, other):
- return ((self.__float__(), self._unit) == (other.__float__(), other._unit))
+ return math.isclose(self.__float__(), other.__float__()) and self._unit == other._unit
def __lt__(self, other):
return ((self.__float__(), self._unit) < (other.__float__(), other._unit))
diff --git a/src/ee/_utils.py b/src/ee/_utils.py
index 9df6126..0551b89 100644
--- a/src/ee/_utils.py
+++ b/src/ee/_utils.py
@@ -16,3 +16,20 @@ def run_filters(filters, obj):
if not f(obj):
return False
return True
+
+def any(filters):
+ def f(obj):
+ for f in filters:
+ if f(obj):
+ return True
+ return False
+ return f
+
+
+def all(filters):
+ def f(obj):
+ for f in filters:
+ if not f(obj):
+ return False
+ return True
+ return f
diff --git a/test/test_EeVal.py b/test/test_EeVal.py
index 6e15ff0..840be30 100644
--- a/test/test_EeVal.py
+++ b/test/test_EeVal.py
@@ -35,6 +35,7 @@ def test_basic(s, num_value, str_value, unit):
@pytest.mark.parametrize("s, str_value, unit", [
("10nF", "10 nF", "F"),
+ ("10 nF", "10 nF", "F"),
("10n F", "10 nF", "F"),
("10 n F", "10 nF", "F"),
("4.7 n F", "4.7 nF", "F"),