aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-02-12 02:13:17 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-02-12 09:02:13 +0100
commitf320eb8aafbceafbbfca02617db4846b3571e598 (patch)
tree288b8f5cda4264fc6e0c889de2c77848bf706c9a
parent0c79fad318711394874d94672e96db6da1ed9c52 (diff)
downloadTrader-f320eb8aafbceafbbfca02617db4846b3571e598.tar.gz
Trader-f320eb8aafbceafbbfca02617db4846b3571e598.tar.zst
Trader-f320eb8aafbceafbbfca02617db4846b3571e598.zip
Add missing amount operations
-rw-r--r--portfolio.py8
-rw-r--r--test.py9
2 files changed, 17 insertions, 0 deletions
diff --git a/portfolio.py b/portfolio.py
index 17a17ba..21a9834 100644
--- a/portfolio.py
+++ b/portfolio.py
@@ -124,6 +124,8 @@ class Amount:
124 return Amount(self.currency, abs(self.value)) 124 return Amount(self.currency, abs(self.value))
125 125
126 def __add__(self, other): 126 def __add__(self, other):
127 if other == 0:
128 return self
127 if other.currency != self.currency and other.value * self.value != 0: 129 if other.currency != self.currency and other.value * self.value != 0:
128 raise Exception("Summing amounts must be done with same currencies") 130 raise Exception("Summing amounts must be done with same currencies")
129 return Amount(self.currency, self.value + other.value) 131 return Amount(self.currency, self.value + other.value)
@@ -141,6 +143,12 @@ class Amount:
141 raise Exception("Summing amounts must be done with same currencies") 143 raise Exception("Summing amounts must be done with same currencies")
142 return Amount(self.currency, self.value - other.value) 144 return Amount(self.currency, self.value - other.value)
143 145
146 def __rsub__(self, other):
147 if other == 0:
148 return -self
149 else:
150 return -self.__sub__(other)
151
144 def __mul__(self, value): 152 def __mul__(self, value):
145 if not isinstance(value, (int, float, D)): 153 if not isinstance(value, (int, float, D)):
146 raise TypeError("Amount may only be multiplied by numbers") 154 raise TypeError("Amount may only be multiplied by numbers")
diff --git a/test.py b/test.py
index 1965521..c496e11 100644
--- a/test.py
+++ b/test.py
@@ -220,6 +220,8 @@ class AmountTest(WebMockTestCase):
220 amount4 = portfolio.Amount("ETH", 0.0) 220 amount4 = portfolio.Amount("ETH", 0.0)
221 self.assertEqual(amount1, amount1 + amount4) 221 self.assertEqual(amount1, amount1 + amount4)
222 222
223 self.assertEqual(amount1, amount1 + 0)
224
223 def test__radd(self): 225 def test__radd(self):
224 amount = portfolio.Amount("XVG", "12.9") 226 amount = portfolio.Amount("XVG", "12.9")
225 227
@@ -241,6 +243,13 @@ class AmountTest(WebMockTestCase):
241 amount4 = portfolio.Amount("ETH", 0.0) 243 amount4 = portfolio.Amount("ETH", 0.0)
242 self.assertEqual(amount1, amount1 - amount4) 244 self.assertEqual(amount1, amount1 - amount4)
243 245
246 def test__rsub(self):
247 amount = portfolio.Amount("ETH", "1.6")
248 with self.assertRaises(Exception):
249 3 - amount
250
251 self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount)
252
244 def test__mul(self): 253 def test__mul(self):
245 amount = portfolio.Amount("XEM", 11) 254 amount = portfolio.Amount("XEM", 11)
246 255