本文整理汇总了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
示例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
示例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
示例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
示例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)
示例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