示例#1
0
 def __truediv__(self, other, context=None):
     other = self._assert_dividable(other)
     amount = Decimal.__truediv__(self, other)
     return self.__class__(amount)
示例#2
0
文件: size.py 项目: cgwalters/blivet
 def __truediv__(self, other, context=None):
     return Size(Decimal.__truediv__(self, other))
示例#3
0
 def __truediv__(self,*args,**kw): 
     return self.__class__(Decimal.__truediv__(self,*args,**kw))
示例#4
0
class FVal(object):
    """A value to represent numbers for financial applications. At the moment
    we use the python Decimal library but the abstraction will help us change the
    underlying implementation if needed.

    At the moment we do not allow any operations against floating points. Even though
    floating points could be converted to Decimals before each operation we will
    use this restriction to make sure floating point numbers are rooted from the codebase first.
    """

    __slots__ = ('num', )

    def __init__(self, data: AcceptableFValInitInput):
        try:
            if isinstance(data, float):
                self.num = Decimal(str(data))
            elif isinstance(data, bytes):
                # assume it's an ascii string and try to decode the bytes to one
                self.num = Decimal(data.decode())
            elif isinstance(data, (Decimal, int, str)):
                self.num = Decimal(data)
            elif isinstance(data, FVal):
                self.num = data.num
            else:
                self.num = None

        except InvalidOperation:
            raise ValueError(
                'Expected string, int, float, or Decimal to initialize an FVal.'
                'Found {}.'.format(type(data)), )

    def __str__(self) -> str:
        return str(self.num)

    def __repr__(self) -> str:
        return 'FVal({})'.format(str(self.num))

    def __gt__(self, other: AcceptableFValOtherInput) -> bool:
        evaluated_other = evaluate_input(other)
        return self.num.compare_signal(evaluated_other) == Decimal('1')

    def __lt__(self, other: AcceptableFValOtherInput) -> bool:
        evaluated_other = evaluate_input(other)
        return self.num.compare_signal(evaluated_other) == Decimal('-1')

    def __le__(self, other: AcceptableFValOtherInput) -> bool:
        evaluated_other = evaluate_input(other)
        return self.num.compare_signal(evaluated_other) in (Decimal('-1'),
                                                            Decimal('0'))

    def __ge__(self, other: AcceptableFValOtherInput) -> bool:
        evaluated_other = evaluate_input(other)
        return self.num.compare_signal(evaluated_other) in (Decimal('1'),
                                                            Decimal('0'))

    def __eq__(self, other: object) -> bool:
        evaluated_other = evaluate_input(other)
        return self.num.compare_signal(evaluated_other) == Decimal('0')

    def __add__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__add__(evaluated_other))

    def __sub__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__sub__(evaluated_other))

    def __mul__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__mul__(evaluated_other))

    def __truediv__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__truediv__(evaluated_other))

    def __floordiv__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__floordiv__(evaluated_other))

    def __pow__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__pow__(evaluated_other))

    def __radd__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__radd__(evaluated_other))

    def __rsub__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__rsub__(evaluated_other))

    def __rmul__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__rmul__(evaluated_other))

    def __rtruediv__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__rtruediv__(evaluated_other))

    def __rfloordiv__(self, other: AcceptableFValOtherInput):
        evaluated_other = evaluate_input(other)
        return FVal(self.num.__rfloordiv__(evaluated_other))

    def __float__(self) -> float:
        return float(self.num)

    # --- Unary operands

    def __neg__(self):
        return FVal(self.num.__neg__())

    def __abs__(self):
        return FVal(self.num.copy_abs())

    # --- Other operations

    def fma(self, other: AcceptableFValOtherInput,
            third: AcceptableFValOtherInput):
        """
        Fused multiply-add. Return self*other+third with no rounding of the
        intermediate product self*other
        """
        evaluated_other = evaluate_input(other)
        evaluated_third = evaluate_input(third)
        return FVal(self.num.fma(evaluated_other, evaluated_third))

    def to_percentage(self, precision: int = 4) -> str:
        return '{:.{}%}'.format(self.num, precision)

    def to_int(self, exact: bool) -> int:
        """Tries to convert to int, If `exact` is true then it will convert only if
        it is a whole decimal number; i.e.: if it has got nothing after the decimal point"""
        if exact and self.num.to_integral_exact() != self.num:
            raise ValueError('Tried to ask for exact int from {}'.format(
                self.num))
        return int(self.num)

    def is_close(self,
                 other: AcceptableFValInitInput,
                 max_diff: str = "1e-6") -> bool:
        evaluated_max_diff = FVal(max_diff)

        if not isinstance(other, FVal):
            other = FVal(other)

        diff_num = abs(self.num - other.num)
        return diff_num <= evaluated_max_diff.num
示例#5
0
 def __truediv__(self, other, context=None):
     if PY2:
         return TimeValue(Decimal.__truediv__(self, other, context))
     return TimeValue(Decimal.__truediv__(self, other))
示例#6
0
 def __truediv__(self, other, context=None):
     other = self._assert_dividable(other)
     amount = Decimal.__truediv__(self, other)
     return self.__class__(amount)
示例#7
0
 def __truediv__(self, *args, **kw):
     return self.__class__(Decimal.__truediv__(self, *args, **kw))
示例#8
0
class ContractingDecimal:
    def _get_other(self, other):
        if type(other) == ContractingDecimal:
            return other._d
        elif type(other) == float or type(other) == int:
            return Decimal(str(other))
        return other

    def __init__(self, a):
        if type(a) == float or type(a) == int:
            a = str(a)

        self._d = Decimal(a)

    def __bool__(self):
        return self._d > 0

    def __eq__(self, other):
        return self._d.__eq__(self._get_other(other))

    def __lt__(self, other):
        return self._d.__lt__(self._get_other(other))

    def __le__(self, other):
        return self._d.__le__(self._get_other(other))

    def __gt__(self, other):
        return self._d.__gt__(self._get_other(other))

    def __ge__(self, other):
        return self._d.__ge__(self._get_other(other))

    def __str__(self):
        return self._d.__str__()

    def __neg__(self):
        return self._d.__neg__()

    def __pos__(self):
        return self._d.__pos__()

    def __abs__(self):
        return self._d.__abs__()

    def __add__(self, other):
        x = self._d.__add__(self._get_other(other))
        return fix_precision(x)

    def __radd__(self, other):
        return fix_precision(self._d.__radd__(self._get_other(other)))

    def __sub__(self, other):
        return fix_precision(self._d.__sub__(self._get_other(other)))

    def __rsub__(self, other):
        return fix_precision(self._d.__rsub__(self._get_other(other)))

    def __mul__(self, other):
        return fix_precision(self._d.__mul__(self._get_other(other)))

    def __rmul__(self, other):
        return fix_precision(self._d.__rmul__(self._get_other(other)))

    def __truediv__(self, other):
        return fix_precision(self._d.__truediv__(self._get_other(other)))

    def __rtruediv__(self, other):
        return fix_precision(self._d.__rtruediv__(self._get_other(other)))

    def __divmod__(self, other):
        return fix_precision(self._d.__divmod__(self._get_other(other)))

    def __rdivmod__(self, other):
        return fix_precision(self._d.__divmod__(self._get_other(other)))

    def __mod__(self, other):
        return fix_precision(self._d.__mod__(self._get_other(other)))

    def __rmod__(self, other):
        return fix_precision(self._d.__rmod__(self._get_other(other)))

    def __floordiv__(self, other):
        return fix_precision(self._d.__floordiv__(self._get_other(other)))

    def __rfloordiv__(self, other):
        return fix_precision(self._d.__rfloordiv__(self._get_other(other)))

    def __pow__(self, other):
        return fix_precision(self._d.__pow__(self._get_other(other)))

    def __rpow__(self, other):
        return fix_precision(self._d.__rpow__(self._get_other(other)))

    def __int__(self):
        return self._d.__int__()

    def __float__(self):
        raise Exception('Cannot cast Decimal to float.')

    def __repr__(self):
        return self._d.__repr__()
示例#9
0
 def __truediv__(self, other, context=None):
     if PY2:
         return TimeValue(Decimal.__truediv__(self, other, context))
     return TimeValue(Decimal.__truediv__(self, other))
示例#10
0
文件: fixed.py 项目: MLBN/mlt2
 def __truediv__(self, x, context = None):
     return self.__class__(
             Decimal.__truediv__(self, Decimal(x) ))