Python decimal.DivisionByZero() Examples

The following are 9 code examples of decimal.DivisionByZero(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module decimal , or try the search function .
Example #1
Source File: test_decimal.py    From recruit with Apache License 2.0 8 votes vote down vote up
def test_arith_series_with_array(self, data, all_arithmetic_operators):
        op_name = all_arithmetic_operators
        s = pd.Series(data)

        context = decimal.getcontext()
        divbyzerotrap = context.traps[decimal.DivisionByZero]
        invalidoptrap = context.traps[decimal.InvalidOperation]
        context.traps[decimal.DivisionByZero] = 0
        context.traps[decimal.InvalidOperation] = 0

        # Decimal supports ops with int, but not float
        other = pd.Series([int(d * 100) for d in data])
        self.check_opname(s, op_name, other)

        if "mod" not in op_name:
            self.check_opname(s, op_name, s * 2)

        self.check_opname(s, op_name, 0)
        self.check_opname(s, op_name, 5)
        context.traps[decimal.DivisionByZero] = divbyzerotrap
        context.traps[decimal.InvalidOperation] = invalidoptrap 
Example #2
Source File: queries.py    From koku with GNU Affero General Public License v3.0 6 votes vote down vote up
def _percent_delta(self, a, b):
        """Calculate a percent delta.

        Args:
            a (int or float or Decimal) the current value
            b (int or float or Decimal) the previous value

        Returns:
            (Decimal) (a - b) / b * 100

            Returns Decimal(0) if b is zero.

        """
        try:
            return Decimal((a - b) / b * 100)
        except (DivisionByZero, ZeroDivisionError, InvalidOperation):
            return None 
Example #3
Source File: utils.py    From cryptotrader with MIT License 6 votes vote down vote up
def safe_div(x, y, eps=(dec_eps, 1e-8)):
    try:
        out = dec_con.divide(x, y)
    except DivisionByZero:
        out = dec_con.divide(x, y + eps[0])
    except InvalidOperation:
        out = dec_con.divide(x, y + eps[0])
    except TypeError:
        try:
            out = x / y
        except ZeroDivisionError:
            out = x / (y + eps[1])
        except TypeError:
            out = float(x) / (float(y) + eps[1])

    return out 
Example #4
Source File: utils.py    From cryptotrader with MIT License 6 votes vote down vote up
def array_normalize(x, float=True):
    out = convert_to.decimal(x)

    try:
        out /= out.sum()
    except DivisionByZero:
        out /= (out.sum() + dec_eps)
    except InvalidOperation:
        out /= (out.sum() + dec_eps)

    out[-1] += dec_con.create_decimal('1.00000000') - out.sum()

    if float:
        return np.float64(out)
    else:
        return out 
Example #5
Source File: test_decimal.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_arith_series_with_array(self, data, all_arithmetic_operators):
        op_name = all_arithmetic_operators
        s = pd.Series(data)

        context = decimal.getcontext()
        divbyzerotrap = context.traps[decimal.DivisionByZero]
        invalidoptrap = context.traps[decimal.InvalidOperation]
        context.traps[decimal.DivisionByZero] = 0
        context.traps[decimal.InvalidOperation] = 0

        # Decimal supports ops with int, but not float
        other = pd.Series([int(d * 100) for d in data])
        self.check_opname(s, op_name, other)

        if "mod" not in op_name:
            self.check_opname(s, op_name, s * 2)

        self.check_opname(s, op_name, 0)
        self.check_opname(s, op_name, 5)
        context.traps[decimal.DivisionByZero] = divbyzerotrap
        context.traps[decimal.InvalidOperation] = invalidoptrap 
Example #6
Source File: test_decimal.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_arith_series_with_array(self, data, all_arithmetic_operators):
        op_name = all_arithmetic_operators
        s = pd.Series(data)

        context = decimal.getcontext()
        divbyzerotrap = context.traps[decimal.DivisionByZero]
        invalidoptrap = context.traps[decimal.InvalidOperation]
        context.traps[decimal.DivisionByZero] = 0
        context.traps[decimal.InvalidOperation] = 0

        # Decimal supports ops with int, but not float
        other = pd.Series([int(d * 100) for d in data])
        self.check_opname(s, op_name, other)

        if "mod" not in op_name:
            self.check_opname(s, op_name, s * 2)

        self.check_opname(s, op_name, 0)
        self.check_opname(s, op_name, 5)
        context.traps[decimal.DivisionByZero] = divbyzerotrap
        context.traps[decimal.InvalidOperation] = invalidoptrap 
Example #7
Source File: invalid_exceptions_raised.py    From python-netsurv with MIT License 5 votes vote down vote up
def good_case2():
    """decimal.DivisionByZero is defined in C on Python 3."""
    import decimal
    raise decimal.DivisionByZero(4) 
Example #8
Source File: invalid_exceptions_raised.py    From python-netsurv with MIT License 5 votes vote down vote up
def good_case2():
    """decimal.DivisionByZero is defined in C on Python 3."""
    import decimal
    raise decimal.DivisionByZero(4) 
Example #9
Source File: query_handler.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def add_current_month_deltas(self, query_data, query_sum):
        """Add delta to the resultset using current month comparisons."""
        delta_field_one, delta_field_two = self._delta.split("__")

        for row in query_data:
            delta_value = Decimal(row.get(delta_field_one, 0)) - Decimal(row.get(delta_field_two, 0))  # noqa: W504

            row["delta_value"] = delta_value
            try:
                row["delta_percent"] = row.get(delta_field_one, 0) / row.get(delta_field_two, 0) * 100  # noqa: W504
            except (DivisionByZero, ZeroDivisionError, InvalidOperation):
                row["delta_percent"] = None

        total_delta = Decimal(query_sum.get(delta_field_one, 0)) - Decimal(  # noqa: W504
            query_sum.get(delta_field_two, 0)
        )
        try:
            total_delta_percent = (
                query_sum.get(delta_field_one, 0) / query_sum.get(delta_field_two, 0) * 100  # noqa: W504
            )
        except (DivisionByZero, ZeroDivisionError, InvalidOperation):
            total_delta_percent = None

        self.query_delta = {"value": total_delta, "percent": total_delta_percent}

        return query_data