From: Matt Davis Date: Mon, 18 Jun 2012 01:37:26 +0000 (-0400) Subject: Copying animal.txt files to software engineering directory for use there. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=dd8cf3059215fba494cacfb089ee519c2cfffd46;p=swc-testing-nose.git Copying animal.txt files to software engineering directory for use there. Expanded lesson plan in dev_notes.rst and added exercises to README.rst W. Trevor King: I reformatted the commit message from the original 3a1b378 so it fits better into --oneline logs. I also turned this commit into a merge so we can trace the copy, using: $ echo 081da9ef5959c5abdb8fefbdc8efc701d6a957c3 \ > d3bcf0219b92c2cfdd19239d2bf75de4ff26e579 \ > 6bc0a0bef5d364b59a3927492f025a6c3676656d \ > > .git/info/grafts $ git filter-branch $ rm .git/info/grafts --- dd8cf3059215fba494cacfb089ee519c2cfffd46 diff --cc 4-SoftwareEngineering/README.rst index 0bcf4f2,0000000..cf4b880 mode 100644,000000..100644 --- a/4-SoftwareEngineering/README.rst +++ b/4-SoftwareEngineering/README.rst @@@ -1,14 -1,0 +1,54 @@@ +==================================== +Building a Library of Code you Trust +==================================== + +Suppose we're going to be dealing a lot with these animal count files, +and doing many different kinds of analysis with them. In the introduction +to Python lesson we wrote a function that reads these files but it's stuck +off in an IPython notebook. We could copy and paste it into a new notebook +every time we want to use it but that gets tedious and makes it difficult to +add features to the function. The ideal solution would be to keep the +function in one spot and use it over and over again from many different places. +Python modules to the rescue! + ++Today we're going to move beyond the IPython notebook. Most Python code is ++stored in `.py` files and then used in other `.py` files where it has been ++pulled in using an `import` statement. + ++========= ++Exercises ++========= ++ ++Exercise 1 ++---------- ++ ++Make a new text file called `animals.py`. Copy the file reading ++function from yesterday's IPython notebook into the file and modify it so ++that it returns the columns of the file as lists (instead of printing ++certain lines). ++ ++Exercise 2 ++---------- ++ ++We're going to make a function to calculate the mean of all the ++values in a list, but we're going to write the tests for it first. ++Make a new text file called `test_animals.py`. Make a function called ++`test_mean` that runs your theoretical mean function through several tests. ++ ++Exercise 3 ++---------- ++ ++Write the mean function in `animals.py` and verify that it passes ++your tests. ++ ++Exercise 4 ++---------- ++ ++Write tests for a function that will take a file name and ++animal name as arguments, and return the average number of animals per sighting. ++ ++Exercise 5 ++---------- ++ ++Write a function that takes a file name and animal name and returns ++the average number of animals per sighting. Make sure it passes your tests. diff --cc 4-SoftwareEngineering/animals.txt index 0000000,1d8e153..1d8e153 mode 000000,100644..100644 --- a/4-SoftwareEngineering/animals.txt +++ b/4-SoftwareEngineering/animals.txt diff --cc 4-SoftwareEngineering/big_animals.txt index 0000000,c9524bb..c9524bb mode 000000,100644..100644 --- a/4-SoftwareEngineering/big_animals.txt +++ b/4-SoftwareEngineering/big_animals.txt diff --cc 4-SoftwareEngineering/dev_notes.rst index dc61910,0000000..8609619 mode 100644,000000..100644 --- a/4-SoftwareEngineering/dev_notes.rst +++ b/4-SoftwareEngineering/dev_notes.rst @@@ -1,32 -1,0 +1,78 @@@ +Dev Notes +========= + +Things we want to demonstrate: + +- write lots of small tools (functions) +- document your code +- test your code, including TDD +- debugging + + - build in a crash somewhere and explain tracebacks + - do an import pdb; pdb.set_trace() + +Create a few utilities to work with the `*animal.txt` files from +introductory Python. + +Put all the functionality in one file and make small scripts that import +from that file, parse command line arguments, and do stuff. + +- average number of an animal seen per sighting +- total number of an animal seen over the season +- all the sightings on a given day + +Students may want an IPython notebook open as a scratch pad. + +Lesson Plan +----------- + - 1. Demonstrate putting code in a `.py` file and running it. - 2. Demonstrate importing a module (maybe `math` or `glob`) ++Going to make a command line script that will print the average group ++size in a given file for a given animal. + - Exercise: ++0. Discuss libraries and re-using code. ++1. Demonstrate importing a module (maybe `math` or `glob`) ++ ++Exercise: Make a new text file called `animals.py`. Copy the file reading ++function from yesterday's IPython notebook into the file and modify it so ++that it returns the columns of the file as lists (instead of printing ++certain lines). ++ ++2. Go over the exercise and talk about documentation as you do. ++3. How do we know the function works correctly? Try importing and running it ++ on a small file. ++4. But what if we want to make changes and make sure it still works afterward, ++ or we want to make sure it isn't broken when we add new stuff later? ++ Explain unit tests and show how to write one and run with nosetests. ++5. Explain test driven development. ++ ++Exercise: We're going to make a function to calculate the mean of all the ++values in a list, but we're going to write the tests for it first. ++Make a new text file called `test_animals.py`. Make a function called ++`test_mean` that runs your theoretical mean function through several tests. ++ ++6. When going over this with the students, put in a test with an empty list. ++ Also put in tests with lists that contain all ints. ++ ++Exercise: Write the mean function in `animals.py` and verify that it passes ++your tests. ++ ++7. When going over this with the students do not put in a test for an empty ++ list. The error when running the tests will give a chance to teach ++ tracebacks. Also do not put in any coercion to float so some means will ++ be wrong due to integer truncation. More debugging, `--pdb-failure`. ++8. The last piece we'll need is a function that takes the output of the file ++ reader function and returns only the data relevant to a given animal. ++ Write this function as a live exercise with the students participating, ++ though they can do it on their own if they want. ++ ++Exercise: Write tests for a function that will take a file name and ++animal name as arguments, and return the average number of animals per sighting. ++ ++Exercise: Write a function that takes a file name and animal name and returns ++the average number of animals per sighting. Make sure it passes your tests. ++ ++9. After going over exercises, conclude by showing how to make a command line ++ script that takes a file name and animal name as arguments and prints ++ the average number of animals per sighting. ++10. If time permits, could demonstrate `pdb.set_trace()` somewhere in the ++ script execution. diff --cc 4-SoftwareEngineering/dingwall_animals.txt index 0000000,b2f4cd1..b2f4cd1 mode 000000,100644..100644 --- a/4-SoftwareEngineering/dingwall_animals.txt +++ b/4-SoftwareEngineering/dingwall_animals.txt diff --cc 4-SoftwareEngineering/fergus_animals.txt index 0000000,e40c146..e40c146 mode 000000,100644..100644 --- a/4-SoftwareEngineering/fergus_animals.txt +++ b/4-SoftwareEngineering/fergus_animals.txt diff --cc 4-SoftwareEngineering/macguffin_animals.txt index 0000000,5e03d2d..5e03d2d mode 000000,100644..100644 --- a/4-SoftwareEngineering/macguffin_animals.txt +++ b/4-SoftwareEngineering/macguffin_animals.txt diff --cc 4-SoftwareEngineering/merida_animals.txt index 0000000,fa9a9f0..fa9a9f0 mode 000000,100644..100644 --- a/4-SoftwareEngineering/merida_animals.txt +++ b/4-SoftwareEngineering/merida_animals.txt