Wednesday, 22 August 2012


Not the snake, the language.

Python is a popular scripting/interpretive language that uses object oriented structures, so as a scripting language it is pretty sophisticated. Not only that it is easy to run it from Eclipse, including debug mode which allows you to step through the code, examine the variables etc, much the same way I can do with Java and C++.

I'm having to learn this because I'm building a small add-on to Gramps. Gramps is the genealogy program I am using to maintain my family history data. I have to recommend Gramps. It is open source, and it can spit out a fully interactive website with the information when you've got everything entered. These two points mean I know I can easily pass the data around to people who just want to view it (the html) or edit it (the open source thing). I'm not sure I'll actually do the latter (well, maybe when I am dead) but the former is good if I just want to dump it all to a CD and hand it around. The recipients don't need to install anything, just run their browser. I even threw a copy onto my phone and it works fine (so I have it with me if I need to look up something).

Gramps also has a nice plugin architecture so it is fairly simple to write some extra code and add it to the system... as long as you do it in Python because that is what the system is written in. I am doing something to the database (more on that in a moment) so I assumed I could just write a bit of Java, talk to the database with JDBC and do what I need. I don't actually have any interactive stuff, just a batch database update.

Except Python has built in a non-SQL database which doesn't really handle JDBC, and Gramps, naturally, uses this because it is right there. It is also, I suspect, more suitable for this kind of data than SQL, though that's always arguable either way. Regardless, it is not happy with JDBC. There are Java interfaces to it, but they look a bit messy and with the entire source of Gramps loaded up I have plenty of Python example code, so I set to work.

What I did was not hard. It is a plugin to scan all the media objects (photographs, in practice) and pull the Exif data from them. Depending on what is present I push the relevant Exif data into the Gramps database. The idea is I keep titles, descriptions and dates on the actual image files. This means they show up in various photo management tools automatically and they don't get detached from the photo. But I also want them to show up in Gramps.

Gramps doesn't do this for itself, or not the way I want anyway. This is partly because the Exif data structures are very loose and they can't cater for the endless combinations to figure out what is where. My images are tagged explicitly by me so I know what Exif tags are being used, so I can write the code to map what I want.

It was not a big deal. But do I like Python?

Not really. It was easy to set up in Eclipse and fairly simple to pick up the syntax, and the debug tools work well. But I prefer a compiler phase that finds my syntax errors (especially when I am making a lot as I feel my way through a new language). Python finds these at runtime, which means I have to keep working through my test sequence. I could have shortened this with a unit test, and I would have done that with Java anyway, but that was more figuring out and I got lazy.

So I'm not keen to switch to Python any time soon, but I can see why people like using it. I will stick to Java and C code for now.
Post a Comment