1. Modify the print_unicode.py program so that the user can enter several separate words on the command line, and print rows only where the Unicode character name contains all the words the user has specified. This means that we can type commands like this:

print_unicode_ans.py greek symbol

One way of doing this is to replace the word variable (which held 0, None, or a string), with a words list. Don't forget to update the usage information as well as the code. The changes involve adding less than ten lines of code, and changing less than ten more. A solution is provided in file print_unicode_ans.py. (Windows and cross-platform users should modify print_unicode_uni.py; a solution is provided in print_unicode_uni_ans.py.)

2. Modify quadratic.py so that 0.0 factors are not output, and so that negative factors are output as - n rather than as + -n. This involves replacing the last five lines with about fifteen lines. A solution is provided in quadrat-ic_ans.py. (Windows and cross-platform users should modify quadrat-ic_uni.py; a solution is provided in quadratic_uni_ans.py.)

3. Delete the escape_html() function from csv2html.py, and use the xml.sax. saxutils.escape() function from the xml.sax.saxutils module instead. This is easy, requiring one new line (the import), five deleted lines (the unwanted function), and one changed line (to use xml.sax.saxutils.escape() instead of escape_html()). A solution is provided in csv2html1_ans.py.

4. Modify csv2html.py again, this time adding a new function called pro-cess_options(). This function should be called from main() and should return a tuple of two values: maxwidth (an int) and format (a str). When process_options() is called it should set a default maxwidth of 100, and a default format of ".0f"—this will be used as the format specifier when out-putting numbers.

If the user has typed "-h" or "--help" on the command line, a usage message should be output and (None, None) returned. (In this case main() should do nothing.) Otherwise, the function should read any command-line arguments that are given and perform the appropriate assignments. For example, setting maxwidth if "maxwidth=rc" is given, and similarly setting format if "format=s" is given. Here is a run showing the usage output:

csv2html2_ans.py -h usage:

csv2html.py [maxwidth=int] [format=str] < infile.csv > outfile.html maxwidth is an optional integer; if specified, it sets the maximum number of characters that can be output for string fields, otherwise a default of 100 characters is used.

format is the format to use for numbers; if not specified it defaults to ".0f".

And here is a command line with both options set:

csv2html2_ans.py maxwidth=20 format=0.2f < mydata.csv > mydata.html

Don't forget to modify print_line() to make use of the format for out-putting numbers—you'll need to pass in an extra argument, add one line, and modify another line. And this will slightly affect main() too. The pro-cess_options() function should be about twenty-five lines (including about nine for the usage message). This exercise may prove challenging for inexperienced programmers.

Two files of test data are provided: data/co2-sample.csv and data/co2-from-fossilfuels.csv. A solution is provided in csv2html2_ans.py. In Chapter 5 we will see how to use Python's optparse module to simplify command-line processing.

This page intentionally left blank

• Sequence Types

• Mapping Types

• Iterating and Copying Collections

0 0

Post a comment