PyMOTW: operator

Functional programming using iterators occasionally requires you to
create small functions for simple expressions. Sometimes these can be
expressed as lambda functions. But for some operations, you don’t
need to define your own function at all. The operator module defines
functions that correspond to built-in operations for arithmetic, and
comparison as well as sequence and dictionary operations.

Read more at operator

One year of “The Python Module of the Week”

It’s a bit passé to recognize blogging anniversaries, but as it’s my
first I’m going to do a little retrospecting anyway. :-)

I just realized this afternoon that I had missed celebrating the first
anniversary of PyMOTW by a few weeks. I started the series as an excuse
to force myself to write something once a week. At the time, it seemed
like a somewhat lame idea and I wasn’t sure I would keep it up. There
are any number of reference guides for the standard library out there.
Sitting down to read through one isn’t that exciting, though, so I
thought writing example code with all of the modules would be a way to
force myself to actually study the modules I didn’t use on a regular

The first real post from 25 March 2007 covered the fileinput
module. It wasn’t until several posts into the series that I started
collecting and releasing the code through PyPI, so the version number
for the source package is only up to 1.48 even though I’ve done more
than 52 weeks worth of modules. (The os module took 4 weeks, so I
haven’t done as many modules as weeks of posts.)

My Writing Process

My process for creating the posts has changed substantially over the
last year. The first few posts were posted through the web form on They consisted of a lot of hand-edited HTML combined with
output from the web version of pygments (used to highlight the syntax
in the code examples). I also used to write the prose for each post
first, and the code samples later.

Now, I have the entire process reversed. I work through all of the
code examples before writing any prose. The code comes more quickly, and
I can revise and refactor it so the examples work together without
having to go back and edit the rest of the text. Once I have the code
finished, I use a combination of shortcuts I’ve built for TextMate
and MarsEdit to assemble the post and write the prose portions. It
takes me a lot less time to create a single post now that I’ve refined
the workflow. The post on the operator module from today, for example,
only took a couple of hours (with interruptions). It is a little skimpy
on prose, though.

Future Plans

Since the beginning, I’ve had a fair number of comments (online and
off) from people who tell me that the posts have been personally useful
to them. I appreciate that sort of feedback, and it motivates me to keep
going. I’m running out of the “simple” modules, and as I’ve also started
working on Python Magazine over the past year, I don’t actually have the
same amount of free time any more. Having a bit of extra motivation will
spur me to pick up some of the bigger modules like email and

At the rate I’m going, I’m not going to finish the whole library
before Python 3.0 comes out, and the current plans call for some
modules to be removed, deprecated APIs to be dropped, and other sorts of
changes. The rules say some modules can even be renamed. When that
settles down and there is an actual release, I’ll probably stop writing
about 2.x and pick up with 3.0. I haven’t decided yet, though.

Python Module of the Week Home

Which module should I write about next?

I had some great feedback about the PyMOTW series from several of you
at PyCon this weekend. Unfortunately, when I put you on the spot, no one
had suggestions for what to write about next. I’ve been going through
the library more or less randomly, and in the absence of a better idea I
can continue with that plan. On the other hand, if there’s a topic that
you would really like more details on, let me know and I’ll try to bump
it up in the queue. My regular work schedule is pretty slammed right
now, so smaller (or simpler) modules will be given higher priority than
anything like re or socket, both of which have entire books
written about them.

So, speak up and let me know what to write about next week.

PyMOTW: datetime

The datetime module includes functions and classes for doing date
parsing, formatting, and arithmetic. Time values are represented with
the time class. Times have attributes for hour, minute, second,
and microsecond. They also, optionally, include time zone
information. The arguments to initialize a time instance are
optional, but the default of 0 is unlikely to be what you want.

Read more at datetime

PyMOTW: time

The time module exposes C library functions for manipulating dates
and times. Since it is tied to the underlying C implementation, some
details (such as the start of the epoch and maximum date value
supported) are platform-specific.

Read more at time

PyMOTW: imp

The imp module includes functions that expose part of the
underlying implementation of Python’s import mechanism for loading code
in packages and modules. It is one access point to importing modules
dynamically, and useful in some cases where you don’t know the name of
the module you need to import when you write your code (e.g., for
plugins or extensions to an application).

Read more at imp

PyMOTW: pkgutil

The pkgutil module provides a single function, extend_path(),
that is used to modify the search path for modules in a given package to
include other directories in sys.path. This is very useful for
overriding installed versions of packages with development versions, or
for combining OS-specific and shared modules into a single package

Read more at pkgutil