| 1 |
""" |
| 2 |
pint.facets.measurement.registry |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: 2022 by Pint Authors, see AUTHORS for more details. |
| 6 |
:license: BSD, see LICENSE for more details. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
from typing import Generic |
| 12 |
|
| 13 |
from ...compat import TypeAlias, ufloat |
| 14 |
from ...util import create_class_with_registry |
| 15 |
from ..plain import GenericPlainRegistry, QuantityT, UnitT |
| 16 |
from . import objects |
| 17 |
|
| 18 |
|
| 19 |
class GenericMeasurementRegistry( |
| 20 |
Generic[QuantityT, UnitT], GenericPlainRegistry[QuantityT, UnitT] |
| 21 |
): |
| 22 |
Measurement = objects.Measurement |
| 23 |
|
| 24 |
def _init_dynamic_classes(self) -> None: |
| 25 |
"""Generate subclasses on the fly and attach them to self""" |
| 26 |
super()._init_dynamic_classes() |
| 27 |
|
| 28 |
if ufloat is not None: |
| 29 |
self.Measurement = create_class_with_registry(self, self.Measurement) |
| 30 |
else: |
| 31 |
|
| 32 |
def no_uncertainties(*args, **kwargs): |
| 33 |
raise RuntimeError( |
| 34 |
"Pint requires the 'uncertainties' package to create a Measurement object." |
| 35 |
) |
| 36 |
|
| 37 |
self.Measurement = no_uncertainties |
| 38 |
|
| 39 |
|
| 40 |
class MeasurementRegistry( |
| 41 |
GenericMeasurementRegistry[objects.MeasurementQuantity, objects.MeasurementUnit] |
| 42 |
): |
| 43 |
Quantity: TypeAlias = objects.MeasurementQuantity |
| 44 |
Unit: TypeAlias = objects.MeasurementUnit |