From b020aedca2e6cce1c029dbb236f6aac2e482d60c Mon Sep 17 00:00:00 2001 From: Jon Speicher Date: Fri, 26 Jul 2013 10:56:56 -0400 Subject: [PATCH] Add creation of sightings file from Intro exercise --- .../sw_engineering/SoftwareEngineering.ipynb | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/python/sw_engineering/SoftwareEngineering.ipynb b/python/sw_engineering/SoftwareEngineering.ipynb index c5bbb3b..4b9c52a 100644 --- a/python/sw_engineering/SoftwareEngineering.ipynb +++ b/python/sw_engineering/SoftwareEngineering.ipynb @@ -64,6 +64,70 @@ "To achieve our goal, we'll approach the problem in a fashion typical of modern software engineering." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "***\n", + "# Creating a library of code\n", + "***\n", + "\n", + "Since the best programmer is a lazy programmer, we'll start by copying some code that we've already written.\n", + "\n", + "* Using a text editor, create a file named `sightings.py` in this directory.\n", + "* Open the the file `count_animals.py` that you created in the Intro session.\n", + "* Copy the function `count_wolverines` that you created in Exercise 8 into the `sightings.py` file.\n", + "* Save `sightings.py`.\n", + "\n", + "***\n", + "**Aside: Why `sightings.py`?**\n", + "\n", + "In the intro session, we named our module `count_animals`, because it counted animal total sightings. In this exercise, we are developing a reusable module that will at the very least allow us to find the average number of sightings per animal per day. Since our goal is to develop a reusable module of code to import into a number of programs, we can't say for sure that this is the **only** thing our module will do now and forever more. Furthermore, changing the module's name later, while not impossible, runs the risk of breaking programs that we've already written to rely on it, or will at least require us to touch each of them to update the name of the imported module. In this case, then, the name `sightings` seems reasonable for our module because whether we are counting total sightings or computing average sightings, we are still dealing with \"sightings,\" which, by the way, is the primary information that our data files contain. \n", + "***" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Results\n", + "\n", + "When you are done, the file `sightings.py` should look something like this (run the cell below to create it if you need help)." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%%file sightings.py\n", + "def count_wolverines(filename):\n", + " '''Given a plain text file containing animal sighting data in the form \n", + " date time animal count\n", + " returns the total count of wolverines sighted.'''\n", + " animal_file = open(filename, 'r')\n", + " animal_file_lines = animal_file.readlines()\n", + " animal_file.close()\n", + "\n", + " total_count = 0\n", + " for line in animal_file_lines:\n", + " date, time, animal, count_string = line.split()\n", + " if animal == 'Wolverine':\n", + " total_count = total_count + int(count_string)\n", + " return total_count" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Writing sightings.py\n" + ] + } + ], + "prompt_number": 7 + }, { "cell_type": "code", "collapsed": false, -- 2.26.2