From: Matt Davis Date: Fri, 19 Oct 2012 15:38:01 +0000 (-0400) Subject: Adding examples from second day software engineering lecture. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=refs%2Fheads%2Ftest-davis-2012-10-lbl;p=swc-testing-nose.git Adding examples from second day software engineering lecture. --- diff --git a/4-SoftwareEngineering/TestStuff.ipynb b/4-SoftwareEngineering/TestStuff.ipynb new file mode 100644 index 0000000..204cd9d --- /dev/null +++ b/4-SoftwareEngineering/TestStuff.ipynb @@ -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', ' 10) & (a['secondcol'] == 12)]" + ], + "language": "python", + "outputs": [ + { + "output_type": "pyout", + "prompt_number": 36, + "text": [ + "rec.array([(11, 12, 13)], ", + " dtype=[('firstcol', '