Fun with generators

April 24th, 2008

I’ll admit to being a little scared of Python generators when I first became aware of their existence. I was fairly sure that I didn’t understand them properly, and I failed to see how useful they really are. A few things have opened my eyes in recent months. First, Wesley Chun’s Core Python Programming helped to clarify a couple of issues, and then there was a session at SPA2008 which generated one or two “Aha!” moments, despite ultimately making my head spin.

Most recently, a few folk have blogged about David Beazley’s PyCon ‘08 presentation, Generator Tricks for Systems Programmers. I concur with the person who said it was one of the best introductions he’d come across. I particularly like the approach David’s taken, of avoiding trivial Fibonacci sequence examples and jumping straight into real problems of the kind faced by sysadmins. The performance stats he’s gathered are striking; I hadn’t realised just how competitive writing generator-based scripts for log file analysis can be with non-generator approaches or the more traditional tools used for this purpose, such as awk.

All this has made me more determined to make much better use of generators in my day-to-day Python programming.

New toy

April 12th, 2008

I succumbed to temptation and bought myself an Eee PC - and very nice it is, too. My rationalisation for this is that lugging a bulky Dell Latitude D800 around all the time was giving me backache, but I can’t deny that part of it is the gadget lust of a typical geek!

On teaching programming with Python 3.0

April 8th, 2008

I’ve been doing a lot of thinking lately about Python 3.0 and the impact it might have on our teaching of the language to our first-year students - most of whom have done little or no programming before.

I’ve written a paper about the issues; you might want to check it out if you have an interest in the teaching of programming using Python.

Google App Engine

April 8th, 2008

This is very cool. And Python-based, to boot, with a strong Django flavour to it. Needless to say, I wasn’t quick enough to get a beta testing account. I’m on the waiting list, like countless others, no doubt…

This Week in Django

April 2nd, 2008

Michael Trier & Brian Rosner’s This Week in Django podcast is always worth a listen. I’ve found it to be a very useful way of keeping track of significant changes to trunk or newforms admin. It was good to hear mention of Changeset 7363 (based on my patch :)

Pygments is cool…

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

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

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

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

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…