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):
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)
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):
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):
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()
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))
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))