Adding day 2 software engineering stuff to repo. test-davis-2012-10-caltech
authorMatt Davis <jiffyclub@gmail.com>
Wed, 24 Oct 2012 23:42:31 +0000 (16:42 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 9 Nov 2013 17:17:29 +0000 (09:17 -0800)
4-SoftwareEngineering/TestingDemos.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/test_animals.py [new file with mode: 0644]

diff --git a/4-SoftwareEngineering/TestingDemos.ipynb b/4-SoftwareEngineering/TestingDemos.ipynb
new file mode 100644 (file)
index 0000000..2e7e951
--- /dev/null
@@ -0,0 +1,199 @@
+{
+ "metadata": {
+  "name": "TestingDemos"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "ls"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "\u001b[34mDebugging\u001b[m\u001b[m/             TestingDemos.ipynb     dev_notes.rst          merida_animals.txt\r\n",
+        "\u001b[34mDocumentation\u001b[m\u001b[m/         animals.py             dingwall_animals.txt\r\n",
+        "README.rst             animals.txt            fergus_animals.txt\r\n",
+        "\u001b[34mTesting\u001b[m\u001b[m/               big_animals.txt        macguffin_animals.txt\r\n"
+       ]
+      }
+     ],
+     "prompt_number": 1
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "import animals"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 2
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "animals.read_animals('animals.txt')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 3,
+       "text": [
+        "(['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23'],\n",
+        " ['21:06', '14:12', '10:24', '20:08', '18:46'],\n",
+        " ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox'],\n",
+        " ['36', '25', '26', '31', '20'])"
+       ]
+      }
+     ],
+     "prompt_number": 3
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "sum([1, 2, 3, 4])"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 4,
+       "text": [
+        "10"
+       ]
+      }
+     ],
+     "prompt_number": 4
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "import asciitable"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 5
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "asciitable.__file__"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 6,
+       "text": [
+        "'/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/asciitable/__init__.pyc'"
+       ]
+      }
+     ],
+     "prompt_number": 6
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "asciitable.read('animals.txt')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 7,
+       "text": [
+        "rec.array([('2011-04-22', '21:06', 'Grizzly', 36),\n",
+        "       ('2011-04-23', '14:12', 'Elk', 25),\n",
+        "       ('2011-04-23', '10:24', 'Elk', 26),\n",
+        "       ('2011-04-23', '20:08', 'Wolverine', 31),\n",
+        "       ('2011-04-23', '18:46', 'Muskox', 20)], \n",
+        "      dtype=[('col1', '|S10'), ('col2', '|S5'), ('col3', '|S9'), ('col4', '<i8')])"
+       ]
+      }
+     ],
+     "prompt_number": 7
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "data = asciitable.read('animals.txt')"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [],
+     "prompt_number": 8
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "data['col4']"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 9,
+       "text": [
+        "array([36, 25, 26, 31, 20])"
+       ]
+      }
+     ],
+     "prompt_number": 9
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "data[2]"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "pyout",
+       "prompt_number": 10,
+       "text": [
+        "('2011-04-23', '10:24', 'Elk', 26)"
+       ]
+      }
+     ],
+     "prompt_number": 10
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
+    }
+   ],
+   "metadata": {}
+  }
+ ]
+}
\ 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..3f98aa7
--- /dev/null
@@ -0,0 +1,45 @@
+def read_animals(filename):
+    date = []
+    time = []
+    animal = []
+    count = []
+    with open(filename, 'r') as f:
+        for line in f:
+            d, t, a, c = line.split()
+            date.append(d)
+            time.append(t)
+            animal.append(a)
+            count.append(int(c))
+    return date, time, animal, count
+
+
+def mean(l):
+    if not l:
+        return 0
+
+    return float(sum(l)) / len(l)
+
+
+def filter_animals(date, time, animal, count, species):
+    """
+    Filter a set of survey data based on
+    a species name. Returns four filtered lists.
+
+    """
+    r_date = []
+    r_time = []
+    r_animal = []
+    r_count = []
+    for i, a in enumerate(animal):
+        if a == species:
+            r_date.append(date[i])
+            r_time.append(time[i])
+            r_animal.append(animal[i])
+            r_count.append(count[i])
+    return r_date, r_time, r_animal, r_count
+
+
+def mean_animals(filename, species):
+    d, t, a, c = read_animals(filename)
+    d, t, a, c = filter_animals(d, t, a, c, species)
+    return mean(c)
diff --git a/4-SoftwareEngineering/argv b/4-SoftwareEngineering/argv
new file mode 100755 (executable)
index 0000000..eda765c
--- /dev/null
@@ -0,0 +1,3 @@
+#!/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..2314fb3
--- /dev/null
@@ -0,0 +1,12 @@
+#!/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(filename, species)
diff --git a/4-SoftwareEngineering/test_animals.py b/4-SoftwareEngineering/test_animals.py
new file mode 100644 (file)
index 0000000..640b669
--- /dev/null
@@ -0,0 +1,75 @@
+import animals
+
+
+def test_read_animals_times():
+    date, time, animal, count = \
+        animals.read_animals('animals.txt')
+    ref_t = ['21:06', '14:12', '10:24', '20:08', '18:46']
+    assert time == ref_t, "Times don't match!"
+
+
+def test_read_animals_counts():
+    date, time, animal, count = \
+        animals.read_animals('animals.txt')
+    ref_c = [36, 25, 26, 31, 20]
+    assert count == ref_c, "Counts don't match!"
+
+
+def test_mean1():
+    l = [1]
+    assert animals.mean(l) == 1
+
+
+def test_mean2():
+    l = [1, 2, 3]
+    assert animals.mean(l) == 2
+
+
+def test_mean3():
+    l = [2, 3, 5, 4]
+    assert animals.mean(l) == 3.5
+
+
+def test_mean4():
+    l = []
+    assert animals.mean(l) == 0
+
+
+def test_filter_animals1():
+    date, time, animal, count = \
+        animals.read_animals('animals.txt')
+    date, time, animal, count = \
+        animals.filter_animals(date, time,
+                               animal, count,
+                               'Grizzly')
+    assert date == ['2011-04-22']
+    assert time == ['21:06']
+    assert animal == ['Grizzly']
+    assert count == [36]
+
+
+def test_filter_animals2():
+    date, time, animal, count = \
+        animals.read_animals('animals.txt')
+    date, time, animal, count = \
+        animals.filter_animals(date, time,
+                               animal, count,
+                               'Elk')
+    assert date == ['2011-04-23', '2011-04-23']
+    assert time == ['14:12', '10:24']
+    assert animal == ['Elk', 'Elk']
+    assert count == [25, 26]
+
+
+def test_mean_animals1():
+    filename = 'animals.txt'
+    species = 'Grizzly'
+
+    assert animals.mean_animals(filename, species) == 36
+
+
+def test_mean_animals2():
+    filename = 'animals.txt'
+    species = 'Elk'
+
+    assert animals.mean_animals(filename, species) == 25.5