Skip to content

Commit

Permalink
Defines has_no_units methods for composite dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxcode123 committed Sep 19, 2024
1 parent be68cd1 commit 258ac67
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/property_utils/tests/units/test_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,14 @@ def test_with_aliased_denominator_composite_dimension(self):
def test_with_fully_aliased_composite_dimension(self):
self.assertResultTrue()

@add_to(GenericCompositeDimension_test_suite)
class TestGenericCompositeDimensionHasNoUnits(TestDescriptor):
def test_with_measurement_units(self):
self.assertFalse(generic_composite_dimension().has_no_units())

def test_with_no_measurement_units(self):
self.assertTrue(GenericCompositeDimension().has_no_units())


@add_to(CompositeDimension_test_suite)
class TestCompositeDimensionFromDescriptor(TestDescriptor):
Expand Down Expand Up @@ -2568,6 +2576,19 @@ def test_objects_are_not_persisted(self):
self.assertNotEqual(ids(composite.numerator), ids(inverse.denominator))
self.assertNotEqual(ids(composite.denominator), ids(inverse.numerator))

@add_to(CompositeDimension_test_suite)
class TestCompositeDimensionHasNoUnits(TestDescriptor):
def test_with_units(self):
self.assertFalse(composite_dimension().has_no_units())

def test_with_no_units(self):
self.assertTrue(CompositeDimension().has_no_units())

def test_with_same_unit_type(self):
self.assertFalse(CompositeDimension([Unit1.A],[Unit1.a]).has_no_units())

def test_with_same_unit(self):
self.assertFalse(CompositeDimension([Unit1.A],[Unit1.A]).has_no_units())

@add_to(CompositeDimension_test_suite)
class TestCompositeDimensionMultiplication(TestDescriptorBinaryOperation):
Expand Down
30 changes: 30 additions & 0 deletions src/property_utils/units/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,21 @@ def is_equivalent(self, other: GenericUnitDescriptor) -> bool:

return False

def has_no_units(self) -> bool:
"""
Returns True if the generic composite dimension does not have any units, False
otherwise.
Examples:
>>> class LengthUnit(MeasurementUnit): ...
>>> GenericCompositeDimension().has_no_units()
True
>>> GenericCompositeDimension([LengthUnit]).has_no_units()
False
"""
return len(self.denominator) == 0 and len(self.numerator) == 0

def _numerator_copy(self) -> List[GenericDimension]:
return [replace(n) for n in self.numerator]

Expand Down Expand Up @@ -1743,6 +1758,21 @@ def inverse(self) -> "CompositeDimension":
"""
return CompositeDimension(self._denominator_copy(), self._numerator_copy())

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]

Expand Down

0 comments on commit 258ac67

Please sign in to comment.