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
report.

Read more at pymotw.com: linecache

codehosting now supports feedburner

I just posted a new version of my codehosting project for django
which supports passing the Atom feeds for release updates through
feedburner.com. There isn’t anything tying the implementation to
FeedBurner, of course, but since that’s why I wanted the feature that’s
how I am describing it.

One tricky bit was I wanted all of the existing subscribers to my
feed(s) to be redirected to the FeedBurner URL. I couldn’t just add a
redirect rule in Apache, since not all of the feeds are set up with
FeedBurner yet. So I opted for letting the django code handle the
redirection. If a project has an external_feed property that is not
null, that value is used as the URL for feeds for the project. So when
someone accesses the old URL for the codehosting release feed
(http://www.doughellmann.com/projects/feed/atom/codehosting/) they are
redirected to http://feeds.feedburner.com/DougHellmann-codehosting
instead. And FeedBurner looks at
http://www.doughellmann.com/projects/local_feed/atom/codehosting/,
which always produces the Atom content locally.

The “local_feed” URL is never included in any templates, so no web
crawlers should ever find it by themselves.

This is one of those cases where I had thought to include this feature
from the beginning, since migrating the existing readers of the feed(s)
required this hackish change. But, it looks like it is working. I would
be interested in any feedback anyone else might have on other ways I
could have handled the redirects.

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 pymotw.com: 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 pymotw.com: 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 pymotw.com: ConfigParser

Testing pygments

This is a test post to see experiment with the code hightlighting
output from pygments.org (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):
    self.startRSS()
    self.generateChannelInfo()
    for line in fileinput.input(m3ufilenames):
        mp3filename = line.strip()
        if not mp3filename or mp3filename.startswith('#'):
            continue
        self.generateItem(mp3filename)
    self.endRSS()
    return 0

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

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?

Missing the point

This is a perfect example of being too close to a problem.

Some users of the FlipStart compact PC were having trouble pressing
the tiny keys for Ctrl-Alt-Del to login or reboot Windows.
Unfortunately, the hardware vendor decided that instead of fixing the
operating system so it wasn’t necessary to press Ctrl-Alt-Del on the
tiny keyboard, they would just add a special Ctrl-Alt-Del button.

Go figure.