From d3f7cfc5a6b8cee104b45ebeaad6721ee5da67d4 Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Tue, 19 Jun 2012 19:35:29 -0400 Subject: [PATCH] Adding software engineering example files and notebook to the repository. W. Trevor King: I dropped everything from the original d8acd24 except for the 4-SoftwareEngineering/ additions. Conflicts: .gitignore --- .../SoftwareEngineering.ipynb | 480 ++++++++++++++++++ 4-SoftwareEngineering/animal_count.py | 15 + 4-SoftwareEngineering/animals.py | 56 ++ 4-SoftwareEngineering/demo.py | 3 + 4-SoftwareEngineering/test_animals.py | 54 ++ 5 files changed, 608 insertions(+) create mode 100644 4-SoftwareEngineering/SoftwareEngineering.ipynb create mode 100755 4-SoftwareEngineering/animal_count.py create mode 100644 4-SoftwareEngineering/animals.py create mode 100644 4-SoftwareEngineering/demo.py create mode 100644 4-SoftwareEngineering/test_animals.py diff --git a/4-SoftwareEngineering/SoftwareEngineering.ipynb b/4-SoftwareEngineering/SoftwareEngineering.ipynb new file mode 100644 index 0000000..c5e18d2 --- /dev/null +++ b/4-SoftwareEngineering/SoftwareEngineering.ipynb @@ -0,0 +1,480 @@ +{ + "metadata": { + "name": "SoftwareEngineering" + }, + "nbformat": 2, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": true, + "input": [ + "import glob" + ], + "language": "python", + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "glob.glob('*.txt')" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 2, + "text": [ + "['fergus_animals.txt',", + " 'big_animals.txt',", + " 'dingwall_animals.txt',", + " 'animals.txt',", + " 'macguffin_animals.txt',", + " 'merida_animals.txt']" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "pwd" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 3, + "text": [ + "u'/home/swc/SWC-bootcamp/4-SoftwareEngineering'" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ls" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "animals.txt demo.py \u001b[0m\u001b[01;34mDocumentation\u001b[0m/ merida_animals.txt Untitled0.ipynb", + "big_animals.txt dev_notes.rst fergus_animals.txt README.rst", + "\u001b[01;34mDebugging\u001b[0m/ dingwall_animals.txt macguffin_animals.txt \u001b[01;34mTesting\u001b[0m/" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [ + "import demo" + ], + "language": "python", + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "demo.square(5.5)" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 7, + "text": [ + "30.25" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ls" + ], + "language": "python", + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "animals.py \u001b[0m\u001b[01;34mDebugging\u001b[0m/ dev_notes.rst fergus_animals.txt README.rst", + "animals.txt demo.py dingwall_animals.txt macguffin_animals.txt \u001b[01;34mTesting\u001b[0m/", + "big_animals.txt demo.pyc \u001b[01;34mDocumentation\u001b[0m/ merida_animals.txt Untitled0.ipynb" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [ + "import animals" + ], + "language": "python", + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "animals.read_animals('animals.txt')" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 10, + "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": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "[1, 2, 3] != [2, 1, 3]" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 14, + "text": [ + "True" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "raise ValueError(\"Encountered negative number in Kelvin function.\")" + ], + "language": "python", + "outputs": [ + { + "ename": "ValueError", + "evalue": "Encountered negative number in Kelvin function.", + "output_type": "pyerr", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/home/swc/SWC-bootcamp/4-SoftwareEngineering/\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Encountered negative number in Kelvin function.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mValueError\u001b[0m: Encountered negative number in Kelvin function." + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [], + "prompt_number": 11 + }, + { + "cell_type": "code", + "collapsed": true, + "input": [], + "language": "python", + "outputs": [] + } + ] + } + ] +} \ No newline at end of file diff --git a/4-SoftwareEngineering/animal_count.py b/4-SoftwareEngineering/animal_count.py new file mode 100755 index 0000000..8e449e8 --- /dev/null +++ b/4-SoftwareEngineering/animal_count.py @@ -0,0 +1,15 @@ +#!/usr/bin/python + +import animals + +import sys + +filename = sys.argv[1] +animal = sys.argv[2] + +try: + mean_count = animals.main(filename, animal) +except: + print "There were no", animal + +print "The mean count of", animal, "is", mean_count \ No newline at end of file diff --git a/4-SoftwareEngineering/animals.py b/4-SoftwareEngineering/animals.py new file mode 100644 index 0000000..9c5a7ea --- /dev/null +++ b/4-SoftwareEngineering/animals.py @@ -0,0 +1,56 @@ +def read_animals(filename): + """ + Reads animal count file agreed upon at May 30, meeting. + Must have 4 columns, and no header row. + Columns are date, time, animal name, number seen. + """ + f = open(filename, 'r') + date = [] + time = [] + animal = [] + number = [] + + # iterate over the file one line at a time + for line in f: + d, t, a, n = line.split() + date.append(d) + time.append(t) + animal.append(a) + number.append(int(n)) + return date, time, animal, number + +def mean(l): + """ + Returns the mean value of the given list + """ + sum = 0 + for x in l: + sum = sum + x + return sum / float(len(l)) + +def get_animal(date, time, animal, number, animal_name): + """ + Given lists of dates, times, animals and numbers, return + only those pertaining to the given animal_name. + """ + new_date = [] + new_time = [] + new_number = [] + + # for i in range(len(animal)): + + for d, t, a, n in zip(date, time, animal, number): + if a == animal_name: + new_date.append(d) + new_time.append(t) + new_number.append(n) + + return new_date, new_time, new_number + +def main(filename, animal): + dates, times, animals, counts = read_animals(filename) + dates, times, counts = get_animal(dates, times, animals, counts, animal) + mean_count = mean(counts) + return mean_count + + \ No newline at end of file diff --git a/4-SoftwareEngineering/demo.py b/4-SoftwareEngineering/demo.py new file mode 100644 index 0000000..16be399 --- /dev/null +++ b/4-SoftwareEngineering/demo.py @@ -0,0 +1,3 @@ +def square(x): + return x * x + diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py new file mode 100644 index 0000000..3477246 --- /dev/null +++ b/4-SoftwareEngineering/test_animals.py @@ -0,0 +1,54 @@ +import animals + +def test_read_animals(): + filename = 'animals.txt' + + date, time, animal, count = animals.read_animals(filename) + + ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', + '2011-04-23', '2011-04-23'] + ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46'] + ref_animals = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox'] + ref_counts = [36, 25, 26, 31, 20] + + assert date == ref_date, 'Dates do not match!' + assert count == ref_counts, 'Counts do not match!' + +from numpy import testing + + +def test_mean(): + assert animals.mean([5]) == 5 + assert animals.mean([3, 5]) == 4 + assert animals.mean([1, 2, 3, 4]) == 2.5 + testing.assert_almost_equal(animals.mean([1, 2, 3, 4]), 2.5) + +from nose.tools import raises + +@raises(ZeroDivisionError) +def test_mean_empty_list(): + animals.mean([]) + +def test_get_animal(): + date, time, animal, count = animals.read_animals("animals.txt") + date, time, count = animals.get_animal( + date, time, animal, count, "Elk") + + ref_date = ['2011-04-23', '2011-04-23'] + ref_time = ['14:12', '10:24'] + ref_counts = [25, 26] + + assert date == ref_date, 'Dates do not match!' + assert count == ref_counts, 'Counts do not match!' + +def test_get_missing_animal(): + date, time, animal, count = animals.read_animals("animals.txt") + date, time, count = animals.get_animal( + date, time, animal, count, "Squirrel") + assert date == [] + assert time == [] + assert count == [] + +def test_main(): + mean_count = animals.main("animals.txt", "Elk") + assert mean_count == 25.5 \ No newline at end of file -- 2.26.2