Wires in the money class.
[retire.git] / money.py
index 52fe32b5c72926b37d3a74f48c3e4daff1c9ec9f..848d9725e2387ac342e93814088d3a4a1b47e195 100644 (file)
--- a/money.py
+++ b/money.py
@@ -3,40 +3,41 @@ import decimal
 class money(object):
     def __init__(self, amount="0"):
         try:
+            if isinstance(amount, money):
+                amount = amount.amount
             self.amount = decimal.Decimal(amount)
         except decimal.InvalidOperation:
             raise ValueError("amount value could not be converted to "
                              "Decimal(): '{}'".format(amount))
 
-    @property
     def amount(self):
         return self.amount
 
     def __hash__(self):
-        return hash(self._amount)
+        return hash(self.amount)
 
     def __repr__(self):
-        return "{}".format(self._amount)
+        return "{}".format(self.amount)
 
     def __str__(self):
         return self.__unicode__().encode('utf-8')
 
     def __unicode__(self):
-        return u"${:,.2f}".format(self._amount)
+        return u"${:,.2f}".format(self.amount)
 
     def __lt__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '<')
+            other = money(other)
         return self.amount < other.amount
 
     def __le__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '<=')
-        return self._amount <= other.amount
+            other = money(other)
+        return self.amount <= other.amount
 
     def __eq__(self, other):
         if isinstance(other, money):
-            return self._amount == other.amount
+            return self.amount == other.amount
         return False
 
     def __ne__(self, other):
@@ -44,17 +45,17 @@ class money(object):
 
     def __gt__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '>')
-        return self._amount > other.amount
+            other = money(other)
+        return self.amount > other.amount
 
     def __ge__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '>=')
-        return self._amount >= other.amount
+            other = money(other)
+        return self.amount >= other.amount
 
     def __add__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '+')
+            other = money(other)
         other = other.amount
         amount = self.amount + other
         return self.__class__(amount)
@@ -64,9 +65,9 @@ class money(object):
 
     def __sub__(self, other):
         if not isinstance(other, money):
-            raise InvalidOperandType(other, '-')
+            other = money(other)
         other = other.amount
-        amount = self._amount - other
+        amount = self.amount - other
         return self.__class__(amount)
 
     def __rsub__(self, other):
@@ -74,9 +75,8 @@ class money(object):
 
     def __mul__(self, other):
         if isinstance(other, money):
-            raise TypeError("multiplication is unsupported between "
-                            "two money objects")
-        amount = self._amount * other
+            other = other.amount()
+        amount = self.amount * decimal.Decimal(other)
         return self.__class__(amount)
 
     def __rmul__(self, other):
@@ -93,14 +93,14 @@ class money(object):
         else:
             if other == 0:
                 raise ZeroDivisionError()
-            amount = self.amount / other
+            amount = self.amount / decimal.Decimal(other)
             return self.__class__(amount)
 
     def __floordiv__(self, other):
         if isinstance(other, money):
             if other.amount == 0:
                 raise ZeroDivisionError()
-            return self._amount // other.amount
+            return self.amount // other.amount
         else:
             if other == 0:
                 raise ZeroDivisionError()
@@ -120,11 +120,11 @@ class money(object):
         if isinstance(other, money):
             if other.amount == 0:
                 raise ZeroDivisionError()
-            return divmod(self._amount, other.amount)
+            return divmod(self.amount, other.amount)
         else:
             if other == 0:
                 raise ZeroDivisionError()
-            whole, remainder = divmod(self._amount, other)
+            whole, remainder = divmod(self.amount, other)
             return (self.__class__(whole),
                     self.__class__(remainder))
 
@@ -139,7 +139,7 @@ class money(object):
         return self.__class__(-self.amount)
 
     def __pos__(self):
-        return self.__class__(+self._amount)
+        return self.__class__(+self.amount)
 
     def __abs__(self):
         return self.__class__(abs(self.amount))