aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-22 15:33:34 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-22 16:37:16 +0200
commita7a5c583d295c70f63898a005bd00b7d84412478 (patch)
tree640822746e44ff90e895c052c4df2a6dfc44608c /src
parent5c0c106c9fbdc9e5e189c8ba6903bbf70d803aba (diff)
downloadee-python-a7a5c583d295c70f63898a005bd00b7d84412478.tar.gz
ee-python-a7a5c583d295c70f63898a005bd00b7d84412478.tar.bz2
ee-python-a7a5c583d295c70f63898a005bd00b7d84412478.tar.xz
ee-python-a7a5c583d295c70f63898a005bd00b7d84412478.zip
o Fixing parsing of '0.1' values too.
Diffstat (limited to 'src')
-rw-r--r--src/ee/__init__.py47
-rw-r--r--src/ee/formatting/__init__.py6
2 files changed, 26 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