bazarr/libs/knowit/units.py

34 lines
748 B
Python
Raw Normal View History

import typing
2020-03-18 19:33:54 +00:00
try:
import pint
except ImportError:
pint = False
2020-03-18 19:33:54 +00:00
class NullRegistry:
"""A NullRegistry that masquerades as a pint.UnitRegistry."""
def __init__(self):
"""Initialize a null registry."""
2020-03-18 19:33:54 +00:00
def __getattr__(self, item: typing.Any) -> int:
"""Return a Scalar 1 to simulate a unit."""
return 1
2020-03-18 19:33:54 +00:00
def __bool__(self):
"""Return False since a NullRegistry is not a pint.UnitRegistry."""
return False
2020-03-18 19:33:54 +00:00
def define(self, *args, **kwargs):
"""Pretend to add unit to the registry."""
2020-03-18 19:33:54 +00:00
def _build_unit_registry():
registry = pint.UnitRegistry() if pint else NullRegistry()
registry.define('FPS = 1 * hertz')
2020-03-18 19:33:54 +00:00
return registry
units = _build_unit_registry()