This tutorial discusses various techniques for using generator functions and generator expressions
in the context of systems programming. This topic loosely includes files, file systems, text parsing,
network programming, and programming with threads.
This download also includes all of the code samples that follow below.
Here are various code samples that are used in the course. You can
cut and paste these to your own machine to try them out. The order in
which these are listed follow the course outline. These examples are
written to run inside the "generators" directory that gets created
when you unzip the above file containing the support data.
Part 2 : Processing Data Files
nongenlog.py. Calculate the number of bytes
transferred in an Apache server log using a simple for-loop. Does not use generators.
genlog.py. Calculate the number of bytes
transferred in an Apache server log using a series of generator expressions.
makebig.py. Make a large access-log file for performance testing. This will create a file "big-access-log". For the numbers used in the
presentation, I used python makebig.py 2000.
Part 3 : Fun with Files and Directories
genfind.py. A generator function that yields
filenames matching a given filename pattern.
genopen.py. A generator function that yields
open file objects from a sequence of filenames.
gencat.py. A generator function that concatenates
a sequence of generators into a single sequence.
gengrep.py. A generator that greps a series of lines for
those that match a regex pattern.
bytesgen.py. Example that finds out how many bytes
were transferred for a specific file in a whole directory of log files.
Part 4 : Parsing and Processing Data
retuple.py. Parse a sequence of lines into a
sequence of tuples using regular expressions.
redict.py. Parse a sequence of lines into a
sequence of dictionaries with named fields.
fieldmap.py. Remap fields in a sequence of
robots.py. Find out who has been hitting robots.txt.
robotsfast.py. Find out who has been hitting robots.txt (faster version).
Part 5 : Processing Infinite Data
follow.py. Follow a log-file in real-time like tail -f in Unix. To run this program, you need to have a log-file to work with. Run the program runservers.py to start a
simulated web-server. This will write a series of log lines for you to follow.
realtime404.py. Print all 404 requests as they
happen in real-time on a log file.