Skip to content

Commit

Permalink
overload NullRegistry.__call__
Browse files Browse the repository at this point in the history
  • Loading branch information
getzze committed Oct 18, 2024
1 parent c938f0b commit e3c9a2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions knowit/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e3c9a2b

Please sign in to comment.