aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ee/__init__.py47
-rw-r--r--src/ee/formatting/__init__.py6
-rw-r--r--test/test_formatting.py4
3 files changed, 30 insertions, 27 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index e48fa6a..0450c40 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -16,16 +16,39 @@ class EeException(Exception):
@total_ordering
class EeVal(object):
from decimal import Decimal
+ units = ['F', 'Ohm', '\u2126', 'H']
+ exponents = {
+ 'f': -15,
+ 'p': -12,
+ 'n': -9,
+ 'u': -6,
+ '\u00B5': -6, # The micro symbol
+ 'm': -3,
+
+ 'k': 3,
+ 'M': 6,
+ 'G': 9,
+ 'T': 12,
+ 'P': 15,
+ 'E': 18,
+ }
+ r = re.compile("([0-9]+\\.[0-9]+|\\.?[0-9]+|[0-9]+\\.?) *([" + "".join(exponents.keys()) + "]?) *(" + "|".join(units) + "?)")
def __init__(self, s):
m = EeVal.r.match(s)
if not m:
raise EeException("Could not parse value: " + str(s))
gs = m.groups()
+ value = float(gs[0])
exp = gs[1].strip()
+ exp = EeVal.exponents.get(exp) if len(exp) > 0 else 0
unit = gs[2]
- self._value = int(gs[0])
- self._exp = EeVal.exponents.get(exp, 0)
+ if value < 1:
+ 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
@property
@@ -49,26 +72,6 @@ class EeVal(object):
return self._value * math.pow(10, self._exp)
-EeVal.exponents = {
- 'f': -15,
- 'p': -12,
- 'n': -9,
- '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) + "?)")
-print("EeVal.r={}".format(EeVal.r))
-
-
class LtSpiceRaw(object):
def __init__(self, variables, values_first, values_rest):
self.variables = variables
diff --git a/src/ee/formatting/__init__.py b/src/ee/formatting/__init__.py
index 4527a96..3fa82d2 100644
--- a/src/ee/formatting/__init__.py
+++ b/src/ee/formatting/__init__.py
@@ -95,10 +95,6 @@ def eng_str(value, unit=None):
scale = 10 ** (div3 * 3)
scaled = value / scale
- if (scaled - math.floor(scaled)) < 0.0001:
- # if scaled == math.floor(scaled):
- s = "{:1.0f}{}".format(scaled, suffix)
- else:
- s = "{:1.1f}{}".format(scaled, suffix)
+ s = "{:1.3g}{}".format(scaled, suffix)
return s
diff --git a/test/test_formatting.py b/test/test_formatting.py
index 6687e96..642b3de 100644
--- a/test/test_formatting.py
+++ b/test/test_formatting.py
@@ -74,6 +74,9 @@ def xx_test_eng_str3(input, expected):
("100 n", 0.0000001, "100 n", None),
("1 m", 0.001, "1 m", None),
("1 u", 0.000001, "1 u", None),
+ ("0.1 u", 0.0000001, "100 n", None),
+ (".1 u", 0.0000001, "100 n", None),
+ ("4.7 u", 0.0000047, "4.7 u", None),
("1 µ", 0.000001, "1 u", None),
("1 n", 0.000000001, "1 n", None),
("10 n", 0.00000001, "10 n", None),
@@ -92,6 +95,7 @@ def test_EeVal(s, num_value, str_value, unit):
("10nF", "10 nF", "F"),
("10n F", "10 nF", "F"),
("10 n F", "10 nF", "F"),
+# ("4.7 n F", "4.7 uF", "F"),
])
def test_EeVal_with_units(s, str_value, unit):
v = EeVal(s)