PyMOTW: pickle and cPickle

The pickle module implements an algorithm for turning an arbitrary
Python object into a series of bytes (“serializing” the object). The
byte stream can then be transmitted or stored, and later reconstructed
to create a new object with the same characteristics.

The cPickle module implements the same algorithm, in C instead of
Python. It is many times faster than the Python implementation, but does
not allow the user to subclass from Pickle. If sub-classing is not
important for your use, you probably want to use cPickle.

Read more at pickle

DjangoKit help?

I spent a little time last night trying to assemble an application
using DjangoKit without much success.

I’m running Python 2.5 on a PowerBook with Mac OS 10.4. I downloaded
and installed PyObjC from source so it would compile (I thought) against
the right version of Python, then installed DjangoKit using
python install. Everything seemed to be working, and I was
able to build an application. But when I ran that app, it produced an
error about the version of the SQLite libraries being used (2 instead of
3) and missing libraries.

I gave up on Python 2.5, re-installed PyObjC and DjangoKit for 2.4 and
tried again. Same error.

Just for grins, I copied the app over to my wife’s laptop (she has a
MacBook Pro). The result was, of course, a new error about the platform.
No universal binaries? Really?

I’m sure there are options, or something, that I’m leaving out when I
build the app. This was mostly an experiment, and I was in a hurry, so I
gave up easily and just installed the django code I wanted on an
existing (Linux) web server and let her use that instead of messing with
a desktop application.

Has anyone else had more success building portable Python apps, esp.
with django, on Mac OS X?


Of course I knew better than to post in frustration when I posted this
originally. In my haste, I didn’t post sample code, the error message,
or much of the rest of the information I would have wanted if I was the
DjangoKit author trying to help someone out. Nonetheless, Tom did some
digging anyway and offered suggestions. Others did as well. Thanks! I
finally found time to follow up, and am coming closer to an answer.

Here are the full details

The application is very, very simple. The model just contains 2
classes for creating an index of a pile of Cook’s Illustrated magazine
we have laying around the house. There is no front-end, since the admin
views already provide the functionality she wanted. I thought I would be
cute and bundle it as a desktop app for Ms. PyMOTW, instead of setting
the app up on my web server. I have packaged the sample code and placed
it on my server.

I have included 2 separate files ( and I couldn’t package the source using the DjangoKit
version of setup:

$ python sdist --force-manifest
Loading 'initial_data' fixtures...
No fixtures found.
running sdist
warning: sdist: missing required meta-data: name, url
warning: sdist: manifest template '' does not exist (using default file list)
error: dist/Cook's Illustrated File name too long

I only seem to get the error if my dist directory includes the
application, too. Otherwise I get a minimal package with the name
‘UNKNOWN’. So, the tarball was packaged with a regular distutils That’s not a big problem, since it is easy to use separate

When I ran python py2app, the first time it
reported this error:

*** creating application bundle: Cook's Illustrated Index ***
error: can't copy 'media': doesn't exist or not a regular file

I eventually figured out (guessed) that even though I don’t have any
external media, I need a media directory at the same level in the
directory tree as the setup file. Creating the directory let me create
the app. Running that app gives me this traceback:

Traceback (most recent call last):
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 31, in
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 28, in _run
    execfile(path, globals(), globals())
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 9, in
    from pysqlite2 import dbapi2 as sqlite
ImportError: No module named pysqlite2

That brings the error reporting up to date, without trying any of the
suggestions in the comments, yet. As I mentioned, the code itself works
if I run django outside of the packaged application (from the command
line, etc.). So I’m confident that my own imports are valid, etc.

Based on a hint from Tom (in the comments, he suggests that I install
pysqlite2), I tried editing the file created inside the
application to import from sqlite3 instead of sqlite2. Editing the file
directly didn’t do it. Editing the copy already in my application
changed the error message to:

Traceback (most recent call last):
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 31, in
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 28, in _run
    execfile(path, globals(), globals())
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 10, in
    from sqlite3 import dbapi2 as sqlite
ImportError: No module named sqlite3

Next I tried editing the version of in
After removing the application and rebuilding, I see the same error.

So I finally broke down and installed the pysqlte2 package Tom
pointed out for me. The package seems to imply that it is for Python
2.4, and I’m running 2.5, but I installed it anyway.

The application Packaged with python 2.5 gave me “No module named
pysqlite2” when I ran it. Repackaged using “python2.4 py2app”, I got the app to run but it does not seem
to actually work. The console log shows this:

2007-06-23 15:07:28.403 Cook's Illustrated Index[14739] creating support folder /Users/dhellmann/Library/Application Support/DjangoKit/CooksIndex
2007-06-23 15:07:28.405 Cook's Illustrated Index[14739] installing default database
Starting web server on port 10557
Unhandled exception in thread started by
Traceback (most recent call last):
  File "/Users/dhellmann/Devel/personal/CooksIndex/trunk/dist/Cook's Illustrated", line 81, in startWebServer
    handler = AdminMediaHandler(WSGIHandler(), path)
TypeError: __init__() takes exactly 2 arguments (3 given)

So, I am a lot closer but not quite where I would like to be. I don’t
really care whether I package under 2.4 or 2.5, so long as the result
runs on my wife’s laptop, which doesn’t have any development packages

PyAtl Presentations

Last night’s Atlanta Python Meetup included several interesting
presentations. Living so far outside of Atlanta, it isn’t easy to make
it down for as many of the meetings as I would like, but it was
definitely worth the effort last night. We had a larger than usual
crowd, due to the fact that Google was sponsoring pizza and providing
speakers, all apparently part of their current recruiting drive.

Cary Hull of Google talked about twisted. I hadn’t ever really
looked at twisted closely, so the overview and examples he provided were
new material for me. It seems to be very informative and something worth
looking into, particularly since we are looking at redoing the
architecture of part of our system at work, and we will want to handle a
lot of sub-processes which might block on I/O from a number of sources.
We already know we want to use processes to take advantage of
multi-processor systems, but twisted seems to offer some nice tools for
managing those processes.

Sandwiched between Cary and the final presenter, Luis Caamano gave a
presentation on DynaCenter, our use of Pyro, and the event manager we
have built with it. Luis’ talk was our first “public” technical
presentation from Racemi, and it seemed to be well received.

After Luis, Dan Morrill of Google spoke on cross-site scripting
vulnerabilities. Lots of food for thought there, especially regarding
the trustworthiness of data coming from your own database. Dan is on the
web toolkit team at Google. He has obviously given similar presentations
before, and it was clear that he knew what he was talking about. Due to
a miscommunication about network access, the live demo he had planned
wasn’t possible. He wasn’t phased a bit, and proceeded to work up sample
code on the fly in front of the audience.

Noah also announced the formation of the PyAtl Book Club,
membership in which comes with a discount code for O’Reilly books. I’m
looking forward to participating, since it will be something I can do
without making that 90 minute drive. :-) If you are interested, even if
you aren’t in the Atlanta area, join our Google group.

S(a|i)mple Python Programs

Via Jesse Noller, I came across Steve Howell’s SimplePrograms
page in the Python wiki. What an excellent idea!

It’s a perfectly concise reference for program structure and common
usage patterns. It isn’t anywhere close to as exhaustive as the Python
Quick Reference
(can anything 59 pages long qualify as a “quick”
reference?) or the Python Phrasebook (another recent discovery, I
need to pick up a copy) – but that’s the point. It is a lot more
challenging to write concise examples than an exhaustive reference.
Thanks to Steve for a getting the ball rolling.

I do not see an example of raising exceptions. I’ll have to spend some
time thinking of the simplest program that would benefit from using

Dialing down email distractions

I have been experimenting with various productivity hacks lately. I
feel like I’m already fairly productive, based on tracking the amount of
work I accomplish week-to-week. So instead of trying to do more, I’m
trying to maintain the same level of output with less effort (and
hopefully time).

One of the top tips I have seen repeatedly is to reduce the amount of
time spent checking email, and only check it a couple of times per day.
I’m not sure I’m ready go go that hard-core, yet. Our team is spread out
and relies heavily on email for communication. Most of the messages are
important, and there aren’t too many that I feel like I have trouble
keeping up. The biggest issue is that they can be very time-sensitive.
Our code-review process is triggered via email messages generated when a
ticket is given a specific status in trac. If one of us does not notice
the change, the author of the change might be blocked for some period of
time from doing dependent work, until the code review can be completed.
Since we want to encourage code reviews, we don’t want those blockages
to last too long. So, I can’t turn email off entirely. But I can dial it
down fairly far.

Besides cutting down on email during the week, I also want to break
myself of the habit of checking work email over the weekend. Working for
a startup, it is too easy (for me) to get sucked into giving up weekend
after weekend. This is draining, so I’m not as fresh during the week as
I would be if I avoided the work mail. But, I don’t want to give up my
personal email at the same time.

image0So, the question is, how do I strike that balance?

The first step was to decide what my new mail schedule would be. I
tend to get up fairly early in the mornings, and enjoy breakfast on the
patio when the weather is nice enough (it rained last night, finally).
This is relaxing time, but not especially conducive to the long
stretches of deep thought needed for development or debugging. So,
mornings are a good time to do a lot of email, instant messaging with
the rest of the team, review code or documentation, and the other sorts
of tasks that don’t take hours at a stretch to complete. Afternoons are
too warm to sit outside and think anyway, so I move inside to work on
coding projects then. That gives me a schedule: I am willing to have
more interactions (and interruptions) in the morning than the afternoon
and I want to “reclaim” weekends. The next step is to tell Mail the

image1I use the same email client (OS X’s to access all
of my mail accounts. Even though I use IMAP, this lets me read and
search old messages when I don’t have access to the network. The
brute-force way would be to manually change the preference “Check for
new mail” to the appropriate schedule. I hate doing things like this
manually, though.

I’ve done some work with AppleScript and Mail in the past. This
week (I’m not sure why it took me so long to figure out this approach) I
realized I could use AppleScript to control how often Mail checks for
new messages, and which accounts are checked.

Now that I have a general schedule identified, I can configure some
events in iCal using AppleScripts to control Mail. I began by composing
a few AppleScripts in Script Editor.

To check mail frequently in the morning:


To check mail infrequently, for the afternoons:


To turn of automatic checking entirely in the evening:


To disable my work account for the weekend:


To enable my work account on Monday mornings:


With the scripts in place, I configured events in iCal to run the
scripts to adjust my settings at appropriate times.

image7Every week day morning, I turn up the frequency to every 5
minutes. This ensures that by the time I am up and ready to look at
email, the mailbox is up to date.

image8Around lunch time, I turn the frequency back down to once
per hour. I find I don’t even notice the change, and when I come back
from lunch I am ready to settle in and concentrate. I don’t make it
through the afternoon without checking email, but stretching out the
time between checks does help.

image9And in the evenings, I turn email off entirely. Note that
this script only runs Monday-Thursday. On Friday, I leave Mail set to
check messages once per hour, since I do receive personal messages over
the weekend and I want to see those.

image10To avoid being sucked back into work, I disable that
account entirely. Of course, on Monday morning, I have a similar job
scheduled to run the MailCheckWorkEnable script to re-enable the account
for the week.

Disabling the account entirely seemed like a drastic step, but is very
effective. When Monday comes around, I am refreshed and ready to work
again. I do not miss any personal mail, and have not been tempted to
“just look at this one thing” from my work messages.

PyMOTW: os

The os module provides a wrapper for platform specific modules such as
posix, nt, and mac. The API for functions available on all
platform should be the same, so using the os module offers some
measure of portability. Not all functions are available on all
platforms, however. Many of the process management functions
described in this summary are not available for Windows.

Read more at os

PyMOTW: locale

The locale module is part of Python’s internationalization and
localization support library. It provides a standard way to handle
operations that may depend on the language or location of your users.
For example, formatting numbers as currency, comparing strings for
sorting, and working with dates. It does not cover translation (see the
gettext module) or Unicode encoding.

Read more at locale