From d926787965e74290b0ffe30d7443671cfbc06521 Mon Sep 17 00:00:00 2001 From: Maximos Nikiforakis Date: Fri, 16 Feb 2024 08:06:57 +0200 Subject: [PATCH] Introduce aliased_generic_descriptor method in AliasMeasurementUnit --- src/property_utils/units/descriptors.py | 17 +++++++++++++++++ src/property_utils/units/units.py | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/property_utils/units/descriptors.py b/src/property_utils/units/descriptors.py index 90f9583..82bb338 100644 --- a/src/property_utils/units/descriptors.py +++ b/src/property_utils/units/descriptors.py @@ -326,6 +326,9 @@ def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit: it is intended to be used to convert an unknown unit descriptor to an AliasMeasurementUnit. + Subclasses should implement aliased_generic_descriptor and alias_mapping + methods. + Raises WrongUnitDescriptorType if given descriptor cannot be translated to an AliasMeasurementUnit instance. @@ -345,6 +348,20 @@ def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit: f"cannot create AliasMeasurementUnit from descriptor {descriptor}" ) + @classmethod + def aliased_generic_descriptor(cls) -> GenericUnitDescriptor: + """ + Implement this method by returning the generic of the unit descriptor that this + measurement unit aliases. + + >>> class LengthUnit(MeasurementUnit): ... + >>> class AreaUnit(AliasMeasurementUnit): + ... @classmethod + ... def aliased_generic_descriptor(cls): + ... return LengthUnit**2 + """ + raise NotImplementedError + @dataclass class GenericDimension: diff --git a/src/property_utils/units/units.py b/src/property_utils/units/units.py index 893219c..d968a13 100644 --- a/src/property_utils/units/units.py +++ b/src/property_utils/units/units.py @@ -1,6 +1,8 @@ from property_utils.units.descriptors import ( MeasurementUnit, AliasMeasurementUnit, + GenericCompositeDimension, + UnitDescriptor, ) @@ -57,6 +59,10 @@ class PressureUnit(AliasMeasurementUnit): PASCAL = "Pa" KILO_PASCAL = "kPa" + @classmethod + def aliased_generic_descriptor(cls) -> GenericCompositeDimension: + return MassUnit / LengthUnit / (TimeUnit**2) + class EnergyUnit(AliasMeasurementUnit): JOULE = "J" @@ -66,3 +72,7 @@ class EnergyUnit(AliasMeasurementUnit): CALORIE = "cal" KILO_CALORIE = "kcal" BTU = "Btu" + + @classmethod + def aliased_generic_descriptor(cls) -> GenericCompositeDimension: + return MassUnit * (LengthUnit**2) / (TimeUnit**2)