From a7a5c583d295c70f63898a005bd00b7d84412478 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 22 Sep 2017 15:33:34 +0200 Subject: o Fixing parsing of '0.1' values too. --- src/ee/__init__.py | 47 +++++++++++++++++++++++-------------------- src/ee/formatting/__init__.py | 6 +----- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src') 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 -- cgit v1.2.3