Python footy charts

As mentioned previously, I have been rewriting my graphical AFL footy ladder in Python. This is now complete, and lessons have been learnt.

The initial work was on a direct conversion of the PHP code to Python. This was straightforward, as the syntax and functionality of the two languages was similar. I had also done the original code in a highly modular way, so it was easy to do testing on the way. The biggest change was the need to initalise multidimensional arrays in Python prior to use. PHP will automatically do the initialisation on first use, which is somewhat more convenient, especially when you don't know the size in advance. I ended up setting up a larger array than necessary, then slicing it to the correct size later.

The second step was to "pythonise" the code. In this case, this mostly meant redoing the loops without loop counters. For example:

for i in range(len(rounds)):
  print("%d" % (rounds[i]))

became
for round in rounds:
  print("%d" % (round))

The result was shorter, easier to understand, and less error-prone code.

Thirdly, using the libraries available in Python, I worked on smartening up the graph. The first thing I tried was antialiasing, which had a major improvement. I did this by increasing the graph size, then reducing it using the antialiasing filter in the Python Imaging Library. The rest of the graphics work was done in wxWindows (wxPython), but it was surprisingly easy to transfer images between the two.

Finally, I thought I'd have a play with splines, to see if that gave a better result than staight lines. Surprisingly enough, to do this took just the addition of a 'p' and the movement of an 's', changing a function call from drawLines() to drawSpline()!

The only problem with all these image improvements has been an increase in image size. To cope with this, I changed the images from the lossless png to the lossy jpeg. You can see the difference in quality, but it's a good tradeoff, with the size reduced from about 250kB to 65kB.

About this Entry

This page contains a single entry by James published on April 7, 2004 6:36 PM.

More footy charts was the previous entry in this blog.

Out of the Dark Ages is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Archives