summaryrefslogtreecommitdiffhomepage
path: root/libs/knowit/units.py
blob: 51e6cae73dc0415f57a0b4e3490d15cfe69eb8ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import typing


class NullRegistry:
    """A NullRegistry that masquerades as a pint.UnitRegistry."""

    def __init__(self):
        """Initialize a null registry."""

    def __getattr__(self, item: typing.Any) -> int:
        """Return a Scalar 1 to simulate a unit."""
        return 1

    def __bool__(self):
        """Return False since a NullRegistry is not a pint.UnitRegistry."""
        return False

    def define(self, *args, **kwargs):
        """Pretend to add unit to the registry."""


def _build_unit_registry():
    try:
        import pint

        registry = pint.UnitRegistry()
        registry.define('FPS = 1 * hertz')

        pint.set_application_registry(registry)
        return registry
    except ModuleNotFoundError:
        pass

    return NullRegistry()


units = _build_unit_registry()