Skip to content

Commit

Permalink
Cleanup a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
r4g3baby committed May 27, 2023
1 parent 072a174 commit bf00999
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
5 changes: 2 additions & 3 deletions money/django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def _currency_field_name(name): return f"{name}_currency"


class MoneyFieldProxy(object):
class MoneyFieldProxy:
def __init__(self, field):
self.field = field
self.currency_name = _currency_field_name(self.field.name)
Expand All @@ -29,8 +29,7 @@ def __get__(self, obj, type=None):
amount = obj.__dict__[self.field.name]
if amount is None:
return None
else:
return Money(amount, getattr(obj, self.currency_name))
return Money(amount, getattr(obj, self.currency_name))


class CurrencyField(models.CharField):
Expand Down
2 changes: 1 addition & 1 deletion money/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __getattr__(self, item):
return getattr(self._backend, item)

def __setattr__(self, key, value):
if key == '_backend' or key == 'backend':
if key in ('_backend', 'backend'):
return super().__setattr__(key, value)
if self._backend is None:
raise ExchangeBackendNotSet()
Expand Down
37 changes: 17 additions & 20 deletions money/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def operator_func(self, other, *args):
return operator_func


def _make_class_operator(name):
method = getattr(Decimal, name, None)

def operator_func(self, *args):
return self.__class__(method(self, *args), self._currency)

return operator_func


class Money(Decimal):
"""Class representing a monetary amount."""

Expand Down Expand Up @@ -84,8 +93,7 @@ def __eq__(self, other):

if isinstance(other, Money):
return result and other._currency == self._currency
else:
return result
return result

def __ne__(self, other):
return not self == other
Expand Down Expand Up @@ -114,23 +122,12 @@ def __ne__(self, other):
__pow__ = _make_arithmetic_operator('__pow__')
__rpow__ = _make_arithmetic_operator('__rpow__')

def __neg__(self):
return self.__class__(super().__neg__(), self._currency)

def __pos__(self):
return self.__class__(super().__pos__(), self._currency)

def __abs__(self):
return self.__class__(super().__abs__(), self._currency)

def __round__(self, n=0):
return self.__class__(super().__round__(n), self._currency)

def __floor__(self):
return self.__class__(super().__floor__(), self._currency)

def __ceil__(self):
return self.__class__(super().__ceil__(), self._currency)
__neg__ = _make_class_operator('__neg__')
__pos__ = _make_class_operator('__pos__')
__abs__ = _make_class_operator('__abs__')
__round__ = _make_class_operator('__round__')
__floor__ = _make_class_operator('__floor__')
__ceil__ = _make_class_operator('__ceil__')

def to(self, currency):
"""Returns the equivalent money object in another currency."""
Expand All @@ -153,7 +150,7 @@ def to(self, currency):
def format(self, locale='en_US'):
"""Returns a string of the currency formatted for the specified locale."""

return format_currency(self, self.currency.code, locale=locale).replace(u'\xa0', u' ')
return format_currency(self, self.currency.code, locale=locale).replace('\xa0', ' ')

@classmethod
def set_rounding_mode(cls, mode):
Expand Down

0 comments on commit bf00999

Please sign in to comment.