diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 02:13:17 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-02-12 09:02:13 +0100 |
commit | f320eb8aafbceafbbfca02617db4846b3571e598 (patch) | |
tree | 288b8f5cda4264fc6e0c889de2c77848bf706c9a /portfolio.py | |
parent | 0c79fad318711394874d94672e96db6da1ed9c52 (diff) | |
download | Trader-f320eb8aafbceafbbfca02617db4846b3571e598.tar.gz Trader-f320eb8aafbceafbbfca02617db4846b3571e598.tar.zst Trader-f320eb8aafbceafbbfca02617db4846b3571e598.zip |
Add missing amount operations
Diffstat (limited to 'portfolio.py')
-rw-r--r-- | portfolio.py | 8 |
1 files changed, 8 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") |