March 07, 2006
What a load of CRUD

I'm taking the opportunity of all the waiting around time to write some great Python stuff. I'm away from the office for at least two weeks, which is far too great a strech of time to be away from our subversion repository for me to feel that I'll be able to merge stuff back into the trunk if I were to make any big changes, so I can't work on my main project - but I have roadkill with me, and a locker to leave it in while I'm in court, so I have a little side project on the go.

We are finding that we have to write huge swathes of Java code for each new business entity (policy, claim, broker, that kind of thing) that we want to make available for our users - domain objects implementing soft coded rules (in Jython!), data access objects, model view controller classes and the JSPs to go with them, web services and clients, plus interfaces and unit tests for them all, and a ton of config files - it just goes on and on. It's at least couple of days work just to get the basics in place, and it's the worst kind of work - repetitive and boring without being easy.

But it occured to be that 90% of this stuff could be automatically generated from the information in the Hibernate mapping document that we write for each of these entities - more given the fact that Hibernate helpfully allows you to add your own meta tags to your mappings, so I can add stuff to the mappings specifying which fields get shown in which places, for example.

I've not done any serious work in Python for a while - it's been all Java. So it comes as a surprise, once again, how powerful Python is. And how much fun! Nice to be reminded.

Aside from Python itself, there's a wierd kind of synergistic effect you get when you combine various well (Pythonically) designed modules. For example - I'm using classses inheriting from a new-style-class variation on Alex Martellini's Bunch class to store data about the entities in question, and their properties, and I'm using ElementTree to parse the mapping file. (I'm using ElementTree for its elegance here, not for its performance. The mapping files are tiny.) The fact that ElementTree spits out a node's attributes as a dictionary made the data just fall into my objects, so my parser has an obscenely small amount of code considering how much it does, it's still very readable.

Then I use Cheetah to generate the Java code, and again, the fact that Cheetah can be driven by my objects as they are means that the only code that I have to add to them is for derived values (which I'm implementing as properties) - no dross, only real domain logic.

I've pretty much finished the Python code now, I think. :-( Just the rest of the templates to build.

Posted to Python by Simon Brunning at March 07, 2006 08:02 PM
Post a comment

Email Address:



Remember info?