Context Managers FTW

In their most basic form, Python's context managers make it easier to do the right thing by getting rid of some boiler-plate. For example, it was sometimes tempting to skip the try/finally to close a file object correctly, since Python would usually clean it up anyways when it went out of scope. But it's much easier to do it right this way:

with open('filename') as fp:
  contents = fp.read()

instead of this way:

fp = open('filename')
try:
  contents = fp.read()
finally:
  fp.close()

However, the more I work with context managers, the more they change the ...



Released jprops library

I've uploaded a new Python library jprops to GitHub. It reads and writes Java's .properties file format.

We were using pyjavaproperties at work, but ran into a few limitations. One of our developers had patched it to be able to read from a StringIO object instead of a file object. I had also encountered an issue where it has extended the standard property parsing to interpolate patterns like {var} to expand the value of var as a reference to another property. This interfered with some of our properties files that use ${var} as a pattern recognized by our ...


Simple JavaScript Namespaces

Namespaces are one honking great idea — let's do more of those!

Tim Peters, The Zen of Python

After a few years of mostly server-side development I'm getting back into JavaScript, and for the first time I feel like I'm approaching it as an actual engineering challenge rather than a bunch of quick hacks. As our JS code base is growing I'm breaking things up into namespaces to keep things manageable.

Though JavaScript has no built-in notion of "modules" like many other languages you can use objects to create namespaces, which in their simplest form would look ...


Scripts: svndiff

To make the output of the "svn diff" command more readable here's a small script to pipe the output to the Pygments library to colorize the command line output:

#!/bin/bash
svn diff "$@" | pygmentize -ldiff


Trac Nominated for Linux User Awards

Trac has been nominated in the Linux User Awards for "Best Linux/OSS Developer Tool". Launchpad and Mono are the other nominations in this category, so we're among some pretty big competition. The Trac community has grown tremendously in the past year, so it's nice to see that it's so highly regarded.


Disabling HTML <option>s

If browsers worked as they should it would be pretty simple to disable some of the options in a select list:

<select>
    <option>Enabled</option>
    <option disabled>Disabled</option>
</select>

Of course IE doesn't support this part of the spec, and it's not included in the IE 7 beta, so it's unlikely it will be supported in the near future. So, one option that makes IE behave similarly to the disabled options is to replace them with <optgroup> tags. The <optgroup>s cannot be selected and with a little CSS they look like disabled options. However, the ...


Python SQL Commandline

I hacked together a simple command line app in Python for running SQL using any installed DB-API driver.

Usage depends on the arguments of the connect() method of your DB driver. For example:

python pysql.py psycopg "host=localhost dbname=mydb user=myuser password=mypassword"

or:

python pysql.py sqlite mysqlite.db

or with named parameters:

python pysql.py pymssql --host=localhost --user=sa --password=sapassword

Download: pysql.py


Fun with Evolution Filters

I've fixed up a couple of things in Evolution that have been irritating me for a while using filters to pipe the messages through external apps.

New message notification

I've wanted to have popup notifications when I get new messages, so I wrote a little Python script that parses the message and displays a summary using Galago:

image

Installation

  • Download Ka-Ping's scrape.py library to somewhere on your PYTHONPATH (usually /usr/lib/python2.4/site-packages; change the version number if necessary)
  • Download mail-notify.py and make it executable (chmod a+x mail-notify.py)
  • Add your Evolution filter (use ...