Section 9.11. Exercises


9.11. Exercises

9-1.

File Filtering. Display all lines of a file, except those that start with a pound sign ( # ), the comment character for Python, Perl, Tcl, and most other scripting languages.

Extra credit: Also strip out comments that begin after the first character.

9-2.

File Access. Prompt for a number N and file F, and display the first N lines of F.

9-3.

File Information. Prompt for a filename and display the number of lines in that text file.

9-4.

File Access. Write a "pager" program. Your solution should prompt for a filename, and display the text file 25 lines at a time, pausing each time to ask the user to "press a key to continue."

9-5.

Test Scores. Update your solution to the test scores problems (Exercises 5-3 and 6-4) by allowing a set of test scores be loaded from a file. We leave the file format to your discretion.

9-6.

File Comparison. Write a program to compare two text files. If they are different, give the line and column numbers in the files where the first difference occurs.

9-7.

Parsing Files. Win32 users: Create a program that parses a Windows .ini file. POSIX users: Create a program that parses the /etc/services file. All other platforms: Create a program that parses a system file with some kind of structure to it.

9-8.

Module Introspection. Extract module attribute information. Prompt the user for a module name (or accept it from the command line). Then, using dir() and other built-in functions, extract all its attributes, and display their names, types, and values.

9-9.

"PythonDoc." Go to the directory where your Python standard library modules are located. Examine each .py file and determine whether a __doc__ string is available for that module. If so, format it properly and catalog it. When your program has completed, it should present a nice list of those modules that have documentation strings and what they are. There should be a trailing list showing which modules do not have documentation strings (the shame list). Extra credit: Extract documentation for all classes and functions within the standard library modules.

9-10.

Home Finances. Create a home finance manager. Your solution should be able to manage savings, checking, money market, certificate of deposit (CD), and similar accounts. Provide a menu-based interface to each account as well as operations such as deposits, withdrawals, debits, and credits. An option should be given to a user to remove transactions as well. The data should be stored to file when the user quits the application (but randomly during execution for backup purposes).

9-11.

Web site Addresses.

  1. Write a URL bookmark manager. Create a text-driven menu-based application that allows the user to add, update, or delete entries. Entries include a site name, Web site URL address, and perhaps a one-line description (optional). Allow search functionality so that a search "word" looks through both names and URLs for possible matches. Store the data to a disk file when the user quits the application, and load up the data when the user restarts.

  2. (b) Upgrade your solution to part (a) by providing output of the bookmarks to a legible and syntactically correct HTML file (.htm or .html) so that users can then point their browsers to this output file and be presented with a list of their bookmarks. Another feature to implement is allowing the creation of "folders" to allow grouping of related bookmarks. Extra credit: Read the literature on regular expressions and the Python re module. Add regular expression validation of URLs that users enter into their databases.

9-12.

Users and Passwords.

Do Exercise 7-5, which keeps track of usernames and passwords. Update your code to support a "last login time" (7-5a). See the documentation for the time module to obtain timestamps for when users "log in" to the system.

Also, create the concept of an "administrative" user that can dump a list of all the users, their passwords (you can add encryption on top of the passwords if you wish [7-5c]), and their last login times (7-5b).

  1. The data should be stored to disk, one line at a time, with fields delimited by colons ( : ), e.g., "joe:boohoo:953176591.145", for each user. The number of lines in the file will be the number of users that are part of your system.

  2. Further update your example such that instead of writing out one line at a time, you pickle the entire data object and write that out instead. Read the documentation on the pickle module to find out how to flatten or serialize your object, as well as how to perform I/O using picked objects. With the addition of this new code, your solution should take up fewer lines than your solution in part (a).

  3. Replace your login database and explicit use of pickle by converting your code to use shelve files. Your resulting source file should actually take up fewer lines than your solution to part (b) because some of the maintenance work is gone.

9-13.

Command-Line Arguments.

  1. What are they, and why might they be useful?

  2. Write code to display the command-line arguments which were entered.

9-14.

Logging Results. Convert your calculator program

(Exercise 5-6) to take input from the command line, i.e.,

$ calc.py 1 + 2


Output the result only. Also, write each expression and result to a disk file. Issuing a command of...

$ calc.py print


... will cause the entire contents of the "register tape" to be dumped to the screen and file reset/truncated. Here is an example session:

$ calc.py 1 + 2 3 $ calc.py 3 ^ 3 27 $ calc.py print 1 + 2 3 3 ^ 3 27 $ calc.py print $


Extra credit: Also strip out comments that begin after the first character.

9-15.

Copying Files. Prompt for two filenames (or better yet, use command-line arguments). The contents of the first file should be copied to the second file.

9-16.

Text Processing. You are tired of seeing lines on your e-mail wrap because people type lines that are too long for your mail reader application. Create a program to scan a text file for all lines longer than 80 characters. For each of the offending lines, find the closest word before 80 characters and break the line there, inserting the remaining text to the next line (and pushing the previous next line down one). When you are done, there should be no lines longer than 80 characters.

9-17.

Text Processing. Create a crude and elementary text file editor. Your solution is menu-driven, with the following options:

  1. create file [prompt for filename and any number of lines of input],

  2. display file [dump its contents to the screen],

  3. edit file (prompt for line to edit and allow user to make changes),

  4. save file, and

  5. quit.

9-18.

Searching Files. Obtain a byte value (0-255) and a filename. Display the number of times that byte appears in the file.

9-19.

Generating Files. Create a sister program to the previous problem. Create a binary data file with random bytes, but one particular byte will appear in that file a set number of times. Obtain the following three values:

  1. a byte value (0-255),

  2. the number of times that byte should appear in the data file, and

  3. the total number of bytes that make up the data file.

Your job is to create that file, randomly scatter the requested byte across the file, ensure that there are no duplicates, the file contains exactly the number of occurrences that byte was requested for, and that the resulting data file is exactly the size requested.

9-20.

Compressed Files. Write a short piece of code that will compress and decompress gzipped or bzipped files. Confirm your solution works by using the command-line gzip or bzip2 programs or a GUI program like PowerArchiver, StuffIt, and/or WinZip.

9-21.

ZIP Archive Files. Create a program that can extract files from or add files to, and perhaps creating, a ZIP archive file.

9-22.

ZIP Archive Files. The unzip -l command to dump the contents of ZIP archive is boring. Create a Python script called lszip.py that gives additional information such as: the compressed file size, the compressed percentage of each file (by comparing the original and compressed file sizes), and a full time.ctime() timestamp instead of the unzip output (of just the date and HH:MM). Hint: The date_time attribute of an archived file does not contain enough information to feed to time.mktime()... it is up to you!

9-23.

TAR Archive Files. Repeat the previous problem for TAR archive files. One difference between these two types of files is that ZIP files are generally compressed, but TAR files are not and usually require the support of gzip or bzip2. Add either type of compression support. Extra credit: Support both gzip and bzip2.

9-24.

File Transfer Between Archive Files. Take your solutions from the previous two problems and write a program that moves files between ZIP (.zip) and TAR/gzip (.tgz/.tar.gz) or TAR/bzip2 (.tbz/.tar.bz2) archive files. The files may preexist; create them if necessary.

9-25.

Universal Extractor. Create an application that will take any number of files in an archived and/or compression format, i.e., .zip, .tgz, .tar.gz, .gz, .bz2, .tar.bz2, .tbz, and a target directory. The program will uncompress the standalone files to the target while all archived files will be extracted into subdirectories named the same as the archive file without the file extension. For example, if the target directory was incoming, and the input files were header.txt.gz and data.tgz, header.txt will be extracted to incoming while the files in data.tgz will be pulled out into incoming/data.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net