123
+ | @register_converter(AbsoluteTemperatureUnit)
-class AbsoluteTemperatureUnitConverter(AbsoluteUnitConverter):
- """
- Convert absolute temperature with this converter.
-
- Examples:
- >>> AbsoluteTemperatureUnitConverter.convert(10, AbsoluteTemperatureUnit.KELVIN, AbsoluteTemperatureUnit.RANKINE)
- 18.0
- """
-
- reference_unit = AbsoluteTemperatureUnit.KELVIN
- conversion_map = {
- AbsoluteTemperatureUnit.KELVIN: 1,
- AbsoluteTemperatureUnit.RANKINE: 1.8,
- }
-
- @override
- @classmethod
- def convert(
- cls,
- value: float,
- from_descriptor: UnitDescriptor,
- to_descriptor: UnitDescriptor,
- ) -> float:
- if not isinstance(value, (float, int)):
- raise UnitConversionError(f"invalid 'value': {value}; expected numeric. ")
- if from_descriptor.isinstance(
- RelativeTemperatureUnit
- ) or to_descriptor.isinstance(RelativeTemperatureUnit):
- return RelativeTemperatureUnitConverter.convert(
- value, from_descriptor, to_descriptor
- )
- return value * cls.get_factor(from_descriptor, to_descriptor)
+154
| @register_converter(AbsoluteTemperatureUnit)
+class AbsoluteTemperatureUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert absolute temperature with this converter.
+
+ Examples:
+ >>> AbsoluteTemperatureUnitConverter.convert(10, AbsoluteTemperatureUnit.KELVIN, AbsoluteTemperatureUnit.RANKINE)
+ 18.0
+ """
+
+ reference_unit = AbsoluteTemperatureUnit.KELVIN
+ conversion_map = {
+ AbsoluteTemperatureUnit.KELVIN: 1,
+ AbsoluteTemperatureUnit.RANKINE: 1.8,
+ }
+
+ @override
+ @classmethod
+ def convert(
+ cls,
+ value: float,
+ from_descriptor: UnitDescriptor,
+ to_descriptor: UnitDescriptor,
+ ) -> float:
+ if not isinstance(value, (float, int)):
+ raise UnitConversionError(f"invalid 'value': {value}; expected numeric. ")
+ if from_descriptor.isinstance(
+ RelativeTemperatureUnit
+ ) or to_descriptor.isinstance(RelativeTemperatureUnit):
+ return RelativeTemperatureUnitConverter.convert(
+ value, from_descriptor, to_descriptor
+ )
+ return value * cls.get_factor(from_descriptor, to_descriptor)
|
@@ -1350,17 +1368,7 @@
Source code in src/property_utils/units/converters.py
- 292
-293
-294
-295
-296
-297
-298
-299
-300
-301
-302
+ | @register_converter(EnergyUnit)
-class AliasEnergyUnitConverter(AbsoluteUnitConverter):
- """
- Convert energy units with this converter.
-
- Examples:
- >>> AliasEnergyUnitConverter.convert(2500, EnergyUnit.JOULE, EnergyUnit.KILO_JOULE)
- 2.5
- """
-
- reference_unit = EnergyUnit.JOULE
- conversion_map = {
- EnergyUnit.JOULE: 1,
- EnergyUnit.KILO_JOULE: UnitPrefix.KILO.inverse(),
- EnergyUnit.MEGA_JOULE: UnitPrefix.MEGA.inverse(),
- EnergyUnit.GIGA_JOULE: UnitPrefix.GIGA.inverse(),
- EnergyUnit.CALORIE: 1 / 4.184,
- EnergyUnit.KILO_CALORIE: (1 / 4.184) * UnitPrefix.KILO.inverse(),
- EnergyUnit.BTU: 1 / 1055.0,
- EnergyUnit.ELECTRONVOLT: 6.242e18,
- EnergyUnit.WATTHOUR: 1 / 3600,
- EnergyUnit.KILO_WATTHOUR: (1 / 3600) * UnitPrefix.KILO.inverse(),
- }
+314
+315
+316
+317
+318
+319
+320
+321
+322
+323
+324
| @register_converter(EnergyUnit)
+class AliasEnergyUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert energy units with this converter.
+
+ Examples:
+ >>> AliasEnergyUnitConverter.convert(2500, EnergyUnit.JOULE, EnergyUnit.KILO_JOULE)
+ 2.5
+ """
+
+ reference_unit = EnergyUnit.JOULE
+ conversion_map = {
+ EnergyUnit.JOULE: 1,
+ EnergyUnit.KILO_JOULE: UnitPrefix.KILO.inverse(),
+ EnergyUnit.MEGA_JOULE: UnitPrefix.MEGA.inverse(),
+ EnergyUnit.GIGA_JOULE: UnitPrefix.GIGA.inverse(),
+ EnergyUnit.CALORIE: 1 / 4.184,
+ EnergyUnit.KILO_CALORIE: (1 / 4.184) * UnitPrefix.KILO.inverse(),
+ EnergyUnit.BTU: 1 / 1055.0,
+ EnergyUnit.ELECTRONVOLT: 6.242e18,
+ EnergyUnit.WATTHOUR: 1 / 3600,
+ EnergyUnit.KILO_WATTHOUR: (1 / 3600) * UnitPrefix.KILO.inverse(),
+ }
|
@@ -1444,29 +1462,29 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(ForceUnit)
-class AliasForceUnitConverter(AbsoluteUnitConverter):
- """
- Convert force units with this converter.
-
- Examples:
- >>> AliasForceUnitConverter.convert(2, ForceUnit.NEWTON, ForceUnit.DYNE)
- 200000.0
- """
-
- reference_unit = ForceUnit.NEWTON
- conversion_map = {ForceUnit.NEWTON: 1, ForceUnit.DYNE: 100_000}
+ | @register_converter(ForceUnit)
+class AliasForceUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert force units with this converter.
+
+ Examples:
+ >>> AliasForceUnitConverter.convert(2, ForceUnit.NEWTON, ForceUnit.DYNE)
+ 200000.0
+ """
+
+ reference_unit = ForceUnit.NEWTON
+ conversion_map = {ForceUnit.NEWTON: 1, ForceUnit.DYNE: 100_000}
|
@@ -1516,39 +1534,39 @@
Source code in src/property_utils/units/converters.py
- 317
-318
-319
-320
-321
-322
-323
-324
-325
-326
-327
+ | @register_converter(PowerUnit)
-class AliasPowerUnitConverter(AbsoluteUnitConverter):
- """
- Convert power units with this converter.
-
- Examples:
- >>> AliasPowerUnitConverter.convert(5, PowerUnit.KILO_WATT, PowerUnit.WATT)
- 5000.0
- """
-
- reference_unit = PowerUnit.WATT
- conversion_map = {
- PowerUnit.WATT: 1,
- PowerUnit.KILO_WATT: UnitPrefix.KILO.inverse(),
- PowerUnit.MEGA_WATT: UnitPrefix.MEGA.inverse(),
- PowerUnit.GIGA_WATT: UnitPrefix.GIGA.inverse(),
- }
+333
+334
+335
+336
+337
+338
+339
+340
+341
+342
+343
| @register_converter(PowerUnit)
+class AliasPowerUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert power units with this converter.
+
+ Examples:
+ >>> AliasPowerUnitConverter.convert(5, PowerUnit.KILO_WATT, PowerUnit.WATT)
+ 5000.0
+ """
+
+ reference_unit = PowerUnit.WATT
+ conversion_map = {
+ PowerUnit.WATT: 1,
+ PowerUnit.KILO_WATT: UnitPrefix.KILO.inverse(),
+ PowerUnit.MEGA_WATT: UnitPrefix.MEGA.inverse(),
+ PowerUnit.GIGA_WATT: UnitPrefix.GIGA.inverse(),
+ }
|
@@ -1598,17 +1616,7 @@
Source code in src/property_utils/units/converters.py
- 271
-272
-273
-274
-275
-276
-277
-278
-279
-280
-281
+ | @register_converter(PressureUnit)
-class AliasPressureUnitConverter(AbsoluteUnitConverter):
- """
- Convert pressure units with this converter.
-
- Examples:
- >>> AliasPressureUnitConverter.convert(2, PressureUnit.BAR, PressureUnit.KILO_PASCAL)
- 200.0
- """
-
- reference_unit = PressureUnit.BAR
- conversion_map = {
- PressureUnit.MILLI_BAR: UnitPrefix.MILLI.inverse(),
- PressureUnit.BAR: 1,
- PressureUnit.PSI: 14.5038,
- PressureUnit.PASCAL: 100_000,
- PressureUnit.KILO_PASCAL: 100,
- PressureUnit.MEGA_PASCAL: 0.1,
- }
+289
+290
+291
+292
+293
+294
+295
+296
+297
+298
+299
| @register_converter(PressureUnit)
+class AliasPressureUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert pressure units with this converter.
+
+ Examples:
+ >>> AliasPressureUnitConverter.convert(2, PressureUnit.BAR, PressureUnit.KILO_PASCAL)
+ 200.0
+ """
+
+ reference_unit = PressureUnit.BAR
+ conversion_map = {
+ PressureUnit.MILLI_BAR: UnitPrefix.MILLI.inverse(),
+ PressureUnit.BAR: 1,
+ PressureUnit.PSI: 14.5038,
+ PressureUnit.PASCAL: 100_000,
+ PressureUnit.KILO_PASCAL: 100,
+ PressureUnit.MEGA_PASCAL: 0.1,
+ }
|
@@ -1684,29 +1702,29 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(AmountUnit)
-class AmountUnitConverter(AbsoluteUnitConverter):
- """
- Convert amount units with this converter.
-
- Examples:
- >>> AmountUnitConverter.convert(2000, AmountUnit.MOL, AmountUnit.KILO_MOL)
- 2.0
- """
-
- reference_unit = AmountUnit.MOL
- conversion_map = {AmountUnit.MOL: 1, AmountUnit.KILO_MOL: UnitPrefix.KILO.inverse()}
+ | @register_converter(AmountUnit)
+class AmountUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert amount units with this converter.
+
+ Examples:
+ >>> AmountUnitConverter.convert(2000, AmountUnit.MOL, AmountUnit.KILO_MOL)
+ 2.0
+ """
+
+ reference_unit = AmountUnit.MOL
+ conversion_map = {AmountUnit.MOL: 1, AmountUnit.KILO_MOL: UnitPrefix.KILO.inverse()}
|
@@ -1756,23 +1774,23 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(LengthUnit**2)
-class AreaUnitConverter(ExponentiatedUnitConverter):
- """
- Convert area units with this converter.
-
- Examples:
- >>> AreaUnitConverter.convert(1, LengthUnit.METER**2, LengthUnit.CENTI_METER**2)
- 10000.0
- """
+ | @register_converter(LengthUnit**2)
+class AreaUnitConverter(ExponentiatedUnitConverter):
+ """
+ Convert area units with this converter.
+
+ Examples:
+ >>> AreaUnitConverter.convert(1, LengthUnit.METER**2, LengthUnit.CENTI_METER**2)
+ 10000.0
+ """
|
@@ -1822,37 +1840,37 @@
Source code in src/property_utils/units/converters.py
- 239
-240
-241
-242
-243
-244
-245
-246
-247
-248
-249
+ | @register_converter(ElectricCurrentUnit)
-class ElectricCurrentUnitConverter(AbsoluteUnitConverter):
- """
- Convert electric current units with this converter.
-
- Examples:
- >>> ElectricCurrentUnitConverter.convert(1000, ElectricCurrentUnit.MILLI_AMPERE, ElectricCurrentUnit.AMPERE)
- 1.0
- """
-
- reference_unit = ElectricCurrentUnit.AMPERE
- conversion_map = {
- ElectricCurrentUnit.MILLI_AMPERE: UnitPrefix.MILLI.inverse(),
- ElectricCurrentUnit.AMPERE: 1,
- ElectricCurrentUnit.KILO_AMPERE: UnitPrefix.KILO.inverse(),
- }
+254
+255
+256
+257
+258
+259
+260
+261
+262
+263
+264
| @register_converter(ElectricCurrentUnit)
+class ElectricCurrentUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert electric current units with this converter.
+
+ Examples:
+ >>> ElectricCurrentUnitConverter.convert(1000, ElectricCurrentUnit.MILLI_AMPERE, ElectricCurrentUnit.AMPERE)
+ 1.0
+ """
+
+ reference_unit = ElectricCurrentUnit.AMPERE
+ conversion_map = {
+ ElectricCurrentUnit.MILLI_AMPERE: UnitPrefix.MILLI.inverse(),
+ ElectricCurrentUnit.AMPERE: 1,
+ ElectricCurrentUnit.KILO_AMPERE: UnitPrefix.KILO.inverse(),
+ }
|
@@ -1904,27 +1922,27 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(EnergyUnit.aliased_generic_descriptor())
-class EnergyUnitConverter(CompositeUnitConverter):
- """
- Convert energy units (mass * length^2 / time^2) with this converter.
-
- Examples:
- >>> from_unit = MassUnit.KILO_GRAM * (LengthUnit.METER**2) / (TimeUnit.MINUTE**2)
- >>> to_unit = MassUnit.METRIC_TONNE * (LengthUnit.CENTI_METER**2) / (TimeUnit.MINUTE**2)
- >>> EnergyUnitConverter.convert(25, from_unit, to_unit)
- 250.0
- """
+ | @register_converter(EnergyUnit.aliased_generic_descriptor())
+class EnergyUnitConverter(CompositeUnitConverter):
+ """
+ Convert energy units (mass * length^2 / time^2) with this converter.
+
+ Examples:
+ >>> from_unit = MassUnit.KILO_GRAM * (LengthUnit.METER**2) / (TimeUnit.MINUTE**2)
+ >>> to_unit = MassUnit.METRIC_TONNE * (LengthUnit.CENTI_METER**2) / (TimeUnit.MINUTE**2)
+ >>> EnergyUnitConverter.convert(25, from_unit, to_unit)
+ 250.0
+ """
|
@@ -1976,27 +1994,27 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(ForceUnit.aliased_generic_descriptor())
-class ForceUnitConverter(CompositeUnitConverter):
- """
- Convert force units (mass * length / time^2) with this converter.
-
- Examples:
- >>> from_unit = MassUnit.KILO_GRAM * LengthUnit.CENTI_METER / (TimeUnit.SECOND**2)
- >>> to_unit = MassUnit.GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)
- >>> ForceUnitConverter.convert(100, from_unit, to_unit)
- 1000.0
- """
+ | @register_converter(ForceUnit.aliased_generic_descriptor())
+class ForceUnitConverter(CompositeUnitConverter):
+ """
+ Convert force units (mass * length / time^2) with this converter.
+
+ Examples:
+ >>> from_unit = MassUnit.KILO_GRAM * LengthUnit.CENTI_METER / (TimeUnit.SECOND**2)
+ >>> to_unit = MassUnit.GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)
+ >>> ForceUnitConverter.convert(100, from_unit, to_unit)
+ 1000.0
+ """
|
@@ -2046,17 +2064,7 @@
Source code in src/property_utils/units/converters.py
- 158
-159
-160
-161
-162
-163
-164
-165
-166
-167
-168
+ | @register_converter(LengthUnit)
-class LengthUnitConverter(AbsoluteUnitConverter):
- """
- Convert length units with this converter.
-
- Examples:
- >>> LengthUnitConverter.convert(2000, LengthUnit.MILLI_METER, LengthUnit.METER)
- 2.0
- """
-
- reference_unit = LengthUnit.METER
- conversion_map = {
- LengthUnit.MILLI_METER: UnitPrefix.MILLI.inverse(),
- LengthUnit.CENTI_METER: UnitPrefix.CENTI.inverse(),
- LengthUnit.METER: 1,
- LengthUnit.KILO_METER: UnitPrefix.KILO.inverse(),
- LengthUnit.INCH: 39.37,
- LengthUnit.FOOT: 3.281,
- LengthUnit.YARD: 1.094,
- LengthUnit.MILE: 1 / 1609,
- LengthUnit.NAUTICAL_MILE: 1 / 1852,
- }
+179
+180
+181
+182
+183
+184
+185
+186
+187
+188
+189
| @register_converter(LengthUnit)
+class LengthUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert length units with this converter.
+
+ Examples:
+ >>> LengthUnitConverter.convert(2000, LengthUnit.MILLI_METER, LengthUnit.METER)
+ 2.0
+ """
+
+ reference_unit = LengthUnit.METER
+ conversion_map = {
+ LengthUnit.MILLI_METER: UnitPrefix.MILLI.inverse(),
+ LengthUnit.CENTI_METER: UnitPrefix.CENTI.inverse(),
+ LengthUnit.METER: 1,
+ LengthUnit.KILO_METER: UnitPrefix.KILO.inverse(),
+ LengthUnit.INCH: 39.37,
+ LengthUnit.FOOT: 3.281,
+ LengthUnit.YARD: 1.094,
+ LengthUnit.MILE: 1 / 1609,
+ LengthUnit.NAUTICAL_MILE: 1 / 1852,
+ }
|
@@ -2138,41 +2156,102 @@
Source code in src/property_utils/units/converters.py
- 182
-183
-184
-185
-186
-187
-188
-189
-190
-191
-192
+ | @register_converter(MassUnit)
-class MassUnitConverter(AbsoluteUnitConverter):
- """
- Convert mass units with this converter.
-
- Examples:
- >>> MassUnitConverter.convert(10, MassUnit.KILO_GRAM, MassUnit.GRAM)
- 10000.0
- """
-
- reference_unit = MassUnit.KILO_GRAM
- conversion_map = {
- MassUnit.MILLI_GRAM: UnitPrefix.KILO * UnitPrefix.MILLI.inverse(),
- MassUnit.GRAM: UnitPrefix.KILO,
- MassUnit.KILO_GRAM: 1,
- MassUnit.METRIC_TONNE: 1 / 1_000.0,
- MassUnit.POUND: 2.205,
- }
+199
+200
+201
+202
+203
+204
+205
+206
+207
+208
+209
| @register_converter(MassUnit)
+class MassUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert mass units with this converter.
+
+ Examples:
+ >>> MassUnitConverter.convert(10, MassUnit.KILO_GRAM, MassUnit.GRAM)
+ 10000.0
+ """
+
+ reference_unit = MassUnit.KILO_GRAM
+ conversion_map = {
+ MassUnit.MILLI_GRAM: UnitPrefix.KILO * UnitPrefix.MILLI.inverse(),
+ MassUnit.GRAM: UnitPrefix.KILO,
+ MassUnit.KILO_GRAM: 1,
+ MassUnit.METRIC_TONNE: 1 / 1_000.0,
+ MassUnit.POUND: 2.205,
+ }
+
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ NonDimensionalUnitConverter
+
+
+
+
+
+
+
+ Bases: AbsoluteUnitConverter
+
+
+ This converter is needed for compatibility, i.e. for conversions to work from
+non-dimensional units to non-dimensional dimensions.
+
+
+ Source code in src/property_utils/units/converters.py
+ | @register_converter(NonDimensionalUnit)
+class NonDimensionalUnitConverter(AbsoluteUnitConverter):
+ """
+ This converter is needed for compatibility, i.e. for conversions to work from
+ non-dimensional units to non-dimensional dimensions.
+ """
+
+ reference_unit = NonDimensionalUnit.NON_DIMENSIONAL
+ conversion_map = {NonDimensionalUnit.NON_DIMENSIONAL: 1}
|
@@ -2224,27 +2303,27 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(PowerUnit.aliased_generic_descriptor())
-class PowerUnitConverter(CompositeUnitConverter):
- """
- Convert power units (mass * length^2 / time^3) with this converter.
-
- Examples:
- >>> from_unit = MassUnit.KILO_GRAM * (LengthUnit.METER**2) / (TimeUnit.MINUTE**3)
- >>> to_unit = MassUnit.METRIC_TONNE * (LengthUnit.CENTI_METER**2) / (TimeUnit.MINUTE**3)
- >>> PowerUnitConverter.convert(15, from_unit, to_unit)
- 150.0
- """
+ | @register_converter(PowerUnit.aliased_generic_descriptor())
+class PowerUnitConverter(CompositeUnitConverter):
+ """
+ Convert power units (mass * length^2 / time^3) with this converter.
+
+ Examples:
+ >>> from_unit = MassUnit.KILO_GRAM * (LengthUnit.METER**2) / (TimeUnit.MINUTE**3)
+ >>> to_unit = MassUnit.METRIC_TONNE * (LengthUnit.CENTI_METER**2) / (TimeUnit.MINUTE**3)
+ >>> PowerUnitConverter.convert(15, from_unit, to_unit)
+ 150.0
+ """
|
@@ -2296,27 +2375,27 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(PressureUnit.aliased_generic_descriptor())
-class PressureUnitConverter(CompositeUnitConverter):
- """
- Convert pressure units (mass / length / time^2) with this converter.
-
- Examples:
- >>> from_unit = MassUnit.GRAM / LengthUnit.CENTI_METER / (TimeUnit.HOUR**2)
- >>> to_unit = MassUnit.KILO_GRAM / LengthUnit.METER / (TimeUnit.HOUR**2)
- >>> PressureUnitConverter.convert(50, from_unit, to_unit)
- 5.0
- """
+ | @register_converter(PressureUnit.aliased_generic_descriptor())
+class PressureUnitConverter(CompositeUnitConverter):
+ """
+ Convert pressure units (mass / length / time^2) with this converter.
+
+ Examples:
+ >>> from_unit = MassUnit.GRAM / LengthUnit.CENTI_METER / (TimeUnit.HOUR**2)
+ >>> to_unit = MassUnit.KILO_GRAM / LengthUnit.METER / (TimeUnit.HOUR**2)
+ >>> PressureUnitConverter.convert(50, from_unit, to_unit)
+ 5.0
+ """
|
@@ -2366,8 +2445,7 @@
Source code in src/property_utils/units/converters.py
- 96
- 97
+ | @register_converter(RelativeTemperatureUnit)
-class RelativeTemperatureUnitConverter(
- RelativeUnitConverter
-): # pylint: disable=too-few-public-methods
- """
- Convert temperature units with this converter.
-
- Examples:
- >>> RelativeTemperatureUnitConverter.convert(100, RelativeTemperatureUnit.CELCIUS, RelativeTemperatureUnit.FAHRENHEIT)
- 212.0
- """
-
- reference_unit = RelativeTemperatureUnit.CELCIUS
- conversion_map = {
- RelativeTemperatureUnit.CELCIUS: lambda t: t,
- AbsoluteTemperatureUnit.KELVIN: lambda t: t - 273.15,
- RelativeTemperatureUnit.FAHRENHEIT: lambda t: (t - 32) / 1.8,
- AbsoluteTemperatureUnit.RANKINE: lambda t: (t / 1.8) - 273.15,
- }
- reference_conversion_map = {
- RelativeTemperatureUnit.CELCIUS: lambda t: t,
- AbsoluteTemperatureUnit.KELVIN: lambda t: t + 273.15,
- RelativeTemperatureUnit.FAHRENHEIT: lambda t: (t * 1.8) + 32,
- AbsoluteTemperatureUnit.RANKINE: lambda t: (t + 273.15) * 1.8,
- }
+119
| @register_converter(RelativeTemperatureUnit)
+class RelativeTemperatureUnitConverter(RelativeUnitConverter): # pylint: disable=too-few-public-methods
+ """
+ Convert temperature units with this converter.
+
+ Examples:
+ >>> RelativeTemperatureUnitConverter.convert(100, RelativeTemperatureUnit.CELCIUS, RelativeTemperatureUnit.FAHRENHEIT)
+ 212.0
+ """
+
+ reference_unit = RelativeTemperatureUnit.CELCIUS
+ conversion_map = {
+ RelativeTemperatureUnit.CELCIUS: lambda t: t,
+ AbsoluteTemperatureUnit.KELVIN: lambda t: t - 273.15,
+ RelativeTemperatureUnit.FAHRENHEIT: lambda t: (t - 32) / 1.8,
+ AbsoluteTemperatureUnit.RANKINE: lambda t: (t / 1.8) - 273.15,
+ }
+ reference_conversion_map = {
+ RelativeTemperatureUnit.CELCIUS: lambda t: t,
+ AbsoluteTemperatureUnit.KELVIN: lambda t: t + 273.15,
+ RelativeTemperatureUnit.FAHRENHEIT: lambda t: (t * 1.8) + 32,
+ AbsoluteTemperatureUnit.RANKINE: lambda t: (t + 273.15) * 1.8,
+ }
|
@@ -2464,17 +2539,7 @@
Source code in src/property_utils/units/converters.py
- 216
-217
-218
-219
-220
-221
-222
-223
-224
-225
-226
+ | @register_converter(TimeUnit)
-class TimeUnitConverter(AbsoluteUnitConverter):
- """
- Convert time units with this converter.
-
- Examples:
- >>> TimeUnitConverter.convert(1, TimeUnit.HOUR, TimeUnit.SECOND)
- 3600.0
- """
-
- reference_unit = TimeUnit.SECOND
- conversion_map = {
- TimeUnit.MILLI_SECOND: UnitPrefix.MILLI.inverse(),
- TimeUnit.SECOND: 1,
- TimeUnit.MINUTE: 1 / 60.0,
- TimeUnit.HOUR: 1 / 60.0 / 60.0,
- TimeUnit.DAY: 1 / 60.0 / 60.0 / 24.0,
- TimeUnit.WEEK: 1 / 60.0 / 60.0 / 24.0 / 7,
- TimeUnit.MONTH: 1 / 60.0 / 60.0 / 24.0 / (365 / 12),
- TimeUnit.YEAR: 1 / 60.0 / 60.0 / 24.0 / 365,
- }
+236
+237
+238
+239
+240
+241
+242
+243
+244
+245
+246
| @register_converter(TimeUnit)
+class TimeUnitConverter(AbsoluteUnitConverter):
+ """
+ Convert time units with this converter.
+
+ Examples:
+ >>> TimeUnitConverter.convert(1, TimeUnit.HOUR, TimeUnit.SECOND)
+ 3600.0
+ """
+
+ reference_unit = TimeUnit.SECOND
+ conversion_map = {
+ TimeUnit.MILLI_SECOND: UnitPrefix.MILLI.inverse(),
+ TimeUnit.SECOND: 1,
+ TimeUnit.MINUTE: 1 / 60.0,
+ TimeUnit.HOUR: 1 / 60.0 / 60.0,
+ TimeUnit.DAY: 1 / 60.0 / 60.0 / 24.0,
+ TimeUnit.WEEK: 1 / 60.0 / 60.0 / 24.0 / 7,
+ TimeUnit.MONTH: 1 / 60.0 / 60.0 / 24.0 / (365 / 12),
+ TimeUnit.YEAR: 1 / 60.0 / 60.0 / 24.0 / 365,
+ }
|
@@ -2557,8 +2632,7 @@
Source code in src/property_utils/units/converters.py
- 58
-59
+ | class UnitPrefix(float, Enum):
- """
- Enumeration of unit prefixes.
- Handy when converting to and fro prefixed units.
-
- Examples:
- >>> centimeters = 225
- >>> meters = centimeters * UnitPrefix.CENTI
- >>> meters
- 2.25
- """
-
- PICO = 1e-12
- NANO = 1e-9
- MICRO = 1e-6
- MILLI = 1e-3
- CENTI = 1e-2
- DECI = 1e-1
- DECA = 1e1
- HECTO = 1e2
- KILO = 1e3
- MEGA = 1e6
- GIGA = 1e9
- TERA = 1e12
-
- def inverse(self) -> float:
- """
- Return the inverse of the unit prefix. Use when prefixing a unit.
-
- Examples:
- >>> meters = 50.26
- >>> centimeters = meters * UnitPrefix.CENTI.inverse()
- >>> centimeters
- 5026.0
- """
- return 1 / self.value
+93
+94
| class UnitPrefix(float, Enum):
+ """
+ Enumeration of unit prefixes.
+ Handy when converting to and fro prefixed units.
+
+ Examples:
+ >>> centimeters = 225
+ >>> meters = centimeters * UnitPrefix.CENTI
+ >>> meters
+ 2.25
+ """
+
+ PICO = 1e-12
+ NANO = 1e-9
+ MICRO = 1e-6
+ MILLI = 1e-3
+ CENTI = 1e-2
+ DECI = 1e-1
+ DECA = 1e1
+ HECTO = 1e2
+ KILO = 1e3
+ MEGA = 1e6
+ GIGA = 1e9
+ TERA = 1e12
+
+ def inverse(self) -> float:
+ """
+ Return the inverse of the unit prefix. Use when prefixing a unit.
+
+ Examples:
+ >>> meters = 50.26
+ >>> centimeters = meters * UnitPrefix.CENTI.inverse()
+ >>> centimeters
+ 5026.0
+ """
+ return 1 / self.value
|
@@ -2666,8 +2741,7 @@
Source code in src/property_utils/units/converters.py
- 83
-84
+ | def inverse(self) -> float:
- """
- Return the inverse of the unit prefix. Use when prefixing a unit.
-
- Examples:
- >>> meters = 50.26
- >>> centimeters = meters * UnitPrefix.CENTI.inverse()
- >>> centimeters
- 5026.0
- """
- return 1 / self.value
+93
+94
| def inverse(self) -> float:
+ """
+ Return the inverse of the unit prefix. Use when prefixing a unit.
+
+ Examples:
+ >>> meters = 50.26
+ >>> centimeters = meters * UnitPrefix.CENTI.inverse()
+ >>> centimeters
+ 5026.0
+ """
+ return 1 / self.value
|
@@ -2727,23 +2802,23 @@
Source code in src/property_utils/units/converters.py
- | @register_converter(LengthUnit**3)
-class VolumeUnitConverter(ExponentiatedUnitConverter):
- """
- Convert volume units with this converter.
-
- Examples:
- >>> VolumeUnitConverter.convert(1, LengthUnit.METER**3, LengthUnit.CENTI_METER**3)
- 1000000.0
- """
+ | @register_converter(LengthUnit**3)
+class VolumeUnitConverter(ExponentiatedUnitConverter):
+ """
+ Convert volume units with this converter.
+
+ Examples:
+ >>> VolumeUnitConverter.convert(1, LengthUnit.METER**3, LengthUnit.CENTI_METER**3)
+ 1000000.0
+ """
|
diff --git a/descriptors/index.html b/descriptors/index.html
index 546536c..7be9060 100644
--- a/descriptors/index.html
+++ b/descriptors/index.html
@@ -1280,6 +1280,15 @@
+
+
+
+
+
+ is_non_dimensional
+
+
+
@@ -2155,6 +2164,15 @@
+
+
+
+
+
+ is_non_dimensional
+
+
+
@@ -2406,26 +2424,7 @@
Source code in src/property_utils/units/descriptors.py
- 488
-489
-490
-491
-492
-493
-494
-495
-496
-497
-498
-499
-500
-501
-502
-503
-504
-505
-506
-507
+ | class AliasMeasurementUnit(MeasurementUnit):
- """
- Base class for common composite units of physical quantities.
-
- Subclasses of `MeasurementUnit` represent only primitive physical quantities.
- However, many common physical properties have composite units (e.g. pressure, force,
- energy, etc), thus subclasses of this class alias composite units as primitive ones.
-
- Only very common composite units should be aliased.
-
- e.g. you can create an alias for pressure units, instead of using mass * length / (
- time^2) units.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
- ... PASCAL = "Pa"
- ... KILO_PASCAL = "kPa"
- ... PSI = "psi"
- """
-
- @staticmethod
- def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit:
- """
- Create an AliasMeasurementUnit from given descriptor.
- If descriptor is already an AliasMeasurementUnit, it returns the same object.
+556
+557
+558
+559
+560
+561
+562
+563
+564
+565
+566
+567
+568
+569
+570
+571
+572
+573
+574
+575
| class AliasMeasurementUnit(MeasurementUnit):
+ """
+ Base class for common composite units of physical quantities.
+
+ Subclasses of `MeasurementUnit` represent only primitive physical quantities.
+ However, many common physical properties have composite units (e.g. pressure, force,
+ energy, etc), thus subclasses of this class alias composite units as primitive ones.
- This function does not serve as a constructor for AliasMeasurementUnit, rather
- 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 `UnitDescriptorTypeError` if given descriptor cannot be translated
- to an AliasMeasurementUnit instance.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
-
- >>> bar = MeasurementUnit.from_descriptor(PressureUnit.BAR**(-1))
- >>> bar
- <PressureUnit: bar>
- """
- if isinstance(descriptor, Dimension) and isinstance(
- descriptor.unit, AliasMeasurementUnit
- ):
- return descriptor.unit
- if isinstance(descriptor, AliasMeasurementUnit):
- return descriptor
- raise UnitDescriptorTypeError(
- 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.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit): ...
- >>> class AreaUnit(AliasMeasurementUnit):
- ... @classmethod
- ... def aliased_generic_descriptor(cls):
- ... return LengthUnit**2
- """
- raise NotImplementedError
+ Only very common composite units should be aliased.
+
+ e.g. you can create an alias for pressure units, instead of using mass * length / (
+ time^2) units.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+ ... PASCAL = "Pa"
+ ... KILO_PASCAL = "kPa"
+ ... PSI = "psi"
+ """
+
+ @staticmethod
+ def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit:
+ """
+ Create an AliasMeasurementUnit from given descriptor.
+ If descriptor is already an AliasMeasurementUnit, it returns the same object.
+
+ This function does not serve as a constructor for AliasMeasurementUnit, rather
+ 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 `UnitDescriptorTypeError` if given descriptor cannot be translated
+ to an AliasMeasurementUnit instance.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+
+ >>> bar = MeasurementUnit.from_descriptor(PressureUnit.BAR**(-1))
+ >>> bar
+ <PressureUnit: bar>
+ """
+ if isinstance(descriptor, Dimension) and isinstance(
+ descriptor.unit, AliasMeasurementUnit
+ ):
+ return descriptor.unit
+ if isinstance(descriptor, AliasMeasurementUnit):
+ return descriptor
+ raise UnitDescriptorTypeError(
+ 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.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit): ...
+ >>> class AreaUnit(AliasMeasurementUnit):
+ ... @classmethod
+ ... def aliased_generic_descriptor(cls):
+ ... return LengthUnit**2
+ """
+ raise NotImplementedError
|
@@ -2587,33 +2605,33 @@
Source code in src/property_utils/units/descriptors.py
- | @classmethod
-def aliased_generic_descriptor(cls) -> GenericUnitDescriptor:
- """
- Implement this method by returning the generic of the unit descriptor that this
- measurement unit aliases.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit): ...
- >>> class AreaUnit(AliasMeasurementUnit):
- ... @classmethod
- ... def aliased_generic_descriptor(cls):
- ... return LengthUnit**2
- """
- raise NotImplementedError
+ | @classmethod
+def aliased_generic_descriptor(cls) -> GenericUnitDescriptor:
+ """
+ Implement this method by returning the generic of the unit descriptor that this
+ measurement unit aliases.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit): ...
+ >>> class AreaUnit(AliasMeasurementUnit):
+ ... @classmethod
+ ... def aliased_generic_descriptor(cls):
+ ... return LengthUnit**2
+ """
+ raise NotImplementedError
|
@@ -2657,26 +2675,7 @@
Source code in src/property_utils/units/descriptors.py
- 509
-510
-511
-512
-513
-514
-515
-516
-517
-518
-519
-520
-521
-522
-523
-524
-525
-526
-527
-528
+ | @staticmethod
-def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit:
- """
- Create an AliasMeasurementUnit from given descriptor.
- If descriptor is already an AliasMeasurementUnit, it returns the same object.
-
- This function does not serve as a constructor for AliasMeasurementUnit, rather
- 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 `UnitDescriptorTypeError` if given descriptor cannot be translated
- to an AliasMeasurementUnit instance.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
-
- >>> bar = MeasurementUnit.from_descriptor(PressureUnit.BAR**(-1))
- >>> bar
- <PressureUnit: bar>
- """
- if isinstance(descriptor, Dimension) and isinstance(
- descriptor.unit, AliasMeasurementUnit
- ):
- return descriptor.unit
- if isinstance(descriptor, AliasMeasurementUnit):
- return descriptor
- raise UnitDescriptorTypeError(
- f"cannot create AliasMeasurementUnit from descriptor {descriptor}"
- )
+541
+542
+543
+544
+545
+546
+547
+548
+549
+550
+551
+552
+553
+554
+555
+556
+557
+558
+559
+560
| @staticmethod
+def from_descriptor(descriptor: UnitDescriptor) -> MeasurementUnit:
+ """
+ Create an AliasMeasurementUnit from given descriptor.
+ If descriptor is already an AliasMeasurementUnit, it returns the same object.
+
+ This function does not serve as a constructor for AliasMeasurementUnit, rather
+ 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 `UnitDescriptorTypeError` if given descriptor cannot be translated
+ to an AliasMeasurementUnit instance.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+
+ >>> bar = MeasurementUnit.from_descriptor(PressureUnit.BAR**(-1))
+ >>> bar
+ <PressureUnit: bar>
+ """
+ if isinstance(descriptor, Dimension) and isinstance(
+ descriptor.unit, AliasMeasurementUnit
+ ):
+ return descriptor.unit
+ if isinstance(descriptor, AliasMeasurementUnit):
+ return descriptor
+ raise UnitDescriptorTypeError(
+ f"cannot create AliasMeasurementUnit from descriptor {descriptor}"
+ )
|
@@ -2775,29 +2793,7 @@
Source code in src/property_utils/units/descriptors.py
- 1460
-1461
-1462
-1463
-1464
-1465
-1466
-1467
-1468
-1469
-1470
-1471
-1472
-1473
-1474
-1475
-1476
-1477
-1478
-1479
-1480
-1481
-1482
+ | @dataclass
-class CompositeDimension:
- """
- A CompositeDimension represents a measurement unit that is composed from other
- measurement units.
-
- Objects of this class can represent either multiplication or division between two
- Dimension objects.
-
- Create objects by multiplying and diving Dimension or MeasurementUnit objects.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class AmountUnit(MeasurementUnit):
- ... KILO_MOL = "kmol"
-
- >>> molal_volume_dimension = (LengthUnit.METER**3) / AmountUnit.KILO_MOL
- >>> molal_volume_dimension
- <CompositeDimension: (m^3) / kmol>
- """
-
- Default = TypeVar("Default") # default return type for `get` functions.
-
- numerator: List[Dimension] = field(default_factory=list)
- denominator: List[Dimension] = field(default_factory=list)
+1897
+1898
+1899
+1900
+1901
+1902
+1903
+1904
+1905
+1906
+1907
+1908
+1909
+1910
+1911
+1912
+1913
+1914
+1915
+1916
+1917
+1918
+1919
+1920
+1921
+1922
| @dataclass
+class CompositeDimension:
+ """
+ A CompositeDimension represents a measurement unit that is composed from other
+ measurement units.
- @staticmethod
- def from_descriptor(descriptor: UnitDescriptor) -> "CompositeDimension":
- """
- Create a CompositeDimension from given descriptor.
- If descriptor is already a CompositeDimension, it returns the same object.
-
- This function does not serve as a constructor for CompositeDimension, rather it
- is intended to be used to convert an unknown unit descriptor to a
- CompositeDimension.
-
- Raises `UnitDescriptorTypeError` if given descriptor cannot be translated
- to a CompositeDimension instance.
- """
- if not isinstance(descriptor, CompositeDimension):
- raise UnitDescriptorTypeError(
- f"cannot create CompositeDimension from descriptor {descriptor}"
- )
- return descriptor
+ Objects of this class can represent either multiplication or division between two
+ Dimension objects.
+
+ Create objects by multiplying and diving Dimension or MeasurementUnit objects.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class AmountUnit(MeasurementUnit):
+ ... KILO_MOL = "kmol"
+
+ >>> molal_volume_dimension = (LengthUnit.METER**3) / AmountUnit.KILO_MOL
+ >>> molal_volume_dimension
+ <CompositeDimension: (m^3) / kmol>
+ """
+
+ Default = TypeVar("Default") # default return type for `get` functions.
- def si(self) -> "CompositeDimension":
- """
- Returns this composite dimension in SI units.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
- ... RANKINE = "R"
- ... @classmethod
- ... def si(cls): return cls.KELVIN
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- ... FOOT = "ft"
- ... @classmethod
- ... def si(cls): return cls.METER
-
- >>> (TemperatureUnit.RANKINE / LengthUnit.FOOT**2).si()
- <CompositeDimension: K / (m^2)>
- """
- return CompositeDimension(
- [n.si() for n in self.numerator], [d.si() for d in self.denominator]
- )
-
- def isinstance(self, generic: GenericUnitDescriptor) -> bool:
- """
- Returns True if the CompositeDimension is an instance of the generic, False
- otherwise.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
+ numerator: List[Dimension] = field(default_factory=list)
+ denominator: List[Dimension] = field(default_factory=list)
+
+ @staticmethod
+ def from_descriptor(descriptor: UnitDescriptor) -> "CompositeDimension":
+ """
+ Create a CompositeDimension from given descriptor.
+ If descriptor is already a CompositeDimension, it returns the same object.
+
+ This function does not serve as a constructor for CompositeDimension, rather it
+ is intended to be used to convert an unknown unit descriptor to a
+ CompositeDimension.
+
+ Raises `UnitDescriptorTypeError` if given descriptor cannot be translated
+ to a CompositeDimension instance.
+ """
+ if not isinstance(descriptor, CompositeDimension):
+ raise UnitDescriptorTypeError(
+ f"cannot create CompositeDimension from descriptor {descriptor}"
+ )
+ return descriptor
+
+ def si(self) -> "CompositeDimension":
+ """
+ Returns this composite dimension in SI units.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+ ... RANKINE = "R"
+ ... @classmethod
+ ... def si(cls): return cls.KELVIN
>>> class LengthUnit(MeasurementUnit):
... METER = "m"
-
- >>> (TemperatureUnit.CELCIUS / LengthUnit.METER).isinstance(TemperatureUnit / LengthUnit)
- True
+ ... FOOT = "ft"
+ ... @classmethod
+ ... def si(cls): return cls.METER
- >>> (TemperatureUnit.CELCIUS * LengthUnit.METER).isinstance(TemperatureUnit**2)
- False
+ >>> (TemperatureUnit.RANKINE / LengthUnit.FOOT**2).si()
+ <CompositeDimension: K / (m^2)>
"""
- if not isinstance(generic, GenericCompositeDimension):
- return False
-
- return self.to_generic() == generic
-
- def isinstance_equivalent(self, generic: GenericUnitDescriptor) -> bool:
- """
- Returns True if the UnitDescriptor is an instance-equivalent of the generic,
- False otherwise.
-
- A unit descriptor is an instance-equivalent of a generic if the generic of the
- unit descriptor is equivalent to the generic.
+ return CompositeDimension(
+ [n.si() for n in self.numerator], [d.si() for d in self.denominator]
+ )
+
+ def isinstance(self, generic: GenericUnitDescriptor) -> bool:
+ """
+ Returns True if the CompositeDimension is an instance of the generic, False
+ otherwise.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
- Equivalence between generics is checked with the `is_equivalent` method.
-
- Examples:
- >>> class MassUnit(MeasurementUnit):
- ... KILO_GRAM = "kg"
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> class ForceUnit(AliasMeasurementUnit):
- ... NEWTON = "N"
- ... @classmethod
- ... def aliased_generic_descriptor(cls):
- ... return MassUnit * LengthUnit / (TimeUnit**2)
-
- >>> (MassUnit.KILO_GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)).isinstance_equivalent(ForceUnit)
- True
- """
- return self.to_generic().is_equivalent(generic)
-
- def to_generic(self) -> GenericCompositeDimension:
- """
- Create a generic descriptor from this CompositeDimension.
-
- Examples:
- >>> class AmountUnit(MeasurementUnit):
- ... MOL = "mol"
-
- >>> class MassUnit(MeasurementUnit):
- ... KILO_GRAM = "kg"
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> (TemperatureUnit.CELCIUS / LengthUnit.METER).isinstance(TemperatureUnit / LengthUnit)
+ True
+
+ >>> (TemperatureUnit.CELCIUS * LengthUnit.METER).isinstance(TemperatureUnit**2)
+ False
+ """
+ if not isinstance(generic, GenericCompositeDimension):
+ return False
+
+ return self.to_generic() == generic
+
+ def isinstance_equivalent(self, generic: GenericUnitDescriptor) -> bool:
+ """
+ Returns True if the UnitDescriptor is an instance-equivalent of the generic,
+ False otherwise.
+
+ A unit descriptor is an instance-equivalent of a generic if the generic of the
+ unit descriptor is equivalent to the generic.
+
+ Equivalence between generics is checked with the `is_equivalent` method.
+
+ Examples:
+ >>> class MassUnit(MeasurementUnit):
+ ... KILO_GRAM = "kg"
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
- >>> (AmountUnit.MOL / MassUnit.KILO_GRAM).to_generic()
- <GenericCompositeDimension: AmountUnit / MassUnit>
- """
- return GenericCompositeDimension(
- numerator=[n.to_generic() for n in self.numerator],
- denominator=[d.to_generic() for d in self.denominator],
- )
-
- def get_numerator(
- self,
- generic: Union[MeasurementUnitType, GenericDimension],
- default: Optional[Default] = None,
- ) -> Union[Dimension, Optional[Default]]:
- """
- Get a dimension from the numerator. If the dimension is not found it returns
- the default.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
- >>> composite.get_numerator(TemperatureUnit)
- <Dimension: K>
- >>> composite.get_numerator(LengthUnit, "default")
- 'default'
- """
- for n in self.numerator:
- if n.isinstance(generic):
- return n
- return default
-
- def get_denominator(
- self,
- generic: Union[MeasurementUnitType, GenericDimension],
- default: Optional[Default] = None,
- ) -> Union[Dimension, Optional[Default]]:
- """
- Get a dimension from the denominator. If the dimension is not found it returns
- the default.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
- >>> composite.get_denominator(LengthUnit**3)
- <Dimension: m^3>
- >>> composite.get_denominator(LengthUnit, "default")
- 'default'
- """
- for d in self.denominator:
- if d.isinstance(generic):
- return d
- return default
-
- def simplify(self) -> None:
- """
- Simplify the composite by merging common dimensions.
+ >>> class ForceUnit(AliasMeasurementUnit):
+ ... NEWTON = "N"
+ ... @classmethod
+ ... def aliased_generic_descriptor(cls):
+ ... return MassUnit * LengthUnit / (TimeUnit**2)
+
+ >>> (MassUnit.KILO_GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)).isinstance_equivalent(ForceUnit)
+ True
+ """
+ return self.to_generic().is_equivalent(generic)
+
+ def to_generic(self) -> GenericCompositeDimension:
+ """
+ Create a generic descriptor from this CompositeDimension.
+
+ Examples:
+ >>> class AmountUnit(MeasurementUnit):
+ ... MOL = "mol"
+
+ >>> class MassUnit(MeasurementUnit):
+ ... KILO_GRAM = "kg"
+
+ >>> (AmountUnit.MOL / MassUnit.KILO_GRAM).to_generic()
+ <GenericCompositeDimension: AmountUnit / MassUnit>
+ """
+ return GenericCompositeDimension(
+ numerator=[n.to_generic() for n in self.numerator],
+ denominator=[d.to_generic() for d in self.denominator],
+ )
+
+ def get_numerator(
+ self,
+ generic: Union[MeasurementUnitType, GenericDimension],
+ default: Optional[Default] = None,
+ ) -> Union[Dimension, Optional[Default]]:
+ """
+ Get a dimension from the numerator. If the dimension is not found it returns
+ the default.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
+ >>> composite.get_numerator(TemperatureUnit)
+ <Dimension: K>
+ >>> composite.get_numerator(LengthUnit, "default")
+ 'default'
+ """
+ for n in self.numerator:
+ if n.isinstance(generic):
+ return n
+ return default
+
+ def get_denominator(
+ self,
+ generic: Union[MeasurementUnitType, GenericDimension],
+ default: Optional[Default] = None,
+ ) -> Union[Dimension, Optional[Default]]:
+ """
+ Get a dimension from the denominator. If the dimension is not found it returns
+ the default.
Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
- ... PASCAL = "Pa"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
- >>> composite
- <CompositeDimension: (bar^-2) / (K^-1)>
- >>> composite.simplify()
- >>> composite
- <CompositeDimension: K / (bar^2)>
-
- >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
- >>> composite
- <CompositeDimension: Pa * Pa * m / s>
- >>> composite.simplify()
- >>> composite
- <CompositeDimension: (Pa^2) * m / s>
- """
- exponents: Dict[MeasurementUnit, float] = {}
- for n in self.numerator:
- if n.unit in exponents:
- exponents[n.unit] += n.power
- else:
- exponents[n.unit] = n.power
-
- for d in self.denominator:
- if d.unit in exponents:
- exponents[d.unit] -= d.power
- else:
- exponents[d.unit] = 0 - d.power
-
- numerator = []
- denominator = []
- for unit, exponent in exponents.items():
- if exponent > 0:
- numerator.append(Dimension(unit) ** exponent)
- elif exponent < 0:
- denominator.append(Dimension(unit) ** abs(exponent))
-
- self.numerator = numerator
- self.denominator = denominator
-
- def simplified(self) -> "CompositeDimension":
- """
- Returns a simplified version of this composite dimension as a new object.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
- ... PASCAL = "Pa"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
- >>> composite
- <CompositeDimension: (bar^-2) / (K^-1)>
- >>> composite.simplified()
- <CompositeDimension: K / (bar^2)>
-
- >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
- >>> composite
- <CompositeDimension: Pa * Pa * m / s>
- >>> composite.simplified()
- <CompositeDimension: (Pa^2) * m / s>
- """
- copy = replace(self)
- copy.simplify()
- return copy
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
+ >>> composite.get_denominator(LengthUnit**3)
+ <Dimension: m^3>
+ >>> composite.get_denominator(LengthUnit, "default")
+ 'default'
+ """
+ for d in self.denominator:
+ if d.isinstance(generic):
+ return d
+ return default
+
+ def simplify(self) -> None:
+ """
+ Simplify the composite by merging common dimensions.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+ ... PASCAL = "Pa"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
+ >>> composite
+ <CompositeDimension: (bar^-2) / (K^-1)>
+ >>> composite.simplify()
+ >>> composite
+ <CompositeDimension: K / (bar^2)>
+
+ >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
+ >>> composite
+ <CompositeDimension: Pa * Pa * m / s>
+ >>> composite.simplify()
+ >>> composite
+ <CompositeDimension: (Pa^2) * m / s>
+ """
+ exponents: Dict[MeasurementUnit, float] = {}
+ for n in self.numerator:
+ if n.unit in exponents:
+ exponents[n.unit] += n.power
+ else:
+ exponents[n.unit] = n.power
+
+ for d in self.denominator:
+ if d.unit in exponents:
+ exponents[d.unit] -= d.power
+ else:
+ exponents[d.unit] = 0 - d.power
+
+ numerator = []
+ denominator = []
+ for unit, exponent in exponents.items():
+ if unit.is_non_dimensional():
+ continue # do not add non dimensional units to the simplified composite
+
+ if exponent > 0:
+ numerator.append(Dimension(unit) ** exponent)
+ elif exponent < 0:
+ denominator.append(Dimension(unit) ** abs(exponent))
+
+ self.numerator = numerator
+ self.denominator = denominator
+
+ def simplified(self) -> "CompositeDimension":
+ """
+ Returns a simplified version of this composite dimension as a new object.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+ ... PASCAL = "Pa"
- def inverse(self) -> "CompositeDimension":
- """
- Create a composite with inverse units.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> (LengthUnit.METER / TimeUnit.SECOND).inverse()
- <CompositeDimension: s / m>
- """
- return CompositeDimension(self._denominator_copy(), self._numerator_copy())
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
+ >>> composite
+ <CompositeDimension: (bar^-2) / (K^-1)>
+ >>> composite.simplified()
+ <CompositeDimension: K / (bar^2)>
- def has_no_units(self) -> bool:
- """
- Returns True if the composite dimension does not have any units, False otherwise.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> CompositeDimension().has_no_units()
- True
- >>> CompositeDimension([LengthUnit.METER]).has_no_units()
- False
- """
- return len(self.denominator) == 0 and len(self.numerator) == 0
-
- def _numerator_copy(self) -> List[Dimension]:
- return [replace(n) for n in self.numerator]
-
- def _denominator_copy(self) -> List[Dimension]:
- return [replace(d) for d in self.denominator]
-
- def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
- """
- Defines multiplication between CompositeDimension(s) and other unit descriptors.
+ >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
+ >>> composite
+ <CompositeDimension: Pa * Pa * m / s>
+ >>> composite.simplified()
+ <CompositeDimension: (Pa^2) * m / s>
+ """
+ copy = replace(self)
+ copy.simplify()
+ return copy
+
+ def inverse(self) -> "CompositeDimension":
+ """
+ Create a composite with inverse units.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> (LengthUnit.METER / TimeUnit.SECOND).inverse()
+ <CompositeDimension: s / m>
+ """
+ return CompositeDimension(self._denominator_copy(), self._numerator_copy())
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
+ def has_no_units(self) -> bool:
+ """
+ Returns True if the composite dimension does not have any units, False otherwise.
+
+ Examples:
>>> class LengthUnit(MeasurementUnit):
- ... CENTI_METER = "cm"
- >>> (TemperatureUnit.CELCIUS / LengthUnit.CENTI_METER) * TimeUnit.SECOND
- <CompositeDimension: C * s / cm>
- """
- numerator = self.numerator.copy()
- denominator = self.denominator.copy()
- if isinstance(descriptor, CompositeDimension):
- numerator.extend(descriptor.numerator)
- denominator.extend(descriptor.denominator)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, Dimension):
- numerator.append(descriptor)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, MeasurementUnit):
- numerator.append(Dimension(descriptor))
- return CompositeDimension(numerator=numerator, denominator=denominator)
- raise DescriptorBinaryOperationError(
- f"cannot multiply {self} with {descriptor}. "
- )
-
- def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
- """
- Defines multiplication between CompositeDimension(s) and other unit descriptors.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
- >>> class LengthUnit(MeasurementUnit):
- ... CENTI_METER = "cm"
- >>> (TemperatureUnit.CELCIUS * LengthUnit.CENTI_METER) / TimeUnit.SECOND
- <CompositeDimension: C * cm / s>
- """
- numerator = self.numerator.copy()
- denominator = self.denominator.copy()
- if isinstance(descriptor, CompositeDimension):
- numerator.extend(descriptor.denominator)
- denominator.extend(descriptor.numerator)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, Dimension):
- denominator.append(descriptor)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, MeasurementUnit):
- denominator.append(Dimension(descriptor))
- return CompositeDimension(numerator=numerator, denominator=denominator)
- raise DescriptorBinaryOperationError(
- f"cannot divide {self} with {descriptor}. "
- )
-
- def __pow__(self, power: float) -> "CompositeDimension":
- """
- Defines exponentiation for CompositeDimension(s).
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... HOUR = "hr"
-
- >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR)**2
- <CompositeDimension: (C^2) / (hr^2)>
- """
- if not isinstance(power, (float, int)):
- raise DescriptorExponentError(
- f"invalid exponent: {{ value: {power}, type: {type(power)} }};"
- " expected float or int. "
- )
- numerator = [n**power for n in self._numerator_copy()]
- denominator = [d**power for d in self._denominator_copy()]
- return CompositeDimension(numerator, denominator)
-
- def __eq__(self, dimension) -> bool:
- """
- Defines equality for CompositeDimension(s).
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... HOUR = "hr"
- >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR) != (TimeUnit.HOUR / TemperatureUnit.CELCIUS)
- True
- """
- if not isinstance(dimension, CompositeDimension):
- return False
- return Counter(self.numerator) == Counter(dimension.numerator) and (
- Counter(self.denominator) == Counter(dimension.denominator)
- )
-
- def __hash__(self) -> int:
- return hash(str(self))
-
- def __str__(self):
- numerators = " * ".join(sorted([str(n) for n in self.numerator]))
- denominators = " / ".join(sorted([str(d) for d in self.denominator]))
- if len(denominators) > 0:
- denominators = " / " + denominators
- return numerators + denominators
-
- def __repr__(self) -> str:
- numerators = " * ".join(sorted([str(n) for n in self.numerator]))
- denominators = " / ".join(sorted([str(d) for d in self.denominator]))
- if len(denominators) > 0:
- denominators = " / " + denominators
- return f"<CompositeDimension: {numerators + denominators}>"
+ ... METER = "m"
+
+ >>> CompositeDimension().has_no_units()
+ True
+ >>> CompositeDimension([LengthUnit.METER]).has_no_units()
+ False
+ """
+ return len(self.denominator) == 0 and len(self.numerator) == 0
+
+ def _numerator_copy(self) -> List[Dimension]:
+ return [replace(n) for n in self.numerator]
+
+ def _denominator_copy(self) -> List[Dimension]:
+ return [replace(d) for d in self.denominator]
+
+ def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
+ """
+ Defines multiplication between CompositeDimension(s) and other unit descriptors.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+ >>> class LengthUnit(MeasurementUnit):
+ ... CENTI_METER = "cm"
+ >>> (TemperatureUnit.CELCIUS / LengthUnit.CENTI_METER) * TimeUnit.SECOND
+ <CompositeDimension: C * s / cm>
+ """
+ numerator = self.numerator.copy()
+ denominator = self.denominator.copy()
+ if isinstance(descriptor, CompositeDimension):
+ numerator.extend(descriptor.numerator)
+ denominator.extend(descriptor.denominator)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, Dimension):
+ numerator.append(descriptor)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, MeasurementUnit):
+ numerator.append(Dimension(descriptor))
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ raise DescriptorBinaryOperationError(
+ f"cannot multiply {self} with {descriptor}. "
+ )
+
+ def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
+ """
+ Defines multiplication between CompositeDimension(s) and other unit descriptors.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+ >>> class LengthUnit(MeasurementUnit):
+ ... CENTI_METER = "cm"
+ >>> (TemperatureUnit.CELCIUS * LengthUnit.CENTI_METER) / TimeUnit.SECOND
+ <CompositeDimension: C * cm / s>
+ """
+ numerator = self.numerator.copy()
+ denominator = self.denominator.copy()
+ if isinstance(descriptor, CompositeDimension):
+ numerator.extend(descriptor.denominator)
+ denominator.extend(descriptor.numerator)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, Dimension):
+ denominator.append(descriptor)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, MeasurementUnit):
+ denominator.append(Dimension(descriptor))
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ raise DescriptorBinaryOperationError(
+ f"cannot divide {self} with {descriptor}. "
+ )
+
+ def __pow__(self, power: float) -> "CompositeDimension":
+ """
+ Defines exponentiation for CompositeDimension(s).
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... HOUR = "hr"
+
+ >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR)**2
+ <CompositeDimension: (C^2) / (hr^2)>
+ """
+ if not isinstance(power, (float, int)):
+ raise DescriptorExponentError(
+ f"invalid exponent: {{ value: {power}, type: {type(power)} }};"
+ " expected float or int. "
+ )
+ numerator = [n**power for n in self._numerator_copy()]
+ denominator = [d**power for d in self._denominator_copy()]
+ return CompositeDimension(numerator, denominator)
+
+ def __eq__(self, dimension) -> bool:
+ """
+ Defines equality for CompositeDimension(s).
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... HOUR = "hr"
+ >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR) != (TimeUnit.HOUR / TemperatureUnit.CELCIUS)
+ True
+ """
+ if not isinstance(dimension, CompositeDimension):
+ return False
+ return Counter(self.numerator) == Counter(dimension.numerator) and (
+ Counter(self.denominator) == Counter(dimension.denominator)
+ )
+
+ def __hash__(self) -> int:
+ return hash(str(self))
+
+ def __str__(self):
+ numerators = " * ".join(sorted([str(n) for n in self.numerator]))
+ denominators = " / ".join(sorted([str(d) for d in self.denominator]))
+ if len(denominators) > 0:
+ denominators = " / " + denominators
+ return numerators + denominators
+
+ def __repr__(self) -> str:
+ numerators = " * ".join(sorted([str(n) for n in self.numerator]))
+ denominators = " / ".join(sorted([str(d) for d in self.denominator]))
+ if len(denominators) > 0:
+ denominators = " / " + denominators
+ return f"<CompositeDimension: {numerators + denominators}>"
|
@@ -3690,39 +3714,39 @@
Source code in src/property_utils/units/descriptors.py
- | def __eq__(self, dimension) -> bool:
- """
- Defines equality for CompositeDimension(s).
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... HOUR = "hr"
- >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR) != (TimeUnit.HOUR / TemperatureUnit.CELCIUS)
- True
- """
- if not isinstance(dimension, CompositeDimension):
- return False
- return Counter(self.numerator) == Counter(dimension.numerator) and (
- Counter(self.denominator) == Counter(dimension.denominator)
- )
+ | def __eq__(self, dimension) -> bool:
+ """
+ Defines equality for CompositeDimension(s).
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... HOUR = "hr"
+ >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR) != (TimeUnit.HOUR / TemperatureUnit.CELCIUS)
+ True
+ """
+ if not isinstance(dimension, CompositeDimension):
+ return False
+ return Counter(self.numerator) == Counter(dimension.numerator) and (
+ Counter(self.denominator) == Counter(dimension.denominator)
+ )
|
@@ -3756,63 +3780,63 @@
Source code in src/property_utils/units/descriptors.py
- 1782
-1783
-1784
-1785
-1786
-1787
-1788
-1789
-1790
-1791
-1792
-1793
-1794
-1795
-1796
-1797
-1798
-1799
-1800
-1801
-1802
-1803
-1804
-1805
-1806
-1807
+ | def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
- """
- Defines multiplication between CompositeDimension(s) and other unit descriptors.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
- >>> class LengthUnit(MeasurementUnit):
- ... CENTI_METER = "cm"
- >>> (TemperatureUnit.CELCIUS / LengthUnit.CENTI_METER) * TimeUnit.SECOND
- <CompositeDimension: C * s / cm>
- """
- numerator = self.numerator.copy()
- denominator = self.denominator.copy()
- if isinstance(descriptor, CompositeDimension):
- numerator.extend(descriptor.numerator)
- denominator.extend(descriptor.denominator)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, Dimension):
- numerator.append(descriptor)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, MeasurementUnit):
- numerator.append(Dimension(descriptor))
- return CompositeDimension(numerator=numerator, denominator=denominator)
- raise DescriptorBinaryOperationError(
- f"cannot multiply {self} with {descriptor}. "
- )
+1810
+1811
+1812
+1813
+1814
+1815
+1816
+1817
+1818
+1819
+1820
+1821
+1822
+1823
+1824
+1825
+1826
+1827
+1828
+1829
+1830
+1831
+1832
+1833
+1834
+1835
| def __mul__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
+ """
+ Defines multiplication between CompositeDimension(s) and other unit descriptors.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+ >>> class LengthUnit(MeasurementUnit):
+ ... CENTI_METER = "cm"
+ >>> (TemperatureUnit.CELCIUS / LengthUnit.CENTI_METER) * TimeUnit.SECOND
+ <CompositeDimension: C * s / cm>
+ """
+ numerator = self.numerator.copy()
+ denominator = self.denominator.copy()
+ if isinstance(descriptor, CompositeDimension):
+ numerator.extend(descriptor.numerator)
+ denominator.extend(descriptor.denominator)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, Dimension):
+ numerator.append(descriptor)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, MeasurementUnit):
+ numerator.append(Dimension(descriptor))
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ raise DescriptorBinaryOperationError(
+ f"cannot multiply {self} with {descriptor}. "
+ )
|
@@ -3845,47 +3869,47 @@
Source code in src/property_utils/units/descriptors.py
- | def __pow__(self, power: float) -> "CompositeDimension":
- """
- Defines exponentiation for CompositeDimension(s).
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... HOUR = "hr"
-
- >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR)**2
- <CompositeDimension: (C^2) / (hr^2)>
- """
- if not isinstance(power, (float, int)):
- raise DescriptorExponentError(
- f"invalid exponent: {{ value: {power}, type: {type(power)} }};"
- " expected float or int. "
- )
- numerator = [n**power for n in self._numerator_copy()]
- denominator = [d**power for d in self._denominator_copy()]
- return CompositeDimension(numerator, denominator)
+ | def __pow__(self, power: float) -> "CompositeDimension":
+ """
+ Defines exponentiation for CompositeDimension(s).
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... HOUR = "hr"
+
+ >>> (TemperatureUnit.CELCIUS / TimeUnit.HOUR)**2
+ <CompositeDimension: (C^2) / (hr^2)>
+ """
+ if not isinstance(power, (float, int)):
+ raise DescriptorExponentError(
+ f"invalid exponent: {{ value: {power}, type: {type(power)} }};"
+ " expected float or int. "
+ )
+ numerator = [n**power for n in self._numerator_copy()]
+ denominator = [d**power for d in self._denominator_copy()]
+ return CompositeDimension(numerator, denominator)
|
@@ -3919,64 +3943,64 @@
Source code in src/property_utils/units/descriptors.py
- 1812
-1813
-1814
-1815
-1816
-1817
-1818
-1819
-1820
-1821
-1822
-1823
-1824
-1825
-1826
-1827
-1828
-1829
-1830
-1831
-1832
-1833
-1834
-1835
-1836
-1837
+ | def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
- """
- Defines multiplication between CompositeDimension(s) and other unit descriptors.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
- >>> class LengthUnit(MeasurementUnit):
- ... CENTI_METER = "cm"
- >>> (TemperatureUnit.CELCIUS * LengthUnit.CENTI_METER) / TimeUnit.SECOND
- <CompositeDimension: C * cm / s>
- """
- numerator = self.numerator.copy()
- denominator = self.denominator.copy()
- if isinstance(descriptor, CompositeDimension):
- numerator.extend(descriptor.denominator)
- denominator.extend(descriptor.numerator)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, Dimension):
- denominator.append(descriptor)
- return CompositeDimension(numerator=numerator, denominator=denominator)
- if isinstance(descriptor, MeasurementUnit):
- denominator.append(Dimension(descriptor))
- return CompositeDimension(numerator=numerator, denominator=denominator)
- raise DescriptorBinaryOperationError(
- f"cannot divide {self} with {descriptor}. "
- )
-
|
+1840
+1841
+1842
+1843
+1844
+1845
+1846
+1847
+1848
+1849
+1850
+1851
+1852
+1853
+1854
+1855
+1856
+1857
+1858
+1859
+1860
+1861
+1862
+1863
+1864
+1865 | def __truediv__(self, descriptor: "UnitDescriptor") -> "CompositeDimension":
+ """
+ Defines multiplication between CompositeDimension(s) and other unit descriptors.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+ >>> class LengthUnit(MeasurementUnit):
+ ... CENTI_METER = "cm"
+ >>> (TemperatureUnit.CELCIUS * LengthUnit.CENTI_METER) / TimeUnit.SECOND
+ <CompositeDimension: C * cm / s>
+ """
+ numerator = self.numerator.copy()
+ denominator = self.denominator.copy()
+ if isinstance(descriptor, CompositeDimension):
+ numerator.extend(descriptor.denominator)
+ denominator.extend(descriptor.numerator)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, Dimension):
+ denominator.append(descriptor)
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ if isinstance(descriptor, MeasurementUnit):
+ denominator.append(Dimension(descriptor))
+ return CompositeDimension(numerator=numerator, denominator=denominator)
+ raise DescriptorBinaryOperationError(
+ f"cannot divide {self} with {descriptor}. "
+ )
+
|
@@ -4007,41 +4031,41 @@
Source code in src/property_utils/units/descriptors.py
- | @staticmethod
-def from_descriptor(descriptor: UnitDescriptor) -> "CompositeDimension":
- """
- Create a CompositeDimension from given descriptor.
- If descriptor is already a CompositeDimension, it returns the same object.
-
- This function does not serve as a constructor for CompositeDimension, rather it
- is intended to be used to convert an unknown unit descriptor to a
- CompositeDimension.
-
- Raises `UnitDescriptorTypeError` if given descriptor cannot be translated
- to a CompositeDimension instance.
- """
- if not isinstance(descriptor, CompositeDimension):
- raise UnitDescriptorTypeError(
- f"cannot create CompositeDimension from descriptor {descriptor}"
- )
- return descriptor
+ | @staticmethod
+def from_descriptor(descriptor: UnitDescriptor) -> "CompositeDimension":
+ """
+ Create a CompositeDimension from given descriptor.
+ If descriptor is already a CompositeDimension, it returns the same object.
+
+ This function does not serve as a constructor for CompositeDimension, rather it
+ is intended to be used to convert an unknown unit descriptor to a
+ CompositeDimension.
+
+ Raises `UnitDescriptorTypeError` if given descriptor cannot be translated
+ to a CompositeDimension instance.
+ """
+ if not isinstance(descriptor, CompositeDimension):
+ raise UnitDescriptorTypeError(
+ f"cannot create CompositeDimension from descriptor {descriptor}"
+ )
+ return descriptor
|
@@ -4079,57 +4103,57 @@
Source code in src/property_utils/units/descriptors.py
- 1629
-1630
-1631
-1632
-1633
-1634
-1635
-1636
-1637
-1638
-1639
-1640
-1641
-1642
-1643
-1644
-1645
-1646
-1647
-1648
-1649
-1650
-1651
+ | def get_denominator(
- self,
- generic: Union[MeasurementUnitType, GenericDimension],
- default: Optional[Default] = None,
-) -> Union[Dimension, Optional[Default]]:
- """
- Get a dimension from the denominator. If the dimension is not found it returns
- the default.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
- >>> composite.get_denominator(LengthUnit**3)
- <Dimension: m^3>
- >>> composite.get_denominator(LengthUnit, "default")
- 'default'
- """
- for d in self.denominator:
- if d.isinstance(generic):
- return d
- return default
+1654
+1655
+1656
+1657
+1658
+1659
+1660
+1661
+1662
+1663
+1664
+1665
+1666
+1667
+1668
+1669
+1670
+1671
+1672
+1673
+1674
+1675
+1676
| def get_denominator(
+ self,
+ generic: Union[MeasurementUnitType, GenericDimension],
+ default: Optional[Default] = None,
+) -> Union[Dimension, Optional[Default]]:
+ """
+ Get a dimension from the denominator. If the dimension is not found it returns
+ the default.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
+ >>> composite.get_denominator(LengthUnit**3)
+ <Dimension: m^3>
+ >>> composite.get_denominator(LengthUnit, "default")
+ 'default'
+ """
+ for d in self.denominator:
+ if d.isinstance(generic):
+ return d
+ return default
|
@@ -4167,57 +4191,57 @@
Source code in src/property_utils/units/descriptors.py
- 1602
-1603
-1604
-1605
-1606
-1607
-1608
-1609
-1610
-1611
-1612
-1613
-1614
-1615
-1616
-1617
-1618
-1619
-1620
-1621
-1622
-1623
-1624
+ | def get_numerator(
- self,
- generic: Union[MeasurementUnitType, GenericDimension],
- default: Optional[Default] = None,
-) -> Union[Dimension, Optional[Default]]:
- """
- Get a dimension from the numerator. If the dimension is not found it returns
- the default.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
- >>> composite.get_numerator(TemperatureUnit)
- <Dimension: K>
- >>> composite.get_numerator(LengthUnit, "default")
- 'default'
- """
- for n in self.numerator:
- if n.isinstance(generic):
- return n
- return default
+1627
+1628
+1629
+1630
+1631
+1632
+1633
+1634
+1635
+1636
+1637
+1638
+1639
+1640
+1641
+1642
+1643
+1644
+1645
+1646
+1647
+1648
+1649
| def get_numerator(
+ self,
+ generic: Union[MeasurementUnitType, GenericDimension],
+ default: Optional[Default] = None,
+) -> Union[Dimension, Optional[Default]]:
+ """
+ Get a dimension from the numerator. If the dimension is not found it returns
+ the default.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> composite = TemperatureUnit.KELVIN / (LengthUnit.METER**3)
+ >>> composite.get_numerator(TemperatureUnit)
+ <Dimension: K>
+ >>> composite.get_numerator(LengthUnit, "default")
+ 'default'
+ """
+ for n in self.numerator:
+ if n.isinstance(generic):
+ return n
+ return default
|
@@ -4250,33 +4274,33 @@
Source code in src/property_utils/units/descriptors.py
- | def has_no_units(self) -> bool:
- """
- Returns True if the composite dimension does not have any units, False otherwise.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> CompositeDimension().has_no_units()
- True
- >>> CompositeDimension([LengthUnit.METER]).has_no_units()
- False
- """
- return len(self.denominator) == 0 and len(self.numerator) == 0
+ | def has_no_units(self) -> bool:
+ """
+ Returns True if the composite dimension does not have any units, False otherwise.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> CompositeDimension().has_no_units()
+ True
+ >>> CompositeDimension([LengthUnit.METER]).has_no_units()
+ False
+ """
+ return len(self.denominator) == 0 and len(self.numerator) == 0
|
@@ -4309,33 +4333,33 @@
Source code in src/property_utils/units/descriptors.py
- | def inverse(self) -> "CompositeDimension":
- """
- Create a composite with inverse units.
-
- Examples:
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> (LengthUnit.METER / TimeUnit.SECOND).inverse()
- <CompositeDimension: s / m>
- """
- return CompositeDimension(self._denominator_copy(), self._numerator_copy())
+ | def inverse(self) -> "CompositeDimension":
+ """
+ Create a composite with inverse units.
+
+ Examples:
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> (LengthUnit.METER / TimeUnit.SECOND).inverse()
+ <CompositeDimension: s / m>
+ """
+ return CompositeDimension(self._denominator_copy(), self._numerator_copy())
|
@@ -4373,49 +4397,49 @@
Source code in src/property_utils/units/descriptors.py
- | def isinstance(self, generic: GenericUnitDescriptor) -> bool:
- """
- Returns True if the CompositeDimension is an instance of the generic, False
- otherwise.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... CELCIUS = "C"
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> (TemperatureUnit.CELCIUS / LengthUnit.METER).isinstance(TemperatureUnit / LengthUnit)
- True
-
- >>> (TemperatureUnit.CELCIUS * LengthUnit.METER).isinstance(TemperatureUnit**2)
- False
- """
- if not isinstance(generic, GenericCompositeDimension):
- return False
-
- return self.to_generic() == generic
+ | def isinstance(self, generic: GenericUnitDescriptor) -> bool:
+ """
+ Returns True if the CompositeDimension is an instance of the generic, False
+ otherwise.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... CELCIUS = "C"
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> (TemperatureUnit.CELCIUS / LengthUnit.METER).isinstance(TemperatureUnit / LengthUnit)
+ True
+
+ >>> (TemperatureUnit.CELCIUS * LengthUnit.METER).isinstance(TemperatureUnit**2)
+ False
+ """
+ if not isinstance(generic, GenericCompositeDimension):
+ return False
+
+ return self.to_generic() == generic
|
@@ -4460,61 +4484,61 @@
Source code in src/property_utils/units/descriptors.py
- 1554
-1555
-1556
-1557
-1558
-1559
-1560
-1561
-1562
-1563
-1564
-1565
-1566
-1567
-1568
-1569
-1570
-1571
-1572
-1573
-1574
-1575
-1576
+ | def isinstance_equivalent(self, generic: GenericUnitDescriptor) -> bool:
- """
- Returns True if the UnitDescriptor is an instance-equivalent of the generic,
- False otherwise.
-
- A unit descriptor is an instance-equivalent of a generic if the generic of the
- unit descriptor is equivalent to the generic.
-
- Equivalence between generics is checked with the `is_equivalent` method.
-
- Examples:
- >>> class MassUnit(MeasurementUnit):
- ... KILO_GRAM = "kg"
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> class ForceUnit(AliasMeasurementUnit):
- ... NEWTON = "N"
- ... @classmethod
- ... def aliased_generic_descriptor(cls):
- ... return MassUnit * LengthUnit / (TimeUnit**2)
-
- >>> (MassUnit.KILO_GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)).isinstance_equivalent(ForceUnit)
- True
- """
- return self.to_generic().is_equivalent(generic)
+1581
+1582
+1583
+1584
+1585
+1586
+1587
+1588
+1589
+1590
+1591
+1592
+1593
+1594
+1595
+1596
+1597
+1598
+1599
+1600
+1601
+1602
+1603
| def isinstance_equivalent(self, generic: GenericUnitDescriptor) -> bool:
+ """
+ Returns True if the UnitDescriptor is an instance-equivalent of the generic,
+ False otherwise.
+
+ A unit descriptor is an instance-equivalent of a generic if the generic of the
+ unit descriptor is equivalent to the generic.
+
+ Equivalence between generics is checked with the `is_equivalent` method.
+
+ Examples:
+ >>> class MassUnit(MeasurementUnit):
+ ... KILO_GRAM = "kg"
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> class ForceUnit(AliasMeasurementUnit):
+ ... NEWTON = "N"
+ ... @classmethod
+ ... def aliased_generic_descriptor(cls):
+ ... return MassUnit * LengthUnit / (TimeUnit**2)
+
+ >>> (MassUnit.KILO_GRAM * LengthUnit.METER / (TimeUnit.SECOND**2)).isinstance_equivalent(ForceUnit)
+ True
+ """
+ return self.to_generic().is_equivalent(generic)
|
@@ -4554,51 +4578,51 @@
Source code in src/property_utils/units/descriptors.py
- | def si(self) -> "CompositeDimension":
- """
- Returns this composite dimension in SI units.
-
- Examples:
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
- ... RANKINE = "R"
- ... @classmethod
- ... def si(cls): return cls.KELVIN
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
- ... FOOT = "ft"
- ... @classmethod
- ... def si(cls): return cls.METER
-
- >>> (TemperatureUnit.RANKINE / LengthUnit.FOOT**2).si()
- <CompositeDimension: K / (m^2)>
- """
- return CompositeDimension(
- [n.si() for n in self.numerator], [d.si() for d in self.denominator]
- )
+ | def si(self) -> "CompositeDimension":
+ """
+ Returns this composite dimension in SI units.
+
+ Examples:
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+ ... RANKINE = "R"
+ ... @classmethod
+ ... def si(cls): return cls.KELVIN
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+ ... FOOT = "ft"
+ ... @classmethod
+ ... def si(cls): return cls.METER
+
+ >>> (TemperatureUnit.RANKINE / LengthUnit.FOOT**2).si()
+ <CompositeDimension: K / (m^2)>
+ """
+ return CompositeDimension(
+ [n.si() for n in self.numerator], [d.si() for d in self.denominator]
+ )
|
@@ -4648,71 +4672,71 @@
Source code in src/property_utils/units/descriptors.py
- 1712
-1713
-1714
-1715
-1716
-1717
-1718
-1719
-1720
-1721
-1722
-1723
-1724
-1725
-1726
-1727
-1728
-1729
-1730
-1731
-1732
-1733
-1734
-1735
-1736
-1737
+ | def simplified(self) -> "CompositeDimension":
- """
- Returns a simplified version of this composite dimension as a new object.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
- ... PASCAL = "Pa"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
- >>> composite
- <CompositeDimension: (bar^-2) / (K^-1)>
- >>> composite.simplified()
- <CompositeDimension: K / (bar^2)>
-
- >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
- >>> composite
- <CompositeDimension: Pa * Pa * m / s>
- >>> composite.simplified()
- <CompositeDimension: (Pa^2) * m / s>
- """
- copy = replace(self)
- copy.simplify()
- return copy
+1744
+1745
+1746
+1747
+1748
+1749
+1750
+1751
+1752
+1753
+1754
+1755
+1756
+1757
+1758
+1759
+1760
+1761
+1762
+1763
+1764
+1765
+1766
+1767
+1768
+1769
| def simplified(self) -> "CompositeDimension":
+ """
+ Returns a simplified version of this composite dimension as a new object.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+ ... PASCAL = "Pa"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
+ >>> composite
+ <CompositeDimension: (bar^-2) / (K^-1)>
+ >>> composite.simplified()
+ <CompositeDimension: K / (bar^2)>
+
+ >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
+ >>> composite
+ <CompositeDimension: Pa * Pa * m / s>
+ >>> composite.simplified()
+ <CompositeDimension: (Pa^2) * m / s>
+ """
+ copy = replace(self)
+ copy.simplify()
+ return copy
|
@@ -4764,29 +4788,7 @@
Source code in src/property_utils/units/descriptors.py
- 1656
-1657
-1658
-1659
-1660
-1661
-1662
-1663
-1664
-1665
-1666
-1667
-1668
-1669
-1670
-1671
-1672
-1673
-1674
-1675
-1676
-1677
-1678
+ | def simplify(self) -> None:
- """
- Simplify the composite by merging common dimensions.
-
- Examples:
- >>> class PressureUnit(AliasMeasurementUnit):
- ... BAR = "bar"
- ... PASCAL = "Pa"
-
- >>> class TemperatureUnit(MeasurementUnit):
- ... KELVIN = "K"
-
- >>> class LengthUnit(MeasurementUnit):
- ... METER = "m"
-
- >>> class TimeUnit(MeasurementUnit):
- ... SECOND = "s"
-
- >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
- >>> composite
- <CompositeDimension: (bar^-2) / (K^-1)>
- >>> composite.simplify()
- >>> composite
- <CompositeDimension: K / (bar^2)>
-
- >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
- >>> composite
- <CompositeDimension: Pa * Pa * m / s>
- >>> composite.simplify()
- >>> composite
- <CompositeDimension: (Pa^2) * m / s>
- """
- exponents: Dict[MeasurementUnit, float] = {}
- for n in self.numerator:
- if n.unit in exponents:
- exponents[n.unit] += n.power
- else:
- exponents[n.unit] = n.power
-
- for d in self.denominator:
- if d.unit in exponents:
- exponents[d.unit] -= d.power
- else:
- exponents[d.unit] = 0 - d.power
-
- numerator = []
- denominator = []
- for unit, exponent in exponents.items():
- if exponent > 0:
- numerator.append(Dimension(unit) ** exponent)
- elif exponent < 0:
- denominator.append(Dimension(unit) ** abs(exponent))
-
- self.numerator = numerator
- self.denominator = denominator
+1710
+1711
+1712
+1713
+1714
+1715
+1716
+1717
+1718
+1719
+1720
+1721
+1722
+1723
+1724
+1725
+1726
+1727
+1728
+1729
+1730
+1731
+1732
+1733
+1734
+1735
| def simplify(self) -> None:
+ """
+ Simplify the composite by merging common dimensions.
+
+ Examples:
+ >>> class PressureUnit(AliasMeasurementUnit):
+ ... BAR = "bar"
+ ... PASCAL = "Pa"
+
+ >>> class TemperatureUnit(MeasurementUnit):
+ ... KELVIN = "K"
+
+ >>> class LengthUnit(MeasurementUnit):
+ ... METER = "m"
+
+ >>> class TimeUnit(MeasurementUnit):
+ ... SECOND = "s"
+
+ >>> composite = (PressureUnit.BAR**(-2)) / (TemperatureUnit.KELVIN**(-1))
+ >>> composite
+ <CompositeDimension: (bar^-2) / (K^-1)>
+ >>> composite.simplify()
+ >>> composite
+ <CompositeDimension: K / (bar^2)>
+
+ >>> composite = PressureUnit.PASCAL * LengthUnit.METER * PressureUnit.PASCAL /TimeUnit.SECOND
+ >>> composite
+ <CompositeDimension: Pa * Pa * m / s>
+ >>> composite.simplify()
+ >>> composite
+ <CompositeDimension: (Pa^2) * m / s>
+ """
+ exponents: Dict[MeasurementUnit, float] = {}
+ for n in self.numerator:
+ if n.unit in exponents:
+ exponents[n.unit] += n.power
+ else:
+ exponents[n.unit] = n.power
+
+ for d in self.denominator:
+ if d.unit in exponents:
+ exponents[d.unit] -= d.power
+ else:
+ exponents[d.unit] = 0 - d.power
+
+ numerator = []
+ denominator = []
+ for unit, exponent in exponents.items():
+ if unit.is_non_dimensional():
+ continue # do not add non dimensional units to the simplified composite
+
+ if exponent > 0:
+ numerator.append(Dimension(unit) ** exponent)
+ elif exponent < 0:
+ denominator.append(Dimension(unit) ** abs(exponent))
+
+ self.numerator = numerator
+ self.denominator = denominator
|
@@ -4906,41 +4936,41 @@
Source code in src/property_utils/units/descriptors.py
- | def to_generic(self) -> GenericCompositeDimension:
- """
- Create a generic descriptor from this CompositeDimension.
-
- Examples:
- >>> class AmountUnit(MeasurementUnit):
- ... MOL = "mol"
-
- >>> class MassUnit(MeasurementUnit):
- ... KILO_GRAM = "kg"
-
- >>> (AmountUnit.MOL / MassUnit.KILO_GRAM).to_generic()
- <GenericCompositeDimension: AmountUnit / MassUnit>
- """
- return GenericCompositeDimension(
- numerator=[n.to_generic() for n in self.numerator],
- denominator=[d.to_generic() for d in self.denominator],
- )
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|