Adding examples from second day software engineering lecture. test-davis-2012-10-lbl
authorMatt Davis <jiffyclub@gmail.com>
Fri, 19 Oct 2012 15:38:01 +0000 (11:38 -0400)
committerW. Trevor King <wking@tremily.us>
Sat, 9 Nov 2013 17:01:45 +0000 (09:01 -0800)
4-SoftwareEngineering/TestStuff.ipynb [new file with mode: 0644]
4-SoftwareEngineering/animals.py [new file with mode: 0644]
4-SoftwareEngineering/argv [new file with mode: 0755]
4-SoftwareEngineering/meananimals [new file with mode: 0755]
4-SoftwareEngineering/spreadsheet.csv [new file with mode: 0644]
4-SoftwareEngineering/test_animals.py [new file with mode: 0644]

diff --git a/4-SoftwareEngineering/TestStuff.ipynb b/4-SoftwareEngineering/TestStuff.ipynb
new file mode 100644 (file)
index 0000000..204cd9d
--- /dev/null
@@ -0,0 +1,441 @@
+{
+ "metadata": {
+  "name": "TestStuff"
+ }, 
+ "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": 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": false, 
+     "input": [
+      "date, time, animal, number = read_animals('animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 5
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "date"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 6, 
+       "text": [
+        "['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']"
+       ]
+      }
+     ], 
+     "prompt_number": 6
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "time"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 7, 
+       "text": [
+        "['21:06', '14:12', '10:24', '20:08', '18:46']"
+       ]
+      }
+     ], 
+     "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/            fergus_animals.txt     \u001b[01;34mTesting\u001b[0m/", 
+        "animals.py~      dev_notes.rst         macguffin_animals.txt  TestStuff.ipynb", 
+        "animals.txt      dingwall_animals.txt  merida_animals.txt", 
+        "big_animals.txt  \u001b[01;34mDocumentation\u001b[0m/        README.rst"
+       ]
+      }
+     ], 
+     "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": [
+      "!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": 11
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "14./4."
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 12, 
+       "text": [
+        "3.5"
+       ]
+      }
+     ], 
+     "prompt_number": 12
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "def filter_animals(date, time, animal, number, species):", 
+      "    \"\"\"", 
+      "    Return the subset of date, time, andimal, and number that apply", 
+      "    to species.", 
+      "", 
+      "    \"\"\"", 
+      "    sub_date = []", 
+      "    sub_time = []", 
+      "    sub_animal = []", 
+      "    sub_number = []", 
+      "", 
+      "    for i, a in enumerate(animal):", 
+      "        if a == species:", 
+      "            sub_date.append(date[i])", 
+      "            sub_time.append(time[i])", 
+      "            sub_animal.append(a)", 
+      "            sub_number.append(number[i])", 
+      "    ", 
+      "    return sub_date, sub_time, sub_animal, sub_number"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 14
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "date"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 15, 
+       "text": [
+        "['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']"
+       ]
+      }
+     ], 
+     "prompt_number": 15
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "animal"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 16, 
+       "text": [
+        "['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']"
+       ]
+      }
+     ], 
+     "prompt_number": 16
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "filter_animals(date, time, animal, number, 'Elk')"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 19, 
+       "text": [
+        "(['2011-04-23', '2011-04-23'], ['14:12', '10:24'], ['Elk', 'Elk'], [25, 26])"
+       ]
+      }
+     ], 
+     "prompt_number": 19
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "import nose"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 20
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "nose.tools.raises?"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 21
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "!cat spreadsheet.csv"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "stream", 
+       "stream": "stdout", 
+       "text": [
+        "firstcol,secondcol,thirdcol", 
+        "1,2,3", 
+        "11,12,13", 
+        "14,15,16"
+       ]
+      }
+     ], 
+     "prompt_number": 22
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "from matplotlib.mlab import csv2rec"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 23
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "a = csv2rec('spreadsheet.csv')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 29
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "csv2rec?", 
+      "a"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 32, 
+       "text": [
+        "rec.array([(1, 2, 3), (11, 12, 13), (14, 15, 16)], ", 
+        "      dtype=[('firstcol', '<i4'), ('secondcol', '<i4'), ('thirdcol', '<i4')])"
+       ]
+      }
+     ], 
+     "prompt_number": 32
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "a[(a['firstcol'] > 10) & (a['secondcol'] == 12)]"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "pyout", 
+       "prompt_number": 36, 
+       "text": [
+        "rec.array([(11, 12, 13)], ", 
+        "      dtype=[('firstcol', '<i4'), ('secondcol', '<i4'), ('thirdcol', '<i4')])"
+       ]
+      }
+     ], 
+     "prompt_number": 36
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "l = ['hi', 'hello', 'yo']", 
+      "q = [10, 11, 12]", 
+      "", 
+      "for i, word in enumerate(l):", 
+      "    print word, q[i]", 
+      "", 
+      "for i in range(len(l)):", 
+      "    print l[i], q[i]", 
+      "", 
+      "for word, number in zip(l, q):", 
+      "    print word, number", 
+      "    ", 
+      "l.index('hello')"
+     ], 
+     "language": "python", 
+     "outputs": [
+      {
+       "output_type": "stream", 
+       "stream": "stdout", 
+       "text": [
+        "hi 10", 
+        "hello 11", 
+        "yo 12", 
+        "hi 10", 
+        "hello 11", 
+        "yo 12", 
+        "hi 10", 
+        "hello 11", 
+        "yo 12"
+       ]
+      }, 
+      {
+       "output_type": "pyout", 
+       "prompt_number": 41, 
+       "text": [
+        "1"
+       ]
+      }
+     ], 
+     "prompt_number": 41
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [], 
+     "language": "python", 
+     "outputs": []
+    }
+   ]
+  }
+ ]
+}
\ No newline at end of file
diff --git a/4-SoftwareEngineering/animals.py b/4-SoftwareEngineering/animals.py
new file mode 100644 (file)
index 0000000..033f4af
--- /dev/null
@@ -0,0 +1,65 @@
+def read_animals(filename):
+    """
+    Return four lists of the dates, times, animals, and counts
+    from 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
+    
+
+def mean(l):
+    """
+    Return the mean of a list of numbers.
+    
+    """
+    if len(l) == 0:
+        return 0
+    
+    return float(sum(l)) / len(l)
+
+def filter_animals(date, time, animal, number, species):
+    """
+    Return the subset of date, time, andimal, and number that apply
+    to species.
+
+    """
+    sub_date = []
+    sub_time = []
+    sub_animal = []
+    sub_number = []
+
+    for i, a in enumerate(animal):
+        if a == species:
+            sub_date.append(date[i])
+            sub_time.append(time[i])
+            sub_animal.append(a)
+            sub_number.append(number[i])
+    
+    return sub_date, sub_time, sub_animal, sub_number
+
+    
+def mean_animals_sighted(filename, species):
+    date, time, animal, number = \
+        read_animals(filename)
+    date, time, animal, number = \
+        filter_animals(date, time, animal, number, species)
+    
+    if len(number) == 0:
+        raise ValueError(species + ' is not in file.')
+    
+    mean_sighted = mean(number)
+    return mean_sighted
+    
+    
\ No newline at end of file
diff --git a/4-SoftwareEngineering/argv b/4-SoftwareEngineering/argv
new file mode 100755 (executable)
index 0000000..114c630
--- /dev/null
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+
+import sys
+
+print sys.argv
diff --git a/4-SoftwareEngineering/meananimals b/4-SoftwareEngineering/meananimals
new file mode 100755 (executable)
index 0000000..d5b0d37
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+
+import sys
+
+import animals
+
+if len(sys.argv) != 3:
+    print 'Usage: meananimals filename species'
+    sys.exit()
+
+filename = sys.argv[1]
+species = sys.argv[2]
+
+print animals.mean_animals_sighted(filename, species)
diff --git a/4-SoftwareEngineering/spreadsheet.csv b/4-SoftwareEngineering/spreadsheet.csv
new file mode 100644 (file)
index 0000000..ebbdfa6
--- /dev/null
@@ -0,0 +1,4 @@
+firstcol,secondcol,thirdcol
+1,2,3
+11,12,13
+14,15,16
diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py
new file mode 100644 (file)
index 0000000..138a2de
--- /dev/null
@@ -0,0 +1,71 @@
+import animals
+
+def test_read_animals():
+    date, time, animal, number = animals.read_animals('animals.txt')
+  
+    reference_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']
+    reference_time = ['21:06', '14:12', '10:24', '20:08', '18:46']
+
+    assert date == reference_date, 'The date is wrong!'
+    assert time == reference_time, 'The time is wrong!'
+
+    
+def test_number():
+    date, time, animal, number = animals.read_animals('animals.txt')
+    
+    ref_numbers = [36, 25, 26, 31, 20]
+    
+    assert number == ref_numbers, 'The numbers are wrong!'
+    
+    
+def test_mean1():
+    l = [1]
+    assert animals.mean(l) == 1
+
+   
+def test_mean2():
+    l = [3, 4, 5]
+    assert animals.mean(l) == 4
+
+    
+def test_mean3():
+    l = []
+    assert animals.mean(l) == 0
+
+    
+def test_mean4():
+    l = [3.0, 4.0, 5.0]
+    assert animals.mean(l) == 4
+
+    
+def test_mean5():
+    l = [2, 3, 5, 4]
+    assert animals.mean(l) == 3.5
+
+    
+def test_mean_animals_sighted1():
+     f = 'animals.txt'
+     a = 'Grizzly'
+     
+     m = animals.mean_animals_sighted(f, a)
+     
+     assert m == 36
+
+     
+def test_mean_animals_sighted2():
+     f = 'animals.txt'
+     a = 'Elk'
+     
+     m = animals.mean_animals_sighted(f, a)
+     
+     assert m == 25.5
+
+
+import nose
+@nose.tools.raises(ValueError)
+def test_mean_animals_sighted3():
+     f = 'animals.txt'
+     a = 'Pangolin'
+     
+     m = animals.mean_animals_sighted(f, a)
+     
\ No newline at end of file