Using Python to get rid of .doc

Written by fredrik

7 februari, 2012

I’ll be appearing att Software Passion to speak about using Python for protocol specifications, instead of using an external document to write the specification, and then try to implement it from there (or, perhaps more common, implementing it and then trying to keep the document up-to-date).

A while ago at Visual Units, the situation was this: There was a protocol to transfer data over TCP from fleet management black boxes running J2ME to a server running Python, which then stored that data so interesting things could be done with it. Accompanying the protocol was a ever-slightly-out-of-date protocol specification, and a client implementation in Python used for testing the server.

This means that we had four different implementations of the protocol: one in Java, two in Python, and one in English. If one of those was not updated when the others were, the system was no longer consistent, and might break in interesting ways.

Since this created a lot of work for me, I set out to change things. First, I searched for viable existing solutions, but the need to keep the protocol compact (telematics data transfer is expensive), and J2ME support meant I did not find anything to use off the shelf.

Instead I started to implement my own solution, with a vision that I would implement the protocol once, and use it everywhere – Java, Python, and English. In the end, using a couple of hundred of rows of Python, we can now specify a protocol thus:

Listing 1.

message = string
timestamp = i64
timediff = i32

ping = ("A ping, with a time and message",
         timestamp, message)

pong = ("A pong, with message, timestamp and perceived lag",
        timestamp, timediff, message)

…and from this, we create Java source code for the terminals, the Python clients and servers use it directly when packing and parsing messages, and the documentation for the poor souls who might want to read English instead of Python is generated.

Want to know how this was made possible, see some code, and point and laugh at my miserable attempts that failed? Want to know why meta-classes were absolutely vital – or not? Register for Software Passion where I’ll be talking about this – if you use the promontion code ’BLAAG’ when registering, you’ll even get a 10% discount!

You May Also Like…

0 kommentarer