{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-05-05 20:33:45.066477: Hi there!\n",
      "2016-05-05 20:33:45.066477: Hi again!\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "from time import sleep\n",
    "from datetime import datetime\n",
    "\n",
    "def log(message, when=datetime.now()):\n",
    "    print('%s: %s' % (when, message))\n",
    "\n",
    "log('Hi there!')\n",
    "sleep(0.1)\n",
    "log('Hi again!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Write docstring for all func!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 2\n",
    "def log(message, when=None):\n",
    "    \"\"\"Log a message with a timestamp.\n",
    "\n",
    "    Args:\n",
    "        message: Message to print.\n",
    "        when: datetime of when the message occurred.\n",
    "            Defaults to the present time.\n",
    "    \"\"\"\n",
    "    when = datetime.now() if when is None else when\n",
    "    print('%s: %s' % (when, message))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-05-05 20:33:45.183324: Hi there!\n",
      "2016-05-05 20:33:45.285816: Hi again!\n"
     ]
    }
   ],
   "source": [
    "# Example 3\n",
    "log('Hi there!')\n",
    "sleep(0.1)\n",
    "log('Hi again!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 4\n",
    "import json\n",
    "\n",
    "def decode(data, default={}):\n",
    "    try:\n",
    "        return json.loads(data)\n",
    "    except ValueError:\n",
    "        return default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Foo: {'stuff': 5, 'meep': 1}\n",
      "Bar: {'stuff': 5, 'meep': 1}\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "foo = decode('bad data')\n",
    "foo['stuff'] = 5\n",
    "bar = decode('also bad')\n",
    "bar['meep'] = 1\n",
    "print('Foo:', foo)\n",
    "print('Bar:', bar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "assert foo is bar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "def decode(data, default=None):\n",
    "    \"\"\"Load JSON data from a string.\n",
    "\n",
    "    Args:\n",
    "        data: JSON data to decode.\n",
    "        default: Value to return if decoding fails.\n",
    "            Defaults to an empty dictionary.\n",
    "    \"\"\"\n",
    "    if default is None:\n",
    "        default = {}\n",
    "    try:\n",
    "        return json.loads(data)\n",
    "    except ValueError:\n",
    "        return default"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Foo: {'stuff': 5}\n",
      "Bar: {'meep': 1}\n"
     ]
    }
   ],
   "source": [
    "# Example 8\n",
    "foo = decode('bad data')\n",
    "foo['stuff'] = 5\n",
    "bar = decode('also bad')\n",
    "bar['meep'] = 1\n",
    "print('Foo:', foo)\n",
    "print('Bar:', bar)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
