Skip to content

Commit b5912c1

Browse files
committed
Implement unit preconversion for property division
1 parent 5c4fd26 commit b5912c1

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

src/property_utils/properties/property.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,14 @@ def __truediv__(self, other) -> "Property":
255255
) from None
256256
return Property(value, self.unit)
257257
if isinstance(other, Property):
258+
_other = self._unit_preconversion(other)
258259
try:
259-
value = self.value / other.value
260+
value = self.value / _other.value
260261
except ZeroDivisionError:
261262
raise PropertyBinaryOperationError(
262263
f"cannot divide {self} with {other}; denominator's value is zero. "
263264
) from None
264-
return Property(value, (self.unit / other.unit).simplified())
265+
return Property(value, (self.unit / _other.unit).simplified())
265266
raise PropertyBinaryOperationError(
266267
f"cannot divide {self} with {other}; "
267268
"denominator must be numeric or Property. "

src/property_utils/tests/properties/test_property.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,95 @@ def test_with_zero_value_property(self):
474474
self.assert_invalid_operation()
475475

476476

477+
@add_to(property_test_suite, "__truediv__")
478+
class TestCompositeDimensionPropertyUnitPreconversionDivision(TestProperty):
479+
480+
def build_property(self) -> Property:
481+
return Property(1, Unit1.A * Unit4.d**2 / Unit6.F / Unit8.H**3)
482+
483+
@args({"other": Property(1, Unit6.f / Unit1.a)})
484+
def test_with_composite_unit_add_to_numerator_and_denominator(self):
485+
self.assert_result("0.2 (A^2) * (d^2) / (F^2) / (H^3)")
486+
487+
@args({"other": Property(1, Unit1.a / Unit6.f)})
488+
def test_with_composite_unit_simplify_numerator_and_denominator(self):
489+
self.assert_result("5.0 (d^2) / (H^3)")
490+
491+
@args({"other": Property(1, Unit8.h**3)})
492+
def test_with_dimension_same_denominator(self):
493+
self.assert_result("64.0 (d^2) * A / (H^6) / F")
494+
495+
@args({"other": Property(1, Unit8.h**2)})
496+
def test_with_dimension_denominator(self):
497+
self.assert_result("16.0 (d^2) * A / (H^5) / F")
498+
499+
@args({"other": Property(1, Unit1.a**2)})
500+
def test_with_dimension_numerator(self):
501+
self.assert_result_almost("100.0 (d^2) / (H^3) / A / F")
502+
503+
@args({"other": Property(1, Unit4.D)})
504+
def test_with_unit_same_numerator(self):
505+
self.assert_result("0.2 A * d / (H^3) / F")
506+
507+
@args({"other": Property(2, Unit6.f)})
508+
def test_with_unit_same_denominator(self):
509+
self.assert_result("1.0 (d^2) * A / (F^2) / (H^3)")
510+
511+
512+
@add_to(property_test_suite, "__truediv__")
513+
class TestDimensionPropertyUnitPreconversionDivision(TestProperty):
514+
515+
def build_property(self) -> Property:
516+
return Property(1, Unit1.A**2)
517+
518+
@args({"other": Property(1, Unit4.d / Unit1.a)})
519+
def test_with_composite_dimension_denominator(self):
520+
self.assert_result("0.1 (A^3) / d")
521+
522+
@args({"other": Property(1, Unit1.a / Unit4.d)})
523+
def test_with_composite_dimension_numerator(self):
524+
self.assert_result("10.0 A * d")
525+
526+
@args({"other": Property(1, Unit4.d / Unit1.a**2)})
527+
def test_with_composite_dimension_same_denominator(self):
528+
self.assert_result_almost("0.01 (A^4) / d")
529+
530+
@args({"other": Property(1000, Unit1.a**3)})
531+
def test_with_same_unit_dimension(self):
532+
self.assert_result_almost("1.0 / A")
533+
534+
@args({"other": Property(10, Unit1.a)})
535+
def test_with_same_unit(self):
536+
self.assert_result("1.0 A")
537+
538+
539+
@add_to(property_test_suite, "__truediv__")
540+
class TestUnitPropertyUnitPreconversionDivision(TestProperty):
541+
542+
def build_property(self) -> Property:
543+
return Property(1, Unit1.A)
544+
545+
@args({"other": Property(1, Unit4.d / Unit1.a)})
546+
def test_with_composite_dimension_same_denominator(self):
547+
self.assert_result("0.1 (A^2) / d")
548+
549+
@args({"other": Property(10, Unit1.a / Unit4.d)})
550+
def test_with_composite_dimension_same_numerator(self):
551+
self.assert_result("1.0 d")
552+
553+
@args({"other": Property(1, Unit4.d / Unit1.a**2)})
554+
def test_with_composite_dimension(self):
555+
self.assert_result_almost("0.01 (A^3) / d")
556+
557+
@args({"other": Property(100, Unit1.a**2)})
558+
def test_with_dimension_same_unit(self):
559+
self.assert_result_almost("1.0 / A")
560+
561+
@args({"other": Property(10, Unit1.a)})
562+
def test_with_same_unit(self):
563+
self.assert_result("1.0 ")
564+
565+
477566
@add_to(property_test_suite, "__rtruediv__")
478567
class TestPropertyRightDivision(TestProperty):
479568

0 commit comments

Comments
 (0)