--- /dev/null
+{
+ "metadata": {
+ "name": "animal-notebook"
+ },
+ "nbformat": 2,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Four key skills:",
+ "",
+ "* Putting functions into modules, and importing",
+ "* Documenting code",
+ "* Testing",
+ "* Debugging"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "### Goal",
+ "",
+ "Create a command line script, written in Python, that will print the average group size",
+ "for each species in a given animals file."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "!head animals.txt"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2011-04-22 21:06 Grizzly 36",
+ "2011-04-23 14:12 Elk 25",
+ "2011-04-23 10:24 Elk 26",
+ "2011-04-23 20:08 Wolverine 31",
+ "2011-04-23 18:46 Muskox 20"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": true,
+ "input": [
+ "import numpy"
+ ],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "code",
+ "collapsed": true,
+ "input": [
+ "def read_animals(filename):",
+ " date = []",
+ " time = []",
+ " animal = []",
+ " number = []",
+ " f = open(filename, 'r')",
+ " for line in f:",
+ " d, t, a, n = line.split()",
+ " date.append(d)",
+ " time.append(t)",
+ " animal.append(a)",
+ " number.append(int(n))",
+ " f.close()",
+ " return (date, time, animal, number)"
+ ],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": true,
+ "input": [
+ "d, t, a, n = read_animals('animals.txt')"
+ ],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "d"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 5,
+ "text": [
+ "['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": true,
+ "input": [
+ "import animals"
+ ],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "animals.read_animals('animals.txt')"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "pyout",
+ "prompt_number": 7,
+ "text": [
+ "(['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23'],",
+ " ['21:06', '14:12', '10:24', '20:08', '18:46'],",
+ " ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox'],",
+ " [36, 25, 26, 31, 20])"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "!cat animals.txt"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2011-04-22 21:06 Grizzly 36",
+ "2011-04-23 14:12 Elk 25",
+ "2011-04-23 10:24 Elk 26",
+ "2011-04-23 20:08 Wolverine 31",
+ "2011-04-23 18:46 Muskox 20"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def square(num):",
+ " assert (type(num) == int) or (type(num) == float), 'Num must be an int'",
+ " return num * num",
+ "",
+ "square('hi')"
+ ],
+ "language": "python",
+ "outputs": [
+ {
+ "ename": "AssertionError",
+ "evalue": "Num must be an int",
+ "output_type": "pyerr",
+ "traceback": [
+ "<span class=\"ansired\">---------------------------------------------------------------------------</span>\n<span class=\"ansired\">AssertionError</span> Traceback (most recent call last)",
+ "<span class=\"ansigreen\">/Users/jkitzes/Desktop/2012-10-ucb/4-SoftwareEngineering/<ipython-input-16-124ea80b1f4f></span> in <span class=\"ansicyan\"><module></span><span class=\"ansiblue\">()</span>\n<span class=\"ansigreen\"> 3</span> <span class=\"ansigreen\">return</span> num <span class=\"ansiblue\">*</span> num<span class=\"ansiblue\"></span>\n<span class=\"ansigreen\"> 4</span> <span class=\"ansiblue\"></span>\n<span class=\"ansigreen\">----> 5</span><span class=\"ansired\"> </span>square<span class=\"ansiblue\">(</span><span class=\"ansiblue\">'hi'</span><span class=\"ansiblue\">)</span><span class=\"ansiblue\"></span>\n",
+ "<span class=\"ansigreen\">/Users/jkitzes/Desktop/2012-10-ucb/4-SoftwareEngineering/<ipython-input-16-124ea80b1f4f></span> in <span class=\"ansicyan\">square</span><span class=\"ansiblue\">(num)</span>\n<span class=\"ansigreen\"> 1</span> <span class=\"ansigreen\">def</span> square<span class=\"ansiblue\">(</span>num<span class=\"ansiblue\">)</span><span class=\"ansiblue\">:</span><span class=\"ansiblue\"></span>\n<span class=\"ansigreen\">----> 2</span><span class=\"ansired\"> </span><span class=\"ansigreen\">assert</span> <span class=\"ansiblue\">(</span>type<span class=\"ansiblue\">(</span>num<span class=\"ansiblue\">)</span> <span class=\"ansiblue\">==</span> int<span class=\"ansiblue\">)</span> <span class=\"ansigreen\">or</span> <span class=\"ansiblue\">(</span>type<span class=\"ansiblue\">(</span>num<span class=\"ansiblue\">)</span> <span class=\"ansiblue\">==</span> float<span class=\"ansiblue\">)</span><span class=\"ansiblue\">,</span> <span class=\"ansiblue\">'Num must be an int'</span><span class=\"ansiblue\"></span>\n<span class=\"ansigreen\"> 3</span> <span class=\"ansigreen\">return</span> num <span class=\"ansiblue\">*</span> num<span class=\"ansiblue\"></span>\n<span class=\"ansigreen\"> 4</span> <span class=\"ansiblue\"></span>\n<span class=\"ansigreen\"> 5</span> square<span class=\"ansiblue\">(</span><span class=\"ansiblue\">'hi'</span><span class=\"ansiblue\">)</span><span class=\"ansiblue\"></span>\n",
+ "<span class=\"ansired\">AssertionError</span>: Num must be an int"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "code",
+ "collapsed": true,
+ "input": [],
+ "language": "python",
+ "outputs": [],
+ "prompt_number": " "
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
--- /dev/null
+import animals
+
+def test_read_animals_date():
+ reference_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23',
+ '2011-04-23']
+
+ date, time, animal, number = animals.read_animals('animals.txt')
+
+ assert date == reference_date, 'Date is wrong'
+
+def test_read_animals_time():
+ reference_time = ['22:06', '14:12', '10:24', '20:08', '18:46']
+
+ date, time, animal, number = animals.read_animals('animals.txt')
+
+ assert time == reference_time, 'Time is wrong'