diff --git a/NanoVNASaver/SITools.py b/NanoVNASaver/SITools.py index aab140d..12e86d4 100644 --- a/NanoVNASaver/SITools.py +++ b/NanoVNASaver/SITools.py @@ -33,6 +33,8 @@ class Format(NamedTuple): allow_strip: bool = False parse_sloppy_unit: bool = False parse_sloppy_kilo: bool = False + parse_clamp_min: float = -math.inf + parse_clamp_max: float = math.inf class Value: @@ -66,7 +68,7 @@ class Value: elif offset > fmt.max_offset: offset = fmt.max_offset - real = self._value / (10 ** (offset * 3)) + real = float(self._value) / (10 ** (offset * 3)) if fmt.max_nr_digits < 4: formstr = ".0f" @@ -111,7 +113,17 @@ class Value: elif self.fmt.assume_infinity and value == "-\N{INFINITY}": self._value = -math.inf else: - self._value = decimal.Decimal(value, context=Value.CTX) * factor + try: + self._value = (decimal.Decimal(value, context=Value.CTX) * + decimal.Decimal(factor, context=Value.CTX)) + except decimal.InvalidOperation: + raise ValueError + # TODO: get formating out of RFTools to be able to import clamp + # and reuse code + if self._value < self.fmt.parse_clamp_min: + self._value = self.fmt.parse_clamp_min + elif self._value > self.fmt.parse_clamp_max: + self._value = self.fmt.parse_clamp_max return float(self._value) @property diff --git a/test/test_parseFrequency.py b/test/test_parseFrequency.py index 8a2e631..860b20b 100644 --- a/test/test_parseFrequency.py +++ b/test/test_parseFrequency.py @@ -1,4 +1,5 @@ -# NanoVNASaver - a python program to view and export Touchstone data from a NanoVNA +# NanoVNASaver +# A python program to view and export Touchstone data from a NanoVNA # Copyright (C) 2019. Rune B. Broberg # # This program is free software: you can redistribute it and/or modify @@ -14,13 +15,20 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys import unittest # Import targets to be tested -from NanoVNASaver import RFTools -rft = RFTools.RFTools() +from NanoVNASaver.RFTools import RFTools +rft = RFTools() + +# TODO: should be tested against SITools.Value +# RFTools.parseFrequency will hopefully go away in future +# and be specialised by input field and device, like +# parse_clamp_min=50000 for sweep input with +# a nanovna version 1 attached ... +# the hardware developer already announced a successor +# which will have different limits class TestCases(unittest.TestCase): def test_basicSIUnits(self): @@ -58,7 +66,7 @@ class TestCases(unittest.TestCase): # Current behavior: unusual SI values that are legal, but inappropriate # for this application provide unexpected outputs. This behavior is # based on the FULL set of SI prefixes defined in SITools (below). - #PREFIXES = ("y", "z", "a", "f", "p", "n", "µ", "m", + # PREFIXES = ("y", "z", "a", "f", "p", "n", "µ", "m", # "", "k", "M", "G", "T", "P", "E", "Z", "Y") ####################################################################### self.assertEqual(rft.parseFrequency('123EHz'), 123000000000000000000)