Lab Exercise 3: Modular Design and Lists
The goal of this week's project is to start building more general functions that may be useful in many contexts. We're going to start using a concept called modular design where we build more complex programs on top of simpler functions.
The primary activity of this lab time is to build a set of general functions in a library and then use that library in a separate Python file. This will be the first time you call a function from another Python file that you wrote by importing one file into another. You will also do a little experimentation with lists, which are a data structure for holding sequences of values.
For the project, you'll continue to work with the Goldie buoy data, doing some more sophisticated calculations and analyses, aided by your function library.
- Set up your workspace
In your personal file space, make a folder called project3.
Open a text editor (e.g. TextWrangler). Create a new file, put your name and Project 3 at the top, then save it in your project3 folder.
- Working with lists
Lists are an important data structure in Python. A list is an ordered sequence of values. In Python the values of a list can be of any type, including other lists, and the list does not have to all be of the same type (that is not necessarily the case for all programming languages). For this assignment, we'll mostly be dealing with lists of numbers.
Put the following assignment into your Python file. It assigns to a a new list with five values.
a = [5, 3, 6, 1, 2]
In Python a list is delimited by square brackets, and the elements of the list are separated by commas. To access one element of a list, use syntax called bracket-notation. The first element of a list has index zero and is specified by the expression
Add two more lines to your file to print out the 5 and the 1 (the first element in the list and the fourth element in the list).
To add items to the end of a list, use the append method. That means that you use the name of the list and then add
.append()with the item to append inside the parentheses. For example, add the following two lines to your Python code.
a.append(7) print( a )
You should now see a 7 at the end of the list.
To change the value of an element of a list, you use an assignment. Use bracket notation to specify in which element of the list to store the new data. For example, the following changes the first element of the list to a 4 and then prints the updated list.
a = 4 print( a )
Modify the value of two other locations in the list in your Python file, then print out the list and make sure it did what you think it should have.
- Building lists
So far, all of our programs have processed data while we read it from a file. For this project, we want to store the incoming data in a list and then manipulate the data in the list. The concept is straightforward: start with an empty list and as your program reads through the file append each value to the list.
Start a new Python file called analyze.py. Put your name, date, and CS 152 at the top of the file. Then copy and paste the following algorithm. Fill in the code; each comment will correspond to one line of code.
def main(): # assign to fp the result of opening the file hurricanes.csv for reading # assign to line the first line of the data file # assign to headers the result of splitting the line using commas # print headers # assign to data an empty list # for all of the lines in the file # assign to words the result of splitting the line using commas # append to data the second item (which index?) in words cast as a float # close the data file # print data return if __name__ == "__main__": main()
Download this file and test your program. It should print the following
['Year', 'Number', 'Damage\n']
[15, 5, 6, 8, 3, 12, 7, 10, 2, 6, 4, 7, 10, 8]
- Discover command-line arguments
When we run a python program in the Terminal (or command-line), sometimes we want to pass information to the program to control its behavior. For example, it would be nice to modify our analyze.py program so that we could use it with any data file and any column. The following steps will show you how to get access to the command-line information.
- Import the sys package
Create a new file com.py. After the putting in your name, write the command to tell python to import the sys package:
- Find out what the sys package can do
Put the following line of code in the com.py file.
Save your file and run com.py file. What do you see?
Type some additional things on the command line after python3 com.py. For example, try:
python3 com.py hello world 1 2 3
What do you see?
The sys package gives you the ability to see what the user has typed on the command line. Each individual string (defined by spaces) from the command line is an entry in a list.
- Access individual strings
Add the following three lines to your com.py file.
print("Running program", sys.argv) print("I'm going to open the file", sys.argv) print("I'm going to extract column", int(sys.argv))
Run the program using the following command.
python3 com.py hurricanes.csv 1
How could you use the capability to control what file and what column of data to extract?
- Import the sys package
- Use command-line parameters to guide a program
Edit your analyze.py file so that the CSV file and column index are given on the command line.
- Add the import sys statement to the top of the file.
- Give the main function two parameters: filename, columnID.
- Replace "hurricanes.csv" with the filename parameter.
- On the line that appends the data to your list, replace the index used to access words with the columnID parameter.
- When the main function is called at the bottom of the file, use sys.argv and int(sys.argv) as arguments.
Run your program using
python3 analyze.py hurricanes.csv 1
The result should be identical to the prior case. Try changing the 1 to a 2 and see what happens.
What happens if you forget the extra commands when you run the program?
- A Library of Useful Functions
Now we're going to create a file that holds a library of useful functions. In Python, a library, or package, or module, is a file that contains functions. When you import the module into another Python file, you can use those functions. We've already done this by importing the sys package into our program. Now we're going to create our own module and then import it into other Python files.
Create a new file called stats.py. Put your name, date, and CS 152 at the top of the file.
- Write a function to compute the sum of a list of numbers
Create a new function called sum that takes one argument. You can assume that argument will be a list of numbers. This function should add together all of the values in the list and return the sum.
The algorithm is as follows. Create a variable to hold the sum and initialize it to 0.0 (explicitly make it floating point number). Then use a for loop to iterate over the list provided as the function parameter. Inside the loop, add each number to the variable holding the sum. Once the loop is complete, return the sum.
- Write a test function
To test your function, make a second function called test at the bottom of your stats.py file. The function does not require any arguments. As the first instruction in the test function, assign to a variable the list [1, 2, 3, 4]. For the second instruction assign to a second variable the result of calling sum with the list as the argument. For the third instruction, print out the variable holding the result.
Put the following at the end of your stats.py file to call the test function only when you run the stats.py file directly. The test function will not run when stats.py is imported into another Python file.
if __name__ == "__main__": test()
Run your program and make sure you get the value 10.0 as an output.
The first task in the project is to create some more functions for your library that compute the min, max, mean, and variance of the elements in a list.
- Write a function to compute the sum of a list of numbers
- Using a Library
The last step in the lab is to import your stats library into another file and use one of the functions. Reopen your analyze.py file. At the top, after the import sys, put the following line of code.
To call a function in your stats library, all you have to do is put stats. in front of the name of the function you want to use.
Edit your main function so it uses the stats.sum function to compute the sum of the values stored in the data list and prints it out.
If you run your analyze.py program using the same command as above in section 2, you should get a sum of 103 as the answer.
When you are done with the lab exercises, you may begin the project.