#!/usr/bin/env python # coding: utf-8 # In[1]: # 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 # In[2]: # Example 1 from time import sleep from datetime import datetime def log(message, when=datetime.now()): print('%s: %s' % (when, message)) log('Hi there!') sleep(0.1) log('Hi again!') # ## Write docstring for all func! # In[3]: # Example 2 def log(message, when=None): """Log a message with a timestamp. Args: message: Message to print. when: datetime of when the message occurred. Defaults to the present time. """ when = datetime.now() if when is None else when print('%s: %s' % (when, message)) # In[4]: # Example 3 log('Hi there!') sleep(0.1) log('Hi again!') # In[5]: # Example 4 import json def decode(data, default={}): try: return json.loads(data) except ValueError: return default # In[6]: # Example 5 foo = decode('bad data') foo['stuff'] = 5 bar = decode('also bad') bar['meep'] = 1 print('Foo:', foo) print('Bar:', bar) # In[7]: # Example 6 assert foo is bar # In[8]: # Example 7 def decode(data, default=None): """Load JSON data from a string. Args: data: JSON data to decode. default: Value to return if decoding fails. Defaults to an empty dictionary. """ if default is None: default = {} try: return json.loads(data) except ValueError: return default # In[9]: # Example 8 foo = decode('bad data') foo['stuff'] = 5 bar = decode('also bad') bar['meep'] = 1 print('Foo:', foo) print('Bar:', bar)