summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHernan <hernan.grecco@gmail.com>2022-05-01 12:53:53 -0300
committerHernan <hernan.grecco@gmail.com>2022-05-01 12:53:53 -0300
commitb884f56db219b42060f2c2f1d2d66911802282b8 (patch)
treedc020195931864fab943cb4009d95b167f890b74
parent6f5ff5a34f8f5fa8db1c7d87d8e8e80ac53a56ef (diff)
downloadpint-b884f56db219b42060f2c2f1d2d66911802282b8.tar.gz
Use for System and Group the same mechanism to build class as for Quantity, Unit and Measurement
-rw-r--r--pint/facets/group/objects.py10
-rw-r--r--pint/facets/group/registry.py12
-rw-r--r--pint/facets/measurement/objects.py5
-rw-r--r--pint/facets/nonmultiplicative/registry.py2
-rw-r--r--pint/facets/plain/registry.py5
-rw-r--r--pint/facets/system/objects.py10
-rw-r--r--pint/facets/system/registry.py17
7 files changed, 24 insertions, 37 deletions
diff --git a/pint/facets/group/objects.py b/pint/facets/group/objects.py
index d54fc25..4ff775c 100644
--- a/pint/facets/group/objects.py
+++ b/pint/facets/group/objects.py
@@ -190,13 +190,3 @@ class Group(SharedRegistryObject):
def __getattr__(self, item):
getattr_maybe_raise(self, item)
return self._REGISTRY
-
-
-_Group = Group
-
-
-def build_group_class(registry):
- class Group(_Group):
- _REGISTRY = registry
-
- return Group
diff --git a/pint/facets/group/registry.py b/pint/facets/group/registry.py
index 0268ad7..1ec0799 100644
--- a/pint/facets/group/registry.py
+++ b/pint/facets/group/registry.py
@@ -13,9 +13,10 @@ from typing import TYPE_CHECKING, Dict, FrozenSet
if TYPE_CHECKING:
from pint import Unit
+from ...util import build_dependent_class, create_class_with_registry
from ..plain.definitions import UnitDefinition
from .definitions import GroupDefinition
-from .objects import Group, build_group_class
+from .objects import Group
class GroupRegistry:
@@ -28,6 +29,8 @@ class GroupRegistry:
- Parse @group directive.
"""
+ _group_class = Group
+
def __init__(self, **kwargs):
super().__init__(**kwargs)
#: Map group name to group.
@@ -35,9 +38,14 @@ class GroupRegistry:
self._groups: Dict[str, Group] = {}
self._groups["root"] = self.Group("root")
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__()
+ cls.Group = build_dependent_class(cls, "Group", "_group_class")
+
def _init_dynamic_classes(self) -> None:
+ """Generate subclasses on the fly and attach them to self"""
super()._init_dynamic_classes()
- self.Group = build_group_class(self)
+ self.Group = create_class_with_registry(self, self.Group)
def _after_init(self) -> None:
"""Invoked at the end of ``__init__``.
diff --git a/pint/facets/measurement/objects.py b/pint/facets/measurement/objects.py
index 7817bdf..88fad0a 100644
--- a/pint/facets/measurement/objects.py
+++ b/pint/facets/measurement/objects.py
@@ -185,8 +185,3 @@ class Measurement(PlainQuantity):
mag = re.sub(r"\)e-0?(\d+)", r")×10<sup>-\1</sup>", mag)
return mag + space + ustr
-
-
-# TODO: Remove in the near future
-# This is kept for easy backward compatibility during refactoring.
-_Measurement = Measurement
diff --git a/pint/facets/nonmultiplicative/registry.py b/pint/facets/nonmultiplicative/registry.py
index 0f2e30c..75d4fcd 100644
--- a/pint/facets/nonmultiplicative/registry.py
+++ b/pint/facets/nonmultiplicative/registry.py
@@ -15,8 +15,6 @@ from ...errors import DimensionalityError, UndefinedUnitError
from ...util import UnitsContainer
from .objects import NonMultiplicativeQuantity
-# TODO: Must sublcass plain Registry when plain fect is built.
-
class NonMultiplicativeRegistry:
"""Handle of non multiplicative units (e.g. Temperature).
diff --git a/pint/facets/plain/registry.py b/pint/facets/plain/registry.py
index e235a60..eeae8d0 100644
--- a/pint/facets/plain/registry.py
+++ b/pint/facets/plain/registry.py
@@ -1348,8 +1348,3 @@ class PlainRegistry(metaclass=RegistryMeta):
)
__call__ = parse_expression
-
-
-# TODO: Remove in the near future
-# This is kept for easy backward compatibility during refactoring.
-BaseRegistry = PlainRegistry
diff --git a/pint/facets/system/objects.py b/pint/facets/system/objects.py
index aaeabd3..b81dfc5 100644
--- a/pint/facets/system/objects.py
+++ b/pint/facets/system/objects.py
@@ -180,13 +180,3 @@ class Lister:
def __getattr__(self, item):
getattr_maybe_raise(self, item)
return self.d[item]
-
-
-_System = System
-
-
-def build_system_class(registry):
- class System(_System):
- _REGISTRY = registry
-
- return System
diff --git a/pint/facets/system/registry.py b/pint/facets/system/registry.py
index 28a9397..ca60766 100644
--- a/pint/facets/system/registry.py
+++ b/pint/facets/system/registry.py
@@ -16,10 +16,14 @@ if TYPE_CHECKING:
from ..._typing import UnitLike
from ...util import UnitsContainer as UnitsContainerT
-from ...util import to_units_container
+from ...util import (
+ build_dependent_class,
+ create_class_with_registry,
+ to_units_container,
+)
from ..group import GroupRegistry
from .definitions import SystemDefinition
-from .objects import Lister, System, build_system_class
+from .objects import Lister, System
class SystemRegistry(GroupRegistry):
@@ -37,6 +41,8 @@ class SystemRegistry(GroupRegistry):
- Parse @group directive.
"""
+ _system_class = System
+
def __init__(self, system=None, **kwargs):
super().__init__(**kwargs)
@@ -49,9 +55,14 @@ class SystemRegistry(GroupRegistry):
self._default_system = system
+ def __init_subclass__(cls, **kwargs):
+ super().__init_subclass__()
+ cls.System = build_dependent_class(cls, "System", "_system_class")
+
def _init_dynamic_classes(self) -> None:
+ """Generate subclasses on the fly and attach them to self"""
super()._init_dynamic_classes()
- self.System = build_system_class(self)
+ self.System = create_class_with_registry(self, self.System)
def _after_init(self) -> None:
"""Invoked at the end of ``__init__``.