Intermediate animal files
authorJustin Kitzes <jkitzes@berkeley.edu>
Sun, 21 Oct 2012 20:54:25 +0000 (13:54 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 9 Nov 2013 17:15:32 +0000 (09:15 -0800)
4-SoftwareEngineering/animal-notebook.ipynb [new file with mode: 0644]
4-SoftwareEngineering/animals.py [new file with mode: 0644]
4-SoftwareEngineering/test_animals.py [new file with mode: 0644]

diff --git a/4-SoftwareEngineering/animal-notebook.ipynb b/4-SoftwareEngineering/animal-notebook.ipynb
new file mode 100644 (file)
index 0000000..8397235
--- /dev/null
@@ -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": [
+        "[&apos;2011-04-22&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;]"
+       ]
+      }
+     ], 
+     "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": [
+        "([&apos;2011-04-22&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;, &apos;2011-04-23&apos;],", 
+        " [&apos;21:06&apos;, &apos;14:12&apos;, &apos;10:24&apos;, &apos;20:08&apos;, &apos;18:46&apos;],", 
+        " [&apos;Grizzly&apos;, &apos;Elk&apos;, &apos;Elk&apos;, &apos;Wolverine&apos;, &apos;Muskox&apos;],", 
+        " [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/&lt;ipython-input-16-124ea80b1f4f&gt;</span> in <span class=\"ansicyan\">&lt;module&gt;</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\">----&gt; 5</span><span class=\"ansired\"> </span>square<span class=\"ansiblue\">(</span><span class=\"ansiblue\">&apos;hi&apos;</span><span class=\"ansiblue\">)</span><span class=\"ansiblue\"></span>\n", 
+        "<span class=\"ansigreen\">/Users/jkitzes/Desktop/2012-10-ucb/4-SoftwareEngineering/&lt;ipython-input-16-124ea80b1f4f&gt;</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\">----&gt; 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\">&apos;Num must be an int&apos;</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\">&apos;hi&apos;</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": "&nbsp;"
+    }
+   ]
+  }
+ ]
+}
\ 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..411ba9a
--- /dev/null
@@ -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 (file)
index 0000000..0cc9a0a
--- /dev/null
@@ -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'