![]() | Ned Batchelder : Blog | Code | Text | Site January 2010 » Home : Blog |
« December 2009 | » Main « | February 2010 » Running code at Python startupThursday 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 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.
tagged:
python» 7 reactions PyCon on the Charles, part 1Wednesday 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.
tagged:
python,
gatherings» react IE8's unpopular XSS filterTuesday 12 January 2010 I've been struggling with the XSS filter in IE8. It seems capricious and secretive, and is getting in the way of an application we're running on AOL.com. I asked about it on stackoverflow, and got some more information, but still not enough to get it out of my hair. Anyone have any experience with this beast?
tagged:
browsers,
malware» 3 reactions Anne Leeds' dyslexia puzzleTuesday 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: She describes it like this:
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.
tagged:
art,
autism,
friends & family» 3 reactions 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.
tagged:
source control,
coverage» 10 reactions The philosopherSaturday 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": I just love watching what he comes up with...
tagged:
art,
friends & family» 1 reaction Happy 2010Friday 1 January 2010 Happy 2010 everybody!
2010 looks like it will be an interesting year for me:
not to mention the usual 365 days of who knows what the future will bring. Stay happy, and take care of yourselves!
tagged:
photos,
friends & family» 2 reactions « December 2009 | » Main « | February 2010 » | |