Skip to content

Commit

Permalink
Merge pull request #556 from suutari-ai/unitted-decimal
Browse files Browse the repository at this point in the history
Rename UnitedDecimal to UnittedDecimal (SHUUP-2879)
  • Loading branch information
shawnadelic committed Jun 20, 2016
2 parents 05b501e + e6d038d commit bc178e8
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 48 deletions.
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ Default Tax
General/miscellaneous
~~~~~~~~~~~~~~~~~~~~~

- Rename UnitedDecimal to UnittedDecimal
- Add a way to find out min and max values from pattern
- Reword doc/provides.rst

Expand Down
2 changes: 1 addition & 1 deletion shoop/core/pricing/_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Price(Money):
will raise an :obj:`~shoop.utils.numbers.UnitMixupError`.
In addition to `includes_tax` info, Prices are Money and know their
`~shoop.utils.numbers.UnitedDecimal.value` and
`~shoop.utils.numbers.UnittedDecimal.value` and
`~shoop.utils.money.Money.currency`. To get the bare Money amount
of a `Price`, use the `amount` property.
"""
Expand Down
76 changes: 38 additions & 38 deletions shoop/utils/_united_decimal.py → shoop/utils/_unitted_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import decimal


class UnitedDecimal(decimal.Decimal):
class UnittedDecimal(decimal.Decimal):
"""
Decimal with unit.
Expand All @@ -19,7 +19,7 @@ class UnitedDecimal(decimal.Decimal):
TaxfulPrice(1) + TaxlessPrice(2)
where `TaxfulPrice` and `TaxlessPrice` are subclasses of
`UnitedDecimal`.
`UnittedDecimal`.
"""
@property
def value(self):
Expand All @@ -31,7 +31,7 @@ def value(self):
return decimal.Decimal(self)

def __repr__(self):
decimal_repr = super(UnitedDecimal, self).__repr__()
decimal_repr = super(UnittedDecimal, self).__repr__()
return decimal_repr.replace('Decimal', type(self).__name__)

def unit_matches_with(self, other):
Expand All @@ -51,7 +51,7 @@ def new(self, value):
:type value:
:return: Object with same type as self and matching unit, but with given decimal value
:rtype: UnitedDecimal
:rtype: UnittedDecimal
"""
return type(self)(value)

Expand All @@ -61,42 +61,42 @@ def _check_units_match(self, other):

def __lt__(self, other, **kwargs):
self._check_units_match(other)
return super(UnitedDecimal, self).__lt__(other, **kwargs)
return super(UnittedDecimal, self).__lt__(other, **kwargs)

def __le__(self, other, **kwargs):
self._check_units_match(other)
return super(UnitedDecimal, self).__le__(other, **kwargs)
return super(UnittedDecimal, self).__le__(other, **kwargs)

def __gt__(self, other, **kwargs):
self._check_units_match(other)
return super(UnitedDecimal, self).__gt__(other, **kwargs)
return super(UnittedDecimal, self).__gt__(other, **kwargs)

def __ge__(self, other, **kwargs):
self._check_units_match(other)
return super(UnitedDecimal, self).__ge__(other, **kwargs)
return super(UnittedDecimal, self).__ge__(other, **kwargs)

def __eq__(self, other, *args, **kwargs):
if not self.unit_matches_with(other):
return False
return super(UnitedDecimal, self).__eq__(other, **kwargs)
return super(UnittedDecimal, self).__eq__(other, **kwargs)

def __ne__(self, other, *args, **kwargs):
if not self.unit_matches_with(other):
return True
return super(UnitedDecimal, self).__ne__(other, **kwargs)
return super(UnittedDecimal, self).__ne__(other, **kwargs)

def __add__(self, other, **kwargs):
self._check_units_match(other)
return self.new(super(UnitedDecimal, self).__add__(other, **kwargs))
return self.new(super(UnittedDecimal, self).__add__(other, **kwargs))

def __sub__(self, other, **kwargs):
self._check_units_match(other)
return self.new(super(UnitedDecimal, self).__sub__(other, **kwargs))
return self.new(super(UnittedDecimal, self).__sub__(other, **kwargs))

def __mul__(self, other, **kwargs):
if isinstance(other, UnitedDecimal):
if isinstance(other, UnittedDecimal):
raise TypeError('Cannot multiply %r with %r' % (self, other))
return self.new(super(UnitedDecimal, self).__mul__(other, **kwargs))
return self.new(super(UnittedDecimal, self).__mul__(other, **kwargs))

def __radd__(self, other, **kwargs):
return self.__add__(other, **kwargs)
Expand All @@ -108,95 +108,95 @@ def __rmul__(self, other, **kwargs):
return self.__mul__(other, **kwargs)

def __truediv__(self, other, **kwargs):
if isinstance(other, UnitedDecimal):
if isinstance(other, UnittedDecimal):
self._check_units_match(other)
return super(UnitedDecimal, self).__truediv__(other, **kwargs)
return super(UnittedDecimal, self).__truediv__(other, **kwargs)
else:
value = super(UnitedDecimal, self).__truediv__(other, **kwargs)
value = super(UnittedDecimal, self).__truediv__(other, **kwargs)
return self.new(value)

def __rtruediv__(self, other, **kwargs):
if not isinstance(other, UnitedDecimal):
if not isinstance(other, UnittedDecimal):
type_name = type(self).__name__
raise TypeError('Cannot divide non-{0} with {0}'.format(type_name))
self._check_units_match(other)
return super(UnitedDecimal, self).__rtruediv__(other, **kwargs)
return super(UnittedDecimal, self).__rtruediv__(other, **kwargs)

__div__ = __truediv__
__rdiv__ = __rtruediv__

def __floordiv__(self, other, **kwargs):
if not isinstance(other, UnitedDecimal):
if not isinstance(other, UnittedDecimal):
type_name = type(self).__name__
msg = 'Cannot floor-div {0} with non-{0}'.format(type_name)
raise TypeError(msg)
self._check_units_match(other)
return super(UnitedDecimal, self).__floordiv__(other, **kwargs)
return super(UnittedDecimal, self).__floordiv__(other, **kwargs)

def __rfloordiv__(self, other, **kwargs):
if not isinstance(other, UnitedDecimal):
if not isinstance(other, UnittedDecimal):
type_name = type(self).__name__
msg = 'Cannot floor-div non-{0} with {0}'.format(type_name)
raise TypeError(msg)
self._check_units_match(other)
return super(UnitedDecimal, self).__rfloordiv__(other, **kwargs)
return super(UnittedDecimal, self).__rfloordiv__(other, **kwargs)

def __mod__(self, other, **kwargs):
if not isinstance(other, UnitedDecimal):
if not isinstance(other, UnittedDecimal):
type_name = type(self).__name__
raise TypeError('Cannot modulo {0} with non-{0}'.format(type_name))
self._check_units_match(other)
return self.new(super(UnitedDecimal, self).__mod__(other, **kwargs))
return self.new(super(UnittedDecimal, self).__mod__(other, **kwargs))

def __divmod__(self, other, **kwargs):
if not isinstance(other, UnitedDecimal):
if not isinstance(other, UnittedDecimal):
type_name = type(self).__name__
raise TypeError('Cannot divmod {0} with non-{0}'.format(type_name))
self._check_units_match(other)
(div, mod) = super(UnitedDecimal, self).__divmod__(other, **kwargs)
(div, mod) = super(UnittedDecimal, self).__divmod__(other, **kwargs)
return (div, self.new(mod))

def __pow__(self, other, **kwargs):
type_name = type(self).__name__
raise TypeError('{} cannot be powered'.format(type_name))

def __neg__(self, **kwargs):
return self.new(super(UnitedDecimal, self).__neg__(**kwargs))
return self.new(super(UnittedDecimal, self).__neg__(**kwargs))

def __pos__(self, **kwargs):
return self.new(super(UnitedDecimal, self).__pos__(**kwargs))
return self.new(super(UnittedDecimal, self).__pos__(**kwargs))

def __abs__(self, **kwargs):
return self.new(super(UnitedDecimal, self).__abs__(**kwargs))
return self.new(super(UnittedDecimal, self).__abs__(**kwargs))

def __int__(self, **kwargs):
return super(UnitedDecimal, self).__int__(**kwargs)
return super(UnittedDecimal, self).__int__(**kwargs)

def __float__(self, **kwargs):
return super(UnitedDecimal, self).__float__(**kwargs)
return super(UnittedDecimal, self).__float__(**kwargs)

def __round__(self, ndigits=0, **kwargs):
value = super(UnitedDecimal, self).__round__(ndigits, **kwargs)
value = super(UnittedDecimal, self).__round__(ndigits, **kwargs)
return self.new(value)

def quantize(self, exp, *args, **kwargs):
value = super(UnitedDecimal, self).quantize(exp, *args, **kwargs)
value = super(UnittedDecimal, self).quantize(exp, *args, **kwargs)
return self.new(value)

def copy_negate(self, *args, **kwargs):
value = super(UnitedDecimal, self).copy_negate(*args, **kwargs)
value = super(UnittedDecimal, self).copy_negate(*args, **kwargs)
return self.new(value)


class UnitMixupError(TypeError):
"""
Invoked operation for UnitedDecimal and object with non-matching unit.
Invoked operation for UnittedDecimal and object with non-matching unit.
The objects involved are stored in instance variables `obj1` and
`obj2`. Former is instance of :class:`UnitedDecimal` or its
`obj2`. Former is instance of :class:`UnittedDecimal` or its
subclass and the other could be any object.
:ivar UnitedDecimal obj1: Involved object 1
:ivar UnittedDecimal obj1: Involved object 1
:ivar Any obj2: Involved object 2
"""
def __init__(self, obj1, obj2, msg='Unit mixup'):
Expand Down
4 changes: 2 additions & 2 deletions shoop/utils/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from . import numbers


class Money(numbers.UnitedDecimal):
class Money(numbers.UnittedDecimal):
"""
Money value with currency.
The pure decimal value is available from the base classes
`~shoop.utils.numbers.UnitedDecimal.value` property (preferred way)
`~shoop.utils.numbers.UnittedDecimal.value` property (preferred way)
or by casting to `Decimal`.
Money objects with different currencies cannot be compared or
Expand Down
4 changes: 2 additions & 2 deletions shoop/utils/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

from shoop.utils import update_module_attributes

from ._united_decimal import UnitedDecimal, UnitMixupError
from ._unitted_decimal import UnitMixupError, UnittedDecimal

__all__ = [
"UnitedDecimal",
"UnittedDecimal",
"UnitMixupError",
"bankers_round",
"strip_non_float_chars",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import pytest
import six

from shoop.utils.numbers import UnitedDecimal
from shoop.utils.numbers import UnittedDecimal


class BaseDecimal(UnitedDecimal):
class BaseDecimal(UnittedDecimal):
unit = None

def unit_matches_with(self, other):
Expand All @@ -27,7 +27,7 @@ class BarDecimal(BaseDecimal):
unit = 'bar'


def test_united_decimal_basics():
def test_unitted_decimal_basics():
assert FooDecimal(1) + FooDecimal(2) == FooDecimal(3)
assert FooDecimal(3) - FooDecimal(2) == FooDecimal(1)
assert FooDecimal(1) < FooDecimal(2)
Expand All @@ -39,7 +39,7 @@ def test_united_decimal_basics():
assert -FooDecimal(4) == FooDecimal('-4')


def test_united_decimal_value():
def test_unitted_decimal_value():
assert FooDecimal(42).value == 42
assert type(FooDecimal(42).value) == Decimal

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_invalid_power():

def test_base_class_units_match_unimplemented():
with pytest.raises(NotImplementedError):
UnitedDecimal().unit_matches_with(UnitedDecimal())
UnittedDecimal().unit_matches_with(UnittedDecimal())


def test_floordiv():
Expand Down

0 comments on commit bc178e8

Please sign in to comment.