本文整理匯總了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