# %load ../example_code/item_21.py
#!/usr/bin/env python3
# Copyright 2014 Brett Slatkin, Pearson Education Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Preamble to mimick book environment
import logging
from pprint import pprint
from sys import stdout as STDOUT
# Example 1
def safe_division(number, divisor, ignore_overflow,
ignore_zero_division):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return float('inf')
else:
raise
# Example 2
result = safe_division(1.0, 10**500, True, False)
print(result)
assert result is 0
0
# Example 3
result = safe_division(1.0, 0, False, True)
print(result)
assert result == float('inf')
inf
# Example 4
def safe_division_b(number, divisor,
ignore_overflow=False,
ignore_zero_division=False):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return float('inf')
else:
raise
# Example 5
assert safe_division_b(1.0, 10**500, ignore_overflow=True) is 0
assert safe_division_b(1.0, 0, ignore_zero_division=True) == float('inf')
# Example 6
assert safe_division_b(1.0, 10**500, True, False) is 0
The * symbol in the argument list indicates the end of positional arguments and the beginning of keyword-only arguments.
# Example 7
def safe_division_c(number, divisor, *,
ignore_overflow=False,
ignore_zero_division=False):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
return float('inf')
else:
raise
# Example 8
safe_division_c(1.0, 10**500, True, False)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-9-5deed29051ad> in <module>() 1 # Example 8 ----> 2 safe_division_c(1.0, 10**500, True, False) TypeError: safe_division_c() takes 2 positional arguments but 4 were given
# Example 9
safe_division_c(1.0, 0, ignore_zero_division=True) # No exception
try:
safe_division_c(1.0, 0)
assert False
except ZeroDivisionError:
pass # Expected
# Example 10
def print_args(*args, **kwargs):
print('Positional:', args)
print('Keyword: ', kwargs)
print_args(1, 2, foo='bar', stuff='meep')
Positional: (1, 2) Keyword: {'foo': 'bar', 'stuff': 'meep'}
# Example 11
def safe_division_d(number, divisor, **kwargs):
ignore_overflow = kwargs.pop('ignore_overflow', False)
ignore_zero_div = kwargs.pop('ignore_zero_division', False)
if kwargs:
raise TypeError('Unexpected **kwargs: %r' % kwargs)
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_div:
return float('inf')
else:
raise
assert safe_division_d(1.0, 10) == 0.1
assert safe_division_d(1.0, 0, ignore_zero_division=True) == float('inf')
assert safe_division_d(1.0, 10**500, ignore_overflow=True) is 0
# Example 12
safe_division_d(1.0, 0, False, True)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-13-7d838ddbc24c> in <module>() 1 # Example 12 ----> 2 safe_division_d(1.0, 0, False, True) TypeError: safe_division_d() takes 2 positional arguments but 4 were given
# Example 13
safe_division_d(0.0, 0, unexpected=True)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-14-26eb839a9523> in <module>() 1 # Example 13 ----> 2 safe_division_d(0.0, 0, unexpected=True) <ipython-input-12-b4a27a867988> in safe_division_d(number, divisor, **kwargs) 4 ignore_zero_div = kwargs.pop('ignore_zero_division', False) 5 if kwargs: ----> 6 raise TypeError('Unexpected **kwargs: %r' % kwargs) 7 try: 8 return number / divisor TypeError: Unexpected **kwargs: {'unexpected': True}