PyMOTW: linecache

The linecache module is used extensively throughout the Python
standard library when dealing with Python source files. The
implementation of the cache simply holds the contents of files, parsed
into separate lines, in a dictionary in memory. The API returns the
requested line(s) by indexing into a list. The time savings is from
(repeatedly) reading the file and parsing lines to find the one
desired. This is especially useful when looking for multiple lines
from the same file, such as when producing a traceback for an error

Read more at linecache

PyMOTW: StringIO and cStringIO

The StringIO class provides a convenient means of working with text
in-memory using the file API (read, write. etc.). There are 2 separate
implementations. The cStringIO module is written in C for speed, while
the StringIO module is written in Python for portability. Using
cStringIO to build large strings can offer performance savings over some
other string conctatenation techniques.

Read more at StringIO

PyMOTW: Queue

The Queue module provides a FIFO implementation suitable for
multi-threaded programming. It can be used to pass messages or other
data between producer and consumer threads safely. Locking is handled
for the caller, so it is simple to have as many threads as you want
working with the same Queue instance. A Queue’s size (number of
elements) may be restricted to throttle memory usage or processing.

Read more at Queue

PyMOTW: ConfigParser

The ConfigParser module is very useful for creating user-editable
configuration files for your applications. The configuration files are
broken up into sections, and each section can contain name-value pairs
for configuration data. Value interpolation using Python formatting
strings is also supported, to build values which depend on one another
(this is especially handy for paths or URLs).

Read more at ConfigParser

Testing pygments

This is a test post to see experiment with the code hightlighting
output from (as recommended by a couple of commenters on
my previous post). Pygments produces HTML with CSS-based styling, so I
have added a bunch of new styles to my blogger template. And I am
including as a sample the same Python code posted earlier with the
alternative syntax highlighting tool.

def main(self, *m3ufilenames):
    for line in fileinput.input(m3ufilenames):
        mp3filename = line.strip()
        if not mp3filename or mp3filename.startswith('#'):
    return 0

So, let me know what you think of the 2 methods, and which looks

PyMOTW: Call for input

Tomorrow’s post will cover the ConfigParser module. Beyond that, I
have a few more weeks planned out, and am looking for suggestions for
which modules to cover next.

If you were stranded on a desert island, which standard library module
would you want, and why?

PyMOTW: Python Module of the Week

I am starting a new series of posts today that I am calling “Python
Module of the Week” (PyMOTW)
. I have two goals for this:

  1. to work my way though the standard library and learn something about
    each module
  2. to get into the habit of posting to the blog more regularly

I will cheat a little, and start out with some modules that I already
know something about. Hopefully that will give me enough of a head-start
that I can keep up a fairly regular flow.

image0Subscribe to PyMOTW in your feed reader