diff --git a/knowit/units.py b/knowit/units.py index c8421ca..684658b 100644 --- a/knowit/units.py +++ b/knowit/units.py @@ -14,6 +14,17 @@ def __getattr__(self, item: typing.Any) -> int: """Return a Scalar 1 to simulate a unit.""" return 1 + def __call__(self, value: str) -> float: + """Try converting to int, to float and fallback to a scalar 1.0.""" + try: + return int(value) + except ValueError: + try: + return float(value) + except ValueError: + pass + return 1 + def __bool__(self): """Return False since a NullRegistry is not a pint.UnitRegistry.""" return False diff --git a/tests/__init__.py b/tests/__init__.py index 20e0490..5c91d3b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -250,7 +250,7 @@ def parse_quantity(value): if isinstance(value, str): for unit in ('pixel', 'bit', 'byte', 'FPS', 'bps', 'Hz'): if value.endswith(' ' + unit): - return units(value[:-(len(unit))] + ' * ' + unit) + return units(value[:-len(unit)]) * units(unit) return value