diff options
Diffstat (limited to 'src/ee/__init__.py')
-rw-r--r-- | src/ee/__init__.py | 121 |
1 files changed, 62 insertions, 59 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py index d3885c3..7fa2243 100644 --- a/src/ee/__init__.py +++ b/src/ee/__init__.py @@ -10,69 +10,72 @@ __all__ = [ "read_ltspice_raw" ] + class EeException(Exception): pass + @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] - 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 - def value(self): - return self.__float__() - - @property - def unit(self): - return self._unit - - def __hash__(self): - return hash((self.__float__(), self._unit)) - - def __eq__(self, other): - return ((self.__float__(), self._unit) == (other.__float__(), other._unit)) - - def __lt__(self, other): - return ((self.__float__(), self._unit) < (other.__float__(), other._unit)) - - def __str__(self): - return eng_str(self.__float__(), self._unit) - - def __float__(self): - return self._value * math.pow(10, self._exp) + 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] + 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 + def value(self): + return self.__float__() + + @property + def unit(self): + return self._unit + + def __hash__(self): + return hash((self.__float__(), self._unit)) + + def __eq__(self, other): + return ((self.__float__(), self._unit) == (other.__float__(), other._unit)) + + def __lt__(self, other): + return ((self.__float__(), self._unit) < (other.__float__(), other._unit)) + + def __str__(self): + return eng_str(self.__float__(), self._unit) + + def __float__(self): + return self._value * math.pow(10, self._exp) class LtSpiceRaw(object): @@ -187,7 +190,7 @@ class LtSpiceReader(object): if line[-1] == '\n' or line[-1] == 0x0a: line = line[:-1] - # print("len(line)={}, line={}".format(len(line), str(line))) + # print("len(line)={}, line={}".format(len(line), str(line))) line = line.decode(encoding="utf-16") if self.mode == 'header': |