Skip to content

Commit

Permalink
Merge branch '4-unit-aliases'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Mar 19, 2024
2 parents 098b9b0 + e2adfc0 commit 68cb1e9
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 63 deletions.
60 changes: 30 additions & 30 deletions src/property_utils/properties/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def eq(self, other: "Property", *, rel_tol=1e-9, abs_tol=0) -> bool:
Raises `PropertyBinaryOperationError` if an error occurs during conversion
of other's units.
>>> from property_utils.units.units import TemperatureUnit, LengthUnit
>>> T1 = Property(33.333333, TemperatureUnit.KELVIN)
>>> T2 = Property(100/3, TemperatureUnit.KELVIN)
>>> from property_utils.units.units import AbsoluteTemperatureUnit, LengthUnit
>>> T1 = Property(33.333333, AbsoluteTemperatureUnit.KELVIN)
>>> T2 = Property(100/3, AbsoluteTemperatureUnit.KELVIN)
>>> T1 == T2
False
>>> T1.eq(T2)
Expand Down Expand Up @@ -106,8 +106,8 @@ def to_si(self) -> Self:
Raises `InvalidUnitConversion` if any error occurs in the unit conversion.
>>> from property_utils.units.units import TemperatureUnit
>>> T = Property(100, TemperatureUnit.CELCIUS)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T.to_si()
<Property: 373.15 K>
"""
Expand All @@ -132,9 +132,9 @@ def to_unit(self, unit: UnitDescriptor) -> Self:
Raises `UnitConversionError` if any error occurs in the unit conversion.
>>> from property_utils.units.units import TemperatureUnit
>>> T = Property(100, TemperatureUnit.CELCIUS)
>>> T.to_unit(TemperatureUnit.FAHRENHEIT)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T.to_unit(RelativeTemperatureUnit.FAHRENHEIT)
<Property: 212.0 °F>
"""
if not unit.isinstance(self.unit.to_generic()):
Expand All @@ -160,8 +160,8 @@ def __neg__(self) -> Self:
"""
Defines negation of properties.
>>> from property_utils.units.units import TemperatureUnit
>>> T = Property(3, TemperatureUnit.CELCIUS)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T = Property(3, RelativeTemperatureUnit.CELCIUS)
>>> -T
<Property: -3 °C>
"""
Expand All @@ -171,8 +171,8 @@ def __mul__(self, other) -> "Property":
"""
Defines multiplication between properties and numerics.
>>> from property_utils.units.units import TemperatureUnit, LengthUnit
>>> T = Property(300, TemperatureUnit.KELVIN)
>>> from property_utils.units.units import AbsoluteTemperatureUnit, LengthUnit
>>> T = Property(300, AbsoluteTemperatureUnit.KELVIN)
>>> 2*T
<Property: 600 K>
>>> A = Property(10, LengthUnit.METER**2)
Expand All @@ -194,8 +194,8 @@ def __rmul__(self, other) -> "Property":
"""
Defines multiplication between properties and numerics.
>>> from property_utils.units.units import TemperatureUnit, LengthUnit
>>> T = Property(300, TemperatureUnit.KELVIN)
>>> from property_utils.units.units import AbsoluteTemperatureUnit, LengthUnit
>>> T = Property(300, AbsoluteTemperatureUnit.KELVIN)
>>> 2*T
<Property: 600 K>
>>> A = Property(10, LengthUnit.METER**2)
Expand All @@ -208,8 +208,8 @@ def __truediv__(self, other) -> "Property":
"""
Defines division between properties and numerics.
>>> from property_utils.units.units import TemperatureUnit, LengthUnit
>>> T = Property(500, TemperatureUnit.KELVIN)
>>> from property_utils.units.units import AbsoluteTemperatureUnit, LengthUnit
>>> T = Property(500, AbsoluteTemperatureUnit.KELVIN)
>>> T/2
<Property: 250.0 K>
>>> A = Property(10, LengthUnit.METER**2)
Expand Down Expand Up @@ -241,8 +241,8 @@ def __rtruediv__(self, other) -> "Property":
"""
Defines right division between properties and numerics.
>>> from property_utils.units.units import TemperatureUnit
>>> T = Property(500, TemperatureUnit.KELVIN)
>>> from property_utils.units.units import AbsoluteTemperatureUnit
>>> T = Property(500, AbsoluteTemperatureUnit.KELVIN)
>>> 100/T
<Property: 0.2 / K>
"""
Expand Down Expand Up @@ -437,9 +437,9 @@ def __gt__(self, other) -> bool:
"""
Defines comparison between properties.
>>> from property_utils.units.units import TemperatureUnit
>>> T1 = Property(100, TemperatureUnit.CELCIUS)
>>> T2 = Property(213, TemperatureUnit.FAHRENHEIT)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T1 = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T2 = Property(213, RelativeTemperatureUnit.FAHRENHEIT)
>>> T1 > T2
False
"""
Expand All @@ -462,9 +462,9 @@ def __ge__(self, other) -> bool:
"""
Defines comparison between properties.
>>> from property_utils.units.units import TemperatureUnit
>>> T1 = Property(100, TemperatureUnit.CELCIUS)
>>> T2 = Property(212, TemperatureUnit.FAHRENHEIT)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T1 = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T2 = Property(212, RelativeTemperatureUnit.FAHRENHEIT)
>>> T1 >= T2
True
"""
Expand All @@ -487,9 +487,9 @@ def __lt__(self, other) -> bool:
"""
Defines comparison between properties.
>>> from property_utils.units.units import TemperatureUnit
>>> T1 = Property(100, TemperatureUnit.CELCIUS)
>>> T2 = Property(213, TemperatureUnit.FAHRENHEIT)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T1 = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T2 = Property(213, RelativeTemperatureUnit.FAHRENHEIT)
>>> T1 < T2
True
"""
Expand All @@ -512,9 +512,9 @@ def __le__(self, other) -> bool:
"""
Defines comparison between properties.
>>> from property_utils.units.units import TemperatureUnit
>>> T1 = Property(100, TemperatureUnit.CELCIUS)
>>> T2 = Property(213, TemperatureUnit.FAHRENHEIT)
>>> from property_utils.units.units import RelativeTemperatureUnit
>>> T1 = Property(100, RelativeTemperatureUnit.CELCIUS)
>>> T2 = Property(213, RelativeTemperatureUnit.FAHRENHEIT)
>>> T1 <= T2
True
"""
Expand Down
44 changes: 30 additions & 14 deletions src/property_utils/tests/units/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest_extensions import TestCase, args

from property_utils.units.units import (
TemperatureUnit,
RelativeTemperatureUnit,
AbsoluteTemperatureUnit,
LengthUnit,
MassUnit,
Expand All @@ -16,7 +16,7 @@
PowerUnit,
)
from property_utils.units.converters import (
TemperatureUnitConverter,
RelativeTemperatureUnitConverter,
AbsoluteTemperatureUnitConverter,
LengthUnitConverter,
MassUnitConverter,
Expand Down Expand Up @@ -56,51 +56,51 @@
@add_to(TemperatureUnitConverter_test_suite)
class TestTemperatureUnitConverterConvertToReference(TestCase):
def subject(self, value, from_descriptor):
return TemperatureUnitConverter.convert(
value, from_descriptor, TemperatureUnit.CELCIUS
return RelativeTemperatureUnitConverter.convert(
value, from_descriptor, RelativeTemperatureUnit.CELCIUS
)

@args({"value": 300, "from_descriptor": TemperatureUnit.KELVIN})
@args({"value": 300, "from_descriptor": AbsoluteTemperatureUnit.KELVIN})
def test_from_kelvin(self):
self.assertResultAlmost(26.85, 2)

@args({"value": 289.23, "from_descriptor": TemperatureUnit.FAHRENHEIT})
@args({"value": 289.23, "from_descriptor": RelativeTemperatureUnit.FAHRENHEIT})
def test_from_fahrenheit(self):
self.assertResultAlmost(142.905, 2)

@args({"value": 467, "from_descriptor": TemperatureUnit.RANKINE})
@args({"value": 467, "from_descriptor": AbsoluteTemperatureUnit.RANKINE})
def test_from_rankine(self):
self.assertResultAlmost(-13.705, 2)

@args({"value": 467, "from_descriptor": LengthUnit.CENTI_METER})
def test_from_invalid_descriptor(self):
self.assertResultRaises(UnitConversionError)

@args({"value": "231.2", "from_descriptor": TemperatureUnit.KELVIN})
@args({"value": "231.2", "from_descriptor": AbsoluteTemperatureUnit.KELVIN})
def test_with_invalid_value(self):
self.assertResultRaises(UnitConversionError)


@add_to(TemperatureUnitConverter_test_suite)
class TestTemperatureUnitConverterConvertFromReference(TestCase):
def subject(self, value, to_descriptor):
return TemperatureUnitConverter.convert(
value, TemperatureUnit.CELCIUS, to_descriptor
return RelativeTemperatureUnitConverter.convert(
value, RelativeTemperatureUnit.CELCIUS, to_descriptor
)

@args({"value": 100, "to_descriptor": TemperatureUnit.CELCIUS})
@args({"value": 100, "to_descriptor": RelativeTemperatureUnit.CELCIUS})
def test_to_celcius(self):
self.assertResult(100)

@args({"value": 10, "to_descriptor": TemperatureUnit.KELVIN})
@args({"value": 10, "to_descriptor": AbsoluteTemperatureUnit.KELVIN})
def test_to_kelvin(self):
self.assertResult(283.15)

@args({"value": 100, "to_descriptor": TemperatureUnit.FAHRENHEIT})
@args({"value": 100, "to_descriptor": RelativeTemperatureUnit.FAHRENHEIT})
def test_to_fahrenheit(self):
self.assertResult(212)

@args({"value": 67.98, "to_descriptor": TemperatureUnit.RANKINE})
@args({"value": 67.98, "to_descriptor": AbsoluteTemperatureUnit.RANKINE})
def test_to_rankine(self):
self.assertResultAlmost(614.03, 2)

Expand All @@ -124,6 +124,14 @@ def test_from_kelvin(self):
def test_from_rankine(self):
self.assertResultAlmost(450 / 1.8)

@args({"value": 25, "from_descriptor": RelativeTemperatureUnit.CELCIUS})
def test_from_celcius(self):
self.assertResult(298.15)

@args({"value": 212, "from_descriptor": RelativeTemperatureUnit.FAHRENHEIT})
def test_from_fahrenheit(self):
self.assertResultAlmost(373.15)


@add_to(AbsoluteTemperatureUnitConverter_test_suite)
class TestAbsoluteTemperatureUnitConverterConvertFromReference(TestCase):
Expand All @@ -140,6 +148,14 @@ def test_to_kelvin(self):
def test_to_rankine(self):
self.assertResult(200.50 * 1.8)

@args({"value": 10.5, "to_descriptor": RelativeTemperatureUnit.CELCIUS})
def test_to_celcius(self):
self.assertResult(-262.65)

@args({"value": 400, "to_descriptor": RelativeTemperatureUnit.FAHRENHEIT})
def test_to_fahrenheit(self):
self.assertResultAlmost(260.33)


@add_to(LengthUnitConverter_test_suite)
class TestLengthUnitConverterConvertToReference(TestCase):
Expand Down
57 changes: 57 additions & 0 deletions src/property_utils/units/aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# pylint: disable=unused-wildcard-import, wildcard-import
from property_utils.units.units import *

NON_DIMENSIONAL = NonDimensionalUnit.NON_DIMENSIONAL
CELCIUS = RelativeTemperatureUnit.CELCIUS
KELVIN = AbsoluteTemperatureUnit.KELVIN
FAHRENHEIT = RelativeTemperatureUnit.FAHRENHEIT
RANKINE = AbsoluteTemperatureUnit.RANKINE
MILLI_METER = LengthUnit.MILLI_METER
CENTI_METER = LengthUnit.CENTI_METER
METER = LengthUnit.METER
KILO_METER = LengthUnit.KILO_METER
INCH = LengthUnit.INCH
FOOT = LengthUnit.FOOT
YARD = LengthUnit.YARD
MILE = LengthUnit.MILE
NAUTICAL_MILE = LengthUnit.NAUTICAL_MILE
MILL_GRAM = MassUnit.MILLI_GRAM
GRAM = MassUnit.GRAM
KILO_GRAM = MassUnit.KILO_GRAM
METRIC_TONNE = MassUnit.METRIC_TONNE
POUND = MassUnit.POUND
MOL = AmountUnit.MOL
KILO_MOL = AmountUnit.KILO_MOL
MILLI_SECOND = TimeUnit.MILLI_SECOND
SECOND = TimeUnit.SECOND
MINUTE = TimeUnit.MINUTE
HOUR = TimeUnit.HOUR
DAY = TimeUnit.DAY
WEEK = TimeUnit.WEEK
MONTH = TimeUnit.MONTH
YEAR = TimeUnit.YEAR
MILLI_AMPERE = ElectricCurrentUnit.MILLI_AMPERE
AMPERE = ElectricCurrentUnit.AMPERE
KILO_AMPERE = ElectricCurrentUnit.KILO_AMPERE
NEWTON = ForceUnit.NEWTON
DYNE = ForceUnit.DYNE
MILLI_BAR = PressureUnit.MILLI_BAR
BAR = PressureUnit.BAR
PSI = PressureUnit.PSI
PASCAL = PressureUnit.PASCAL
KILO_PASCAL = PressureUnit.KILO_PASCAL
MEGA_PASCAL = PressureUnit.MEGA_PASCAL
JOULE = EnergyUnit.JOULE
KILO_JOULE = EnergyUnit.KILO_JOULE
MEGA_JOULE = EnergyUnit.MEGA_JOULE
GIGA_JOULE = EnergyUnit.GIGA_JOULE
CALORIE = EnergyUnit.CALORIE
KILO_CALORIE = EnergyUnit.KILO_CALORIE
BTU = EnergyUnit.BTU
ELECTRONVOLT = EnergyUnit.ELECTRONVOLT
WATTHOUR = EnergyUnit.WATTHOUR
KILO_WATTHOUR = EnergyUnit.KILO_WATTHOUR
WATT = PowerUnit.WATT
KILO_WATT = PowerUnit.KILO_WATT
MEGA_WATT = PowerUnit.MEGA_WATT
GIGA_WATT = PowerUnit.GIGA_WATT
Loading

0 comments on commit 68cb1e9

Please sign in to comment.