From 542ec502c9b9619c005bc73e562229e4547ab90b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 9 Sep 2017 21:26:17 +0200 Subject: o Imporing some old parsing code into a EeVal type. Not quite done yet. --- src/ee/__init__.py | 57 +++++++++++++++++++++++++++++++++++++++++-- src/ee/formatting/__init__.py | 1 + 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'src') 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', -- cgit v1.2.3