Skip to content

Commit

Permalink
Introduce aliased_generic_descriptor method in AliasMeasurementUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Feb 16, 2024
1 parent a13d0f9 commit d926787
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/property_utils/units/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/property_utils/units/units.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from property_utils.units.descriptors import (
MeasurementUnit,
AliasMeasurementUnit,
GenericCompositeDimension,
UnitDescriptor,
)


Expand Down Expand Up @@ -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"
Expand All @@ -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)

0 comments on commit d926787

Please sign in to comment.