From: Tracy K. Teal Date: Tue, 14 May 2013 14:45:01 +0000 (-0400) Subject: Added notebooks for testing and Matt's Python files with the functions X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=refs%2Fheads%2Ftest-davis-2013-05-ucdavis;p=swc-testing-nose.git Added notebooks for testing and Matt's Python files with the functions --- diff --git a/python/sw_engineering/animal_notebook_instructor.ipynb b/python/sw_engineering/animal_notebook_instructor.ipynb new file mode 100644 index 0000000..586a5d7 --- /dev/null +++ b/python/sw_engineering/animal_notebook_instructor.ipynb @@ -0,0 +1,439 @@ +{ + "metadata": { + "name": "animal_notebook" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Software Engineering Unit at UC Davis\n", + "\n", + "Goal:\n", + " \n", + "Write a utility that returns the mean number of a particular animal seen per sighting of that animal.\n", + "\n", + " def mean_animal(filename, species)\n", + " ...\n", + " return mean_animal_per_sighting" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ls" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Look at the animals.txt file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cat animals.txt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that reads in a file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read_file(ifile):\n", + " open_file = open(ifile, 'r')\n", + " \n", + " time = []\n", + " date = []\n", + " animal = []\n", + " count = []\n", + " \n", + " for iline in open_file:\n", + " s = iline.split()\n", + " date.append(s[0])\n", + " time.append(s[1])\n", + " animal.append(s[2])\n", + " count.append(int(s[3]))\n", + " \n", + " open_file.close()\n", + " \n", + " return date, time, animal, count" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Read in the file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "read_file('animals.txt')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a test function for read_animals\n", + "\n", + "First use a print statement\n", + "\n", + "Then use an assert statement" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def test_read_animals():\n", + " date, time, animal, count = read_file('animals.txt')\n", + " ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']\n", + " ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46']\n", + " ref_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']\n", + " ref_count = [36, 25, 26, 31, 20]\n", + " \n", + " if date != ref_date:\n", + " print 'Date is not correct!'" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the test script" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "test_read_animals()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Load the testing tool nose" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load_ext ipython_nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run all the test scripts using nose. Anything with test_ at the beginning is detected as a test." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that calculates the mean of a list" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def test_mean1():\n", + " m = calc_mean([1, 2, 3])\n", + " assert m == 2\n", + " \n", + "def test_mean2():\n", + " m = calc_mean([1])\n", + " assert m == 1\n", + "\n", + "def test_mean3():\n", + " m = calc_mean([3.4, 3.5, 3.6])\n", + " assert m == 3.5\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Use nose.tools for testing" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import nose.tools as nt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "@nt.raises(ValueError)\n", + "def test_mean4():\n", + " m = calc_mean([])" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that only returns the data for a given species\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# This is not a complete function. It's just to get you started\n", + "def filter_animals(species, date, time, animal, count):\n", + " \"\"\"\n", + " Given a particular species, filter out the data for just that species.\n", + "\n", + " Returns four lists: date, time, animal, count.\n", + " \"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Remember what's in the list" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cat animals.txt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function to test the filter function" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Fill in the function\n", + "def test_filter_animals1():\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the tests!" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Tests look good. Run the functions." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "d, t, a, c = read_file('animals.txt')\n", + "filter_animals('Grizzly', d, t, a, c)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write the final function that calculates the mean for a particular species." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Fill in the function\n", + "def mean_animals(filename, species):" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Test the mean_animals function " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the tests" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the function" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean_animals('animals.txt', 'Elk')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/python/sw_engineering/animal_notebook_student.ipynb b/python/sw_engineering/animal_notebook_student.ipynb new file mode 100644 index 0000000..586a5d7 --- /dev/null +++ b/python/sw_engineering/animal_notebook_student.ipynb @@ -0,0 +1,439 @@ +{ + "metadata": { + "name": "animal_notebook" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Software Engineering Unit at UC Davis\n", + "\n", + "Goal:\n", + " \n", + "Write a utility that returns the mean number of a particular animal seen per sighting of that animal.\n", + "\n", + " def mean_animal(filename, species)\n", + " ...\n", + " return mean_animal_per_sighting" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "ls" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Look at the animals.txt file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cat animals.txt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that reads in a file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def read_file(ifile):\n", + " open_file = open(ifile, 'r')\n", + " \n", + " time = []\n", + " date = []\n", + " animal = []\n", + " count = []\n", + " \n", + " for iline in open_file:\n", + " s = iline.split()\n", + " date.append(s[0])\n", + " time.append(s[1])\n", + " animal.append(s[2])\n", + " count.append(int(s[3]))\n", + " \n", + " open_file.close()\n", + " \n", + " return date, time, animal, count" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Read in the file" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "read_file('animals.txt')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a test function for read_animals\n", + "\n", + "First use a print statement\n", + "\n", + "Then use an assert statement" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def test_read_animals():\n", + " date, time, animal, count = read_file('animals.txt')\n", + " ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']\n", + " ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46']\n", + " ref_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']\n", + " ref_count = [36, 25, 26, 31, 20]\n", + " \n", + " if date != ref_date:\n", + " print 'Date is not correct!'" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the test script" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "test_read_animals()" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Load the testing tool nose" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load_ext ipython_nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run all the test scripts using nose. Anything with test_ at the beginning is detected as a test." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that calculates the mean of a list" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def test_mean1():\n", + " m = calc_mean([1, 2, 3])\n", + " assert m == 2\n", + " \n", + "def test_mean2():\n", + " m = calc_mean([1])\n", + " assert m == 1\n", + "\n", + "def test_mean3():\n", + " m = calc_mean([3.4, 3.5, 3.6])\n", + " assert m == 3.5\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Use nose.tools for testing" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import nose.tools as nt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "@nt.raises(ValueError)\n", + "def test_mean4():\n", + " m = calc_mean([])" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function that only returns the data for a given species\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# This is not a complete function. It's just to get you started\n", + "def filter_animals(species, date, time, animal, count):\n", + " \"\"\"\n", + " Given a particular species, filter out the data for just that species.\n", + "\n", + " Returns four lists: date, time, animal, count.\n", + " \"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Remember what's in the list" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "cat animals.txt" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write a function to test the filter function" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Fill in the function\n", + "def test_filter_animals1():\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the tests!" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Tests look good. Run the functions." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "d, t, a, c = read_file('animals.txt')\n", + "filter_animals('Grizzly', d, t, a, c)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Write the final function that calculates the mean for a particular species." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "# Fill in the function\n", + "def mean_animals(filename, species):" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Test the mean_animals function " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the tests" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%nose" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Run the function" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "mean_animals('animals.txt', 'Elk')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/python/sw_engineering/mean_sighted.py b/python/sw_engineering/mean_sighted.py new file mode 100755 index 0000000..51aea49 --- /dev/null +++ b/python/sw_engineering/mean_sighted.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +# The line above tells the shell to run this script with Python. +import sys + +from meananimals import mean_animals + +# sys.argv contains the arguments the user entered at the command line when +# calling this script. See more at http://docs.python.org/2/library/sys.html. +# Another great way of putting together command line interfaces for scripts +# is the argparse module: http://docs.python.org/2/library/argparse.html + +# Try running this script by typing "./mean_sighted.py big_animals.txt Elk" +# at the command line. + +if len(sys.argv) != 3: + print 'Usage: mean_sighted.py ' +else: + filename = sys.argv[1] + species = sys.argv[2] + print mean_animals(filename, species) diff --git a/python/sw_engineering/meananimals.py b/python/sw_engineering/meananimals.py new file mode 100644 index 0000000..22bce35 --- /dev/null +++ b/python/sw_engineering/meananimals.py @@ -0,0 +1,55 @@ +def read_file(ifile): + open_file = open(ifile, 'r') + + time = [] + date = [] + animal = [] + count = [] + + for iline in open_file: + s = iline.split() + date.append(s[0]) + time.append(s[1]) + animal.append(s[2]) + count.append(int(s[3])) + + open_file.close() + + return date, time, animal, count + +def calc_mean(ilist): + ''' + returns the mean of input list + ''' + if len(ilist) == 0: + raise ValueError('Input is empty.') + + total = 0.0 + for num in ilist: + total = total + num + return total/float(len(ilist)) + +def filter_animals(species, date, time, animal, count): + """ + Given a particular species, filter out the data for just that species. + + Returns four lists: date, time, animal, count. + """ + fdate = [] + ftime = [] + fanimal = [] + fcount = [] + + for d, t, a, c in zip(date, time, animal, count): + if a == species: + fdate.append(d) + ftime.append(t) + fanimal.append(a) + fcount.append(c) + + return fdate, ftime, fanimal, fcount + +def mean_animals(filename, species): + d, t, a, c = read_file(filename) + d, t, a, c = filter_animals(species, d, t, a, c) + return calc_mean(c) \ No newline at end of file diff --git a/python/sw_engineering/test_meananimals.py b/python/sw_engineering/test_meananimals.py new file mode 100644 index 0000000..7e6fac8 --- /dev/null +++ b/python/sw_engineering/test_meananimals.py @@ -0,0 +1,47 @@ +import nose.tools as nt +from meananimals import read_file, calc_mean, filter_animals, mean_animals + +def test_read_animals(): + date, time, animal, count = read_file('animals.txt') + 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_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox'] + ref_count = [36, 25, 26, 31, 20] + + assert date == ref_date, 'Dates do not match!' + assert time == ref_time, 'Times do not match!' + assert animal == ref_animal, 'Animals do not match!' + assert count == ref_count, 'Counts do not match!' + +def test_mean1(): + m = calc_mean([1, 2, 3]) + assert m == 2 + +def test_mean2(): + m = calc_mean([1]) + assert m == 1 + +def test_mean3(): + m = calc_mean([3.4, 3.5, 3.6]) + assert m == 3.5 + +@nt.raises(ValueError) +def test_mean4(): + m = calc_mean([]) + +def test_filter_animals1(): + date, time, animal, count = read_file('animals.txt') + fdate, ftime, fanimal, fcount = filter_animals('Elk', date, time, animal, count) + + assert fdate == ['2011-04-23', '2011-04-23'] + assert ftime == ['14:12', '10:24'] + assert fanimal == ['Elk', 'Elk'] + assert fcount == [25, 26] + +def test_mean_animals1(): + m = mean_animals('animals.txt', 'Elk') + assert m == 25.5 + +def test_mean_animals2(): + m = mean_animals('animals.txt', 'Grizzly') + assert m == 36 \ No newline at end of file