Python DevCenter
oreilly.comSafari Books Online.Conferences.


Python News

Transactional Persistence for Python


Michael Pelletier has published an article on using the Z Object Database (ZODB), a way of providing transactional persistence for Python programs. Persistence means objects that are stored, or persist, between program runs. This is often done by pickling the object into a simple file which can be accessed later. Transactional means that changes to the objects happen as a batch. If anything goes wrong, the changes are canceled. Transactions also add the ability to undo changes. Sometimes a relational database is used to provide transactional persistence. Relational databases, however, store information in a tabular format. Python objects are rarely tabular. It can be inconvenient to marshal them in and out of your database tables. ZODB mixes the practicality of shelving objects with the benefits of transactions.

Pelletier shows how to create a file to store your data and how to create objects that are persistent. He covers some gotchas as well. ZODB is simple to use. Your biggest hurdle will probably be getting it installed. You have three basic options. Use the module that comes with Zope, use the zodb source package maintained by Andrew M. Kuchling on SourceForge, or check ZODBStandalone out of the cvs server.

The first option only works if you are using the same version of Python Zope is using. (Zope 2.3 uses Python 1.5.2. Zope 2.4 should use Python 2.1) Add the lib/python and lib/python/ZODB directories to your PYTHONPATH, and you should be ready to roll. If you are using a different version of Python, however, you will need to compile your own version from the source code obtained using one of the other two options. Because you need a C compiler, this will be easier to do using Unix systems than using Microsoft Windows. For my Python 2.1 installation on Linux, I used Kuchling's source package. Compiling it was as simple as running the program as described in the README file. On my Windows system, I just stuck with Python 1.5.2 and Zope. Whichever way you choose, once you can import zodb the rest of the tutorial is a breeze.

Stephen Figgins administrates Linux servers for Sunflower Broadband, a cable company.

Read more Python News columns.

Return to the PHP DevCenter.

Sponsored by: