Browse Source

calcex.Figure: overload * operator

Fabian Peter Hammerle 8 years ago
parent
commit
bfbb508504
2 changed files with 18 additions and 0 deletions
  1. 9 0
      ioex/calcex.py
  2. 9 0
      tests/calcex/test_figure.py

+ 9 - 0
ioex/calcex.py

@@ -90,3 +90,12 @@ class Figure(object):
             raise UnitMismatchError('{} - {}'.format(self, other))
         else:
             return type(self)(value=self.value - other.value, unit=self.unit)
+
+    def __mul__(self, factor):
+        if isinstance(factor, Figure):
+            assert not self.value is None
+            assert not factor.value is None
+            assert factor.unit is None
+            return type(self)(value=self.value * factor.value, unit=self.unit)
+        else:
+            return self * Figure(value=factor, unit=None)

+ 9 - 0
tests/calcex/test_figure.py

@@ -182,3 +182,12 @@ def test_sub_persistent():
     b.value = 4
     b.unit[0] = 'l'
     assert Figure(-1, ['m']) == d
+
+
+@pytest.mark.parametrize(('a', 'b', 'expected_product'), [
+    [Figure(1, 'm'), Figure(2), Figure(2, 'm')],
+    [Figure(1, 'm'), 2, Figure(2, 'm')],
+    [Figure(2, 'm'), -1.5, Figure(-3, 'm')],
+])
+def test_mult(a, b, expected_product):
+    assert expected_product == a * b