当前位置: 首页>>代码示例>>Python>>正文


Python decimal.DivisionByZero方法代码示例

本文整理汇总了Python中decimal.DivisionByZero方法的典型用法代码示例。如果您正苦于以下问题:Python decimal.DivisionByZero方法的具体用法?Python decimal.DivisionByZero怎么用?Python decimal.DivisionByZero使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在decimal的用法示例。


在下文中一共展示了decimal.DivisionByZero方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: safe_div

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
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 
开发者ID:naripok,项目名称:cryptotrader,代码行数:18,代码来源:utils.py

示例2: test_arith_series_with_array

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
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 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_decimal.py

示例3: _percent_delta

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
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 
开发者ID:project-koku,项目名称:koku,代码行数:19,代码来源:queries.py

示例4: array_normalize

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
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 
开发者ID:naripok,项目名称:cryptotrader,代码行数:18,代码来源:utils.py

示例5: good_case2

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
def good_case2():
    """decimal.DivisionByZero is defined in C on Python 3."""
    import decimal
    raise decimal.DivisionByZero(4) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:6,代码来源:invalid_exceptions_raised.py

示例6: add_current_month_deltas

# 需要导入模块: import decimal [as 别名]
# 或者: from decimal import DivisionByZero [as 别名]
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 
开发者ID:project-koku,项目名称:koku,代码行数:28,代码来源:query_handler.py


注:本文中的decimal.DivisionByZero方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。