Related Entries

India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Adding namespace to XML
Opera RSS to OPML

« What is XSLT?
» HOWTO: Encourage Women in Linux

Python pickling

An introductory tutorial to simple data persistence. And my experience with it.

IBM dW: Patrick O'Brien discusses several persistence mechanisms for Python and examines the object-serialization process known as "pickling."

A good one page intro to pickling. In my opinion, Python pickling is a very easy to use persistence mechanism. It might not scale enough for multi-user applications, but for desktop applications, it is really good, when you consider programming effort required and time spent on coding.

I had to write an application that parses Microsoft Visio diagrams to produce some reports. Diagrams can get very complicated and care needed to be taken to look out for special cases, ie., particular node types connected using particular connected types. Visio’s object model is too complicated to manage using Visual Basic for Applications. So, I ended up writing a Python class that stores the parsed tree from Visio. For sake of speeding things I up, I store a reverse tree too - ie., child-parent relations as well as parent-child relations.

This parsing takes resources and time. With 3-4 lines of code, I was able to save the Class in a pickle file and read it when necessary. Users have an option to regenerate the pickle file if the Visio file is newer. Reports are run only from the pickle file data. Another advantage is that the application doesn’t need Visio to be installed on the desktops, once they are able to get the pickle file.

All in all, in a situation where I would’ve had to look into writing data storage code of my own, this was a complete non-issue; thanks to pickle.

//-->