CSC 111: Introduction to Computer Science

Lab 10: File I/O and the getURLclass

Thursday, April 14, 2011

Part 1: Input Data via a File

The first part of lab is for practicing reading input data in from a file. You have been writing our output to a file, and so to balance things out, you should also be comfortable reading data in from a file. One thing to keep in mind is that the Python .read(), .readlines() and .readline() functions will read the input lines in as strings - including the newline character! If you want your data to be recognized as numerical data, then you must use string processing and type casting to get the data into the form you want.

Back to 'simpleMath.py'

In class on April 5 we used a program simpleMath.py. This was used for introducing functions. The lab task today is to update this Python program so that it will now read in the input data from a file, and process it using the simpleMath functions.

  1. Start by creating a text file that contains a list of numbers, each on a separate line.
  2. Your new simpleMath.py program will use this file as input.
  3. Write a new function to
This means that instead of having the existing first line in the main() function, in which the variable numList is assigned a list of numbers, the new first line of main() will assign the value returned by your new function to the variable numList. The remainder of the original program should be unchanged. Be sure you are getting the correct answers (for the sum and average) before you consider this part to be a success.

Part 2: Using Tables in HTML

Tables in HTML can be finicky - be very methodical as you use them! Look at the html code snippet below. Note that you have opening and closing "table" tags. You then open (and also close) table "rows" with the "tr" tag. You enter individual cell data, with "td" (table data) tags.
<p>
<table border="1">
<tr>
    <td>row 1, cell 1</td>
    <td>row 1, cell 2</td>
</tr>
<tr>
    <td>row 2, cell 1</td>
    <td>row 2, cell 2</td>
</tr>
</table>
This will be displayed as:

row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

You will be using tables in the next part of the lab and the homework to put some weather and then FriendInfo data onto your webpage - you might want to just play around with them now to make sure you are comfortable with how they work. For this playing around phase, it is probably easiest to simply edit your .html file directly. Create a table in your .html file, put some data in, and make sure you are comfortable adding rows and adding columns to your table.

Part 3: Using the getURLinfo Class

To practice using the getURLinfo class, this part of the lab will create getURLinfo objects, using the weather URL from HW 9.

You can either build on your existing Python program that creates your .html file, OR you can start fresh and have a new Python program writing a new .html file. If you do start fresh, be sure to copy and paste useful functions from the previous file, such as the functions for opening and for closing the .html file (this is one of the beautiful uses of functions - using them over and over again for new applications).

3a: Creating and using an object

As with homework 9, you want to retrieve the place (town name), current conditions (such as "bright and sunny"), and the temperature from the weather URL, but now you will do it using the getURLinfo class. To use the getURLinfo class, you will:
  1. (remember to import the class definition)
  2. Create an object of type getURLinfo
  3. Set the 'url' instance variable to the baseURL (the one with the "%s" ): "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=%s"
  4. Set the markers for your piece of text to retrieve (town name, current conditions, or temperature)
  5. Retrieve and store the text in a variable
  6. Repeat from step 4 to get the other two pieces of information
Put all of this code inside its own function, and have this function return three strings, the town name, current conditions and temperature. These return values will be used next.

3b: Creating a table to display your weather info

As this weather information is retrieved, and once you have all three pieces of information for a given zipcode, place it into an HTML table to be displayed on your web page. Your table should have: This part of your program should be its own function, and this function should call the function you wrote for step 3a. This should be suggesting to you that you will have a loop in the function you write for this step (3b). This loop will execute as many times as there are zipcodes. Perhaps this function will have a list of zipcodes as a parameter, passed to it by whichever function calls it.

Back to the loop... In each iteration through the loop you will call the function from step (3a) and write the returned values into your html table.

Before entering the loop (but still inside this function for step (3b)) it would make sense to write the opening html table tag, and write the first table row for the header information. Then execute the loop, adding a new row to your table for each iteration i.e., for each zipcode). Finally, after you exit the loop, but before you leave the function, write the closing html table tag to your .html file.

3c: Accepting input data from an input file of zipcodes

Finally, create an input file, perhaps called zipList.txt, that has a list of zipcodes. Read this file into your program, and use the values in this file to create your table of weather around the area or country.

For example, you could use:

01001
20008
30567
60013
90010
You are now done! You will use many of these same concepts and techniques for the homework, so be sure you are comfortable with them now.