Archive for March, 2008

Pygments is cool…

Friday, March 28th, 2008

Pygments has been on my radar for a while, but I hadn’t looked closely at it until the other day, when I had a need to highlight some code fragments in a reST document that I’m writing. Hacking rst2html.py to do the necessary work was easy - just a copy-and-paste from some sample code in the docs. But then I hit a problem; my document includes code fragments written for both Python 2.5 and Python 3.0, and Pygments can handle the former but not the latter.

I delved into the source code with some trepidation, wondering how long it would take to hack a solution of some kind. I needn’t have worried. Pygments has good docs and a very clean design, so it took just a few minutes of study to figure out what was required. Less than fifteen minutes later, I had a Python3Lexer class plugged in and working!

If you want the code, it’s now been merged into Pygments’ Mercurial repository, courtesy of Pygments lead developer, Georg Brandl (Cheers, Georg!) Check it out with

  hg clone http://dev.pocoo.org/hg/pygments-main

Printing in Python 3.0

Thursday, March 27th, 2008

I’ve been playing with Python 3.0 recently. It does some nice things, like fixing the asymmetry between reading from stdin and writing to stdout by making print a function rather than a statement:

print('Square-root of', x, 'is', math.sqrt(x))

There’s also a new (C#-inspired?) syntax for string formatting:

output = 'Square-root of {0:g} is {1:g}'
print(output.format(x, math.sqrt(x)))

It’s a shame that they haven’t closed the loop by providing a printf function of some kind. Still, I suppose it isn’t hard to write one:

def printf(format_string, *args, **kwargs):
    print(format_string.format(*args, **kwargs))

Back from SPA2008

Wednesday, March 19th, 2008

Got back from SPA2008 earlier this evening. The past couple of days have been pretty exhausting, with some good sessions and some that didn’t quite hit the spot. The Erlang session ended up being a little disappointing, as there was insufficient time to get to grips with many of the exercises and hence develop a feel for the language. Another session, on embedding domain-specific languages in Java, turned out to be really thought-provoking. I was familiar with the concept of method chaining, but it hadn’t really registered with me that doing this essentially created a domain-specific language from Java syntax. The final session I attended, on the principles behind MapReduce and Hadoop, also turned out to be very interesting.

I didn’t get to many of the diversions, but I did make time for the Spanish guitar recital by David Harvey on Tuesday - an excellent way of winding down after a long day.

On balance, I’d say this was an even better SPA conference for me than the one in 2005. I’ve come to the conclusion that these are tremendously valuable events, even for folk in academia. I can’t think of many better ways of picking up on current trends in industry. SPA folk are friendly, passionate about their profession, keen to share what they know with you, and equally keen to learn from the things that you have to say. The event is intense, stimulating, but good fun, too. I hope it won’t be another three years until my next visit…

SPA2008 - day 2

Tuesday, March 18th, 2008

A good day today.  Went to an intense but stimulating three-hour session on Python generators in the morning, which had gone beyond my prior knowledge of them within the first half-hour.  I was struggling to grasp some of the more advanced aspects of what was being demonstrated, but I certainly came out of the room with a better understanding than when I went in.

Other sessions of note included a panel discussion asking whether software practice had actually advanced over the past 15 years, and a thought-provoking presentation on the design of domain-specific languages.  Between this and the evening meal, I had the chance to run a Django BoF (basically just a demo and a discussion).  Four people turned up and we had some good discussion of Django vs. Rails.

SPA2008 - day 1

Monday, March 17th, 2008

I’m at SPA2008, the BCS conference for Software Practice Advancement - feeling quite tired after having led a 6-hour introductory tutorial on Python and Django.

I think it went reasonably well, although we were very pushed for time towards the end.  Cutting out one of the exercises helped, but the final group exercise on Django was still somewhat rushed.  I reckon we got far enough to convey the flavour of Django development, at least.

I might offer a BoF session on Django as a follow-up, to discuss some of the things that were omitted from the tutorial and to give other delegates an opportunity to learn about the best Python web development framework…