2022-01-24 12:07:52 +08:00
|
|
|
import typing
|
2020-03-19 03:33:54 +08:00
|
|
|
|
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
class NullRegistry:
|
|
|
|
"""A NullRegistry that masquerades as a pint.UnitRegistry."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize a null registry."""
|
2020-03-19 03:33:54 +08:00
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
def __getattr__(self, item: typing.Any) -> int:
|
|
|
|
"""Return a Scalar 1 to simulate a unit."""
|
|
|
|
return 1
|
2020-03-19 03:33:54 +08:00
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
def __bool__(self):
|
|
|
|
"""Return False since a NullRegistry is not a pint.UnitRegistry."""
|
|
|
|
return False
|
2020-03-19 03:33:54 +08:00
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
def define(self, *args, **kwargs):
|
|
|
|
"""Pretend to add unit to the registry."""
|
2020-03-19 03:33:54 +08:00
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
|
|
|
|
def _build_unit_registry():
|
2023-03-22 11:15:01 +08:00
|
|
|
try:
|
|
|
|
import pint
|
|
|
|
|
|
|
|
registry = pint.UnitRegistry()
|
|
|
|
registry.define('FPS = 1 * hertz')
|
|
|
|
|
|
|
|
pint.set_application_registry(registry)
|
|
|
|
return registry
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return NullRegistry()
|
2020-03-19 03:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
units = _build_unit_registry()
|