#!/usr/bin/env python # coding: utf-8 # In[1]: # %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 # ## Too many args and confusing. # In[2]: # 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 # In[3]: # Example 2 result = safe_division(1.0, 10**500, True, False) print(result) assert result is 0 # In[4]: # Example 3 result = safe_division(1.0, 0, False, True) print(result) assert result == float('inf') # In[5]: # 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 # ## Can use keyword args, but there is no way to force it. # In[6]: # 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') # In[7]: # Example 6 assert safe_division_b(1.0, 10**500, True, False) is 0 # ## We can force it using *. WONDERFUL!! # The * symbol in the argument list indicates the end of positional arguments and the beginning of keyword-only arguments. # In[8]: # 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 # In[9]: # Example 8 safe_division_c(1.0, 10**500, True, False) # In[10]: # 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 # In[11]: # Example 10 def print_args(*args, **kwargs): print('Positional:', args) print('Keyword: ', kwargs) print_args(1, 2, foo='bar', stuff='meep') # ## In Python2, it's complicated, but doable. # In[12]: # 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 # In[13]: # Example 12 safe_division_d(1.0, 0, False, True) # In[14]: # Example 13 safe_division_d(0.0, 0, unexpected=True)