--- /dev/null
+{
+ "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
--- /dev/null
+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
--- /dev/null
+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