From 72b9837af199832e1e5d0795d1c98b7677db9322 Mon Sep 17 00:00:00 2001 From: Justin Kitzes Date: Sun, 21 Oct 2012 13:54:25 -0700 Subject: [PATCH] Intermediate animal files --- 4-SoftwareEngineering/animal-notebook.ipynb | 202 ++++++++++++++++++++ 4-SoftwareEngineering/animals.py | 22 +++ 4-SoftwareEngineering/test_animals.py | 16 ++ 3 files changed, 240 insertions(+) create mode 100644 4-SoftwareEngineering/animal-notebook.ipynb create mode 100644 4-SoftwareEngineering/animals.py create mode 100644 4-SoftwareEngineering/test_animals.py diff --git a/4-SoftwareEngineering/animal-notebook.ipynb b/4-SoftwareEngineering/animal-notebook.ipynb new file mode 100644 index 0000000..8397235 --- /dev/null +++ b/4-SoftwareEngineering/animal-notebook.ipynb @@ -0,0 +1,202 @@ +{ + "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": [ + "---------------------------------------------------------------------------\nAssertionError Traceback (most recent call last)", + "/Users/jkitzes/Desktop/2012-10-ucb/4-SoftwareEngineering/<ipython-input-16-124ea80b1f4f> in <module>()\n 3 return num * num\n 4 \n----> 5 square('hi')\n", + "/Users/jkitzes/Desktop/2012-10-ucb/4-SoftwareEngineering/<ipython-input-16-124ea80b1f4f> in square(num)\n 1 def square(num):\n----> 2 assert (type(num) == int) or (type(num) == float), 'Num must be an int'\n 3 return num * num\n 4 \n 5 square('hi')\n", + "AssertionError: 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 diff --git a/4-SoftwareEngineering/animals.py b/4-SoftwareEngineering/animals.py new file mode 100644 index 0000000..411ba9a --- /dev/null +++ b/4-SoftwareEngineering/animals.py @@ -0,0 +1,22 @@ +def read_animals(filename): + """ + This function will read animal data from a file. + """ + + date = [] + time = [] + animal = [] + number = [] + + f = open(filename, 'r') + + # Go through each line and append each column to the appropriate list + 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) diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py new file mode 100644 index 0000000..0cc9a0a --- /dev/null +++ b/4-SoftwareEngineering/test_animals.py @@ -0,0 +1,16 @@ +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' -- 2.26.2