Skip to content

Commit

Permalink
Merge branch '11-alias-units-conversion'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Apr 8, 2024
2 parents f18d4b6 + 1bf5dd3 commit 8648831
Show file tree
Hide file tree
Showing 9 changed files with 2,143 additions and 85 deletions.
14 changes: 7 additions & 7 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def eq(self, other: "Property", *, rel_tol=1e-9, abs_tol=0) -> bool:
"""
if not isinstance(other, Property):
return False
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
return False
try:
prop = other.to_unit(self.unit) if self.unit != other.unit else other
Expand Down Expand Up @@ -155,7 +155,7 @@ def to_unit(self, unit: UnitDescriptor) -> Self:
>>> T.to_unit(RelativeTemperatureUnit.FAHRENHEIT)
<Property: 212.0 °F>
"""
if not unit.isinstance(self.unit.to_generic()):
if not unit.isinstance_equivalent(self.unit.to_generic()):
raise PropertyUnitConversionError(
f"cannot convert {self} to ({unit}) units; 'unit' should be an instance"
f" of {self.unit.to_generic()}. "
Expand Down Expand Up @@ -300,7 +300,7 @@ def __add__(self, other) -> Self:
f"cannot add {other} to ({self}); {other} is not a {self.__class__}; "
"only same properties can be added to each other. "
)
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
raise PropertyBinaryOperationError(
f"cannot add ({other}) to ({self}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
Expand Down Expand Up @@ -347,7 +347,7 @@ def __sub__(self, other) -> Self:
f"{self.__class__}; only same properties can be subtracted from each "
"other. "
)
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
raise PropertyBinaryOperationError(
f"cannot subtract ({other}) from ({self}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
Expand Down Expand Up @@ -382,7 +382,7 @@ def __rsub__(self, other) -> Self:
f"{self.__class__}; only same properties can be subtracted from each "
"other. "
)
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
raise PropertyBinaryOperationError(
f"cannot subtract ({self}) from ({other}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
Expand Down Expand Up @@ -422,7 +422,7 @@ def __eq__(self, other) -> bool:
"""
if not isinstance(other, Property):
return False
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
return False
try:
prop = other.to_unit(self.unit) if self.unit != other.unit else other
Expand Down Expand Up @@ -581,7 +581,7 @@ def _validate_comparison_input(self, other) -> None:
f"cannot compare {other} to ({self}); {other} is not a Property; "
"only properties can be compared to properties. "
)
if not self.unit.isinstance(other.unit.to_generic()):
if not self.unit.isinstance_equivalent(other.unit.to_generic()):
raise PropertyBinaryOperationError(
f"cannot compare ({other}) to ({self}); "
f"({other}) must have ({self.unit.to_generic()}) units. "
Expand Down
145 changes: 145 additions & 0 deletions src/property_utils/tests/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from property_utils.units.descriptors import (
GenericUnitDescriptor,
MeasurementUnit,
AliasMeasurementUnit,
Dimension,
Expand Down Expand Up @@ -52,11 +53,67 @@ class Unit3(AliasMeasurementUnit):
C = "C"
c = "c"

@classmethod
def aliased_generic_descriptor(cls) -> GenericUnitDescriptor:
return Unit1**3

@classmethod
def si(cls) -> "Unit3":
return cls.c


class Unit5(AliasMeasurementUnit):
E = "E"
e = "e"

@classmethod
def aliased_generic_descriptor(cls) -> GenericCompositeDimension:
return Unit1 / (Unit4**2)

@classmethod
def si(cls) -> "Unit5":
return cls.e


class Unit6(AliasMeasurementUnit):
F = "F"
f = "f"

@classmethod
def aliased_generic_descriptor(cls) -> Dimension:
return Unit1**2

@classmethod
def si(cls) -> "Unit6":
return cls.f


class Unit7(AliasMeasurementUnit):
G = "G"
g = "g"

@classmethod
def aliased_generic_descriptor(cls) -> CompositeDimension:
return Unit1 / (Unit4**2) / Unit2 # Unit5 / Unit2

@classmethod
def si(cls) -> "Unit7":
return cls.g


class Unit8(AliasMeasurementUnit):
H = "H"
h = "h"

@classmethod
def aliased_generic_descriptor(cls) -> CompositeDimension:
return Unit1**2 / (Unit4**2) # Unit6 / Unit4^2

@classmethod
def si(cls) -> "Unit8":
return cls.h


class UnregisteredConverter(AbsoluteUnitConverter): ...


Expand Down Expand Up @@ -87,6 +144,30 @@ class Unit4Converter(AbsoluteUnitConverter):
conversion_map = {Unit4.D: 1, Unit4.d: 5}


@register_converter(Unit5)
class Unit5Converter(AbsoluteUnitConverter):
reference_unit = Unit5.E
conversion_map = {Unit5.E: 1, Unit5.e: 15}


@register_converter(Unit6)
class Unit6Converter(AbsoluteUnitConverter):
reference_unit = Unit6.F
conversion_map = {Unit6.F: 1, Unit6.f: 2}


@register_converter(Unit7)
class Unit7Converter(AbsoluteUnitConverter):
reference_unit = Unit7.G
conversion_map = {Unit7.G: 1, Unit7.g: 3}


@register_converter(Unit8)
class Unit8Converter(AbsoluteUnitConverter):
reference_unit = Unit8.H
conversion_map = {Unit8.H: 1, Unit8.h: 4}


@register_converter(Unit1**2)
class Unit1_2Converter(ExponentiatedUnitConverter): ...

Expand Down Expand Up @@ -119,6 +200,14 @@ class Unit1Unit4Converter(CompositeUnitConverter): ...
class Unit1Unit4FractionConverter(CompositeUnitConverter): ...


@register_converter(Unit1 / (Unit4**2))
class Unit1Unit4_2Converter(CompositeUnitConverter): ...


@register_converter(Unit6 / (Unit4**2))
class Unit6Unit4_2Converter(CompositeUnitConverter): ...


@register_converter(Unit1**2 / Unit4**3)
class Unit1_2Unit4_3Converter(CompositeUnitConverter): ...

Expand All @@ -144,6 +233,34 @@ def dimension_3(power: float = 1) -> Dimension:
return Dimension(Unit3.C, power)


def dimension_4(power: float = 1) -> Dimension:
"""
D^power
"""
return Dimension(Unit4.D, power)


def dimension_5(power: float = 1) -> Dimension:
"""
E^power
"""
return Dimension(Unit5.E, power)


def dimension_6(power: float = 1) -> Dimension:
"""
F^power
"""
return Dimension(Unit6.F, power)


def dimension_7(power: float = 1) -> Dimension:
"""
G^power
"""
return Dimension(Unit7.G, power)


def generic_dimension_1(power: float = 1) -> GenericDimension:
"""
Unit1^power
Expand All @@ -165,6 +282,34 @@ def generic_dimension_3(power: float = 1) -> GenericDimension:
return GenericDimension(Unit3, power)


def generic_dimension_4(power: float = 1) -> GenericDimension:
"""
Unit4^power
"""
return GenericDimension(Unit4, power)


def generic_dimension_5(power: float = 1) -> GenericDimension:
"""
Unit5^power
"""
return GenericDimension(Unit5, power)


def generic_dimension_6(power: float = 1) -> GenericDimension:
"""
Unit6^power
"""
return GenericDimension(Unit6, power)


def generic_dimension_7(power: float = 1) -> GenericDimension:
"""
Unit7^power
"""
return GenericDimension(Unit7, power)


def composite_dimension() -> CompositeDimension:
"""
(A^2) * B / (C^3)
Expand Down
Loading

0 comments on commit 8648831

Please sign in to comment.