« | » Main « | »

Running code at Python startup

Thursday 14 January 2010

I recently had need to run a piece of Python code before every invocation of the interpreter. In my case, it was to enable coverage testing of processes spawned by a test suite. Certain tests would run Python code in a subprocess, and I wanted to measure coverage within that process.

I added a function to coverage.py that is suitable for executing on startup. It examines the environment to determine whether to start measuring coverage. (This code isn't released yet, but if you want to try it, it's on the trunk at bitbucket.)

Perl has a command line switch (-M) for this, as does Ruby (-r), but Python doesn't. I came up with two ways to do it for Python, both depending on the site module which is imported automatically on startup.

The first way is to create a sitecustomize.py file on the path somewhere. This module will be imported automatically by site.py. Any lines you add to that file are executed when Python starts:

import coverage
coverage.process_startup()

This is simple, but editing an existing sitecustomize.py could be awkward, and you may not even have permissions to do it.

The second way is to create a .pth file with an executable line. Usually lines in .pth files are directory names, but if the line starts with "import ", it is executed (what a hack!), so you can create a .pth file like this:

import coverage; coverage.process_startup()

This has the advantage that you can create a new file without worrying about touching existing files, all .pth files will be examined and interpreted. Unfortunately, you might have to give the file a strange name, since they are executed alphabetically, and a package you need may only be available after its .pth file has been seen. In my case, I named my .pth file zzz_coverage_process_start.pth to be sure it ran after coverage was added to the path.

Neither of these feels clean or elegant. I'm wondering if there are better techniques I'm overlooking? Python has a clean way to register functions to be called at process exit (atexit), which has been designed so that multiple authors of exit functions don't need to cooperate to avoid interfering with each other. It'd be nice to have a similar facility for startup functions.

PyCon on the Charles, part 1

Wednesday 13 January 2010

The Cambridge Python Meetup will have a session next Wednesday, January 20. Three Boston-area PyCon speakers will be rehearsing their talks. PyCon 2010 is (as you must know) in Atlanta in February. This is a chance for speakers to practice and get some feedback to improve their talks. It's also a chance for Pythonistas who can't get to Atlanta to see some of the PyCon content. Heck, even if you are going to Atlanta, this is a way to resolve some of those "can't be in two places at once" problems at PyCon, which this year will have five parallel tracks!

Full details and RSVP: PyCon on the Charles, part 1. There may even be snacks.

Boston has six speakers this year, so part 2 will be on February 3: PyCon on the Charles, part 2.

Anne Leeds' dyslexia puzzle

Tuesday 5 January 2010

Anne Leeds is a friend of mine from my years at Farm & Wilderness camps. We recently re-connected after 30 years (thank you, internet!) I remembered her as an energetic girl with an interest in graphic design, and was pleased to see that her essence had not changed over the years.

We caught up, and it turns out that she also has a child with a disability, in her case, dyslexia. One of the things she did about it was to create a dyslexia puzzle:

Sliding block dyslexia puzzle

She describes it like this:

This is four views of a sculpture titled, "Dyslexia," in its various positions. It's set up like one of those picture puzzles that's missing a piece and you slide the individual pieces around to unscramble the image. The white box represents a page. The black puzzle pieces are the letter forms "b," "d," "p," and "q," which are all the same shape flopped in different directions, thereby emphasizing the difficulty some people with dyslexia suffer in discerning letter forms. The gray areas, each revealing a new word, show how my son felt about himself during his early school years when he was diagnosed with dyslexia. The idea, as you move the puzzle pieces around, is to replicate an early reading experience, "b is for bad, d is for dumb, p is for pain, q is for quit," and to suggest what a puzzle the experience of learning to live with dyslexia has been.

I love this: it's typographic, it's empathic, it's for kids, it's creative, it's dealing with a disability. It's got everything.

D. Richard Hipp's software universe

Sunday 3 January 2010

You may not have heard the name D. Richard Hipp, but you've used his software: SQLite is his creation, and it's everywhere. SQLite is an impressive piece of work, but it's not alone. Along the way, Hipp also wrote LEMON, his own parser generator for parsing SQL in SQLite.

And now he has his own distributed source control, Fossil, which hosts the SQLite development stream. Fossil is interesting because it's also a distributed wiki and bug tracker, kind of like Trac meets Mercurial or something. As with all of his work, the Fossil documentation is very clear about the design principles and internals, again, very impressive.

SQLite's documentation includes a detailed page about how it is tested, including how its coverage is measured. Needless to say, it is well measured: 100% condition coverage! The description there of the use of C macros to enhance measurement is a good example of how macros can be extremely useful in building complex software, and makes me wish for something with similar capabilities in Python.

I admire Hipp's output, but I worry that it might be somewhat insular. SQLite obviously has great acceptance, but what will happen to Fossil? It has a huge uphill climb to get users, what with Git and Mercurial slugging it out, and a dozen others competing for attention.

It's the age-old dilemma about using the best technology or the most widely accepted. In this case, I don't even know if Fossil is better than the alternatives. At this point it doesn't have the critical mass that would even move it from the Curiosities category to the Look Into It list.

The philosopher

Saturday 2 January 2010

Ben, my 11-year-old, has always been the artist in the family. His style has of course changed over the years, starting with stick figures, but has now progressed to more fleshed out figures. Of course, I couldn't have anticipated how fleshed-out they'd become.

This is his latest, entitled, "The Philosopher":

A freaky philosopher

I just love watching what he comes up with...

Happy 2010

Friday 1 January 2010

Happy 2010 everybody!

All five of us

2010 looks like it will be an interesting year for me:

  • Susan has a new book coming out the end of March,
  • Max will be going to college somewhere in the fall,
  • Antonio has left HP, so work will be much more of a wildcard,

not to mention the usual 365 days of who knows what the future will bring. Stay happy, and take care of yourselves!

« | » Main « | »