Durus

I've recently been using this nifty little object database, Durus, on several small projects. It's dead simple, the only documentation you need to get started and grok the concepts is their presentation from PyCon 2005. Having worked with ZODB helps – Durus draws heavily from that.

So Durus gives you two basic building blocks. You have the DB connection and you have the Persistent class. The DB connection is fairly obvious: open a database file (or connect to a server), retrieve data, commit, rollback, close. Persistent is where the magic lives: inherit from Persistent and your object will be a first-class database container. When you make changes, Persistent will be notified automatically; commit or abort and the data gets saved or reverted. There's one caveat: Persistent can't detect changes inside mutable attributes (like a list or dict), but there are simple ways around this.

Several power tools come in the package. There's PersistentList and PersistentDict (which behave like list and dict, but detect when you make changes); BTree, which is like PersistentDict, except it's much better for handling a large number of entries; and ComputedAttribute, with is a fancy way of saying "don't persist this piece of information, I want to compute it myself". There's also machinery to run a server with multiple clients but I haven't used any of that.

Mostly Durus just keeps out of your way. The data model is basically Python's data model. No need to map table columns to object fields; no serializing to a foreign format (JSON, XML); just plain Pickle. You still need to have some level of understanding of how the bits are being stored, and object databases are weird animals at that, but it's much more fun than hand-optimizing SQL queries.

Created:
9 Jul 2009, 23:29

Cachetools

I was looking for a simple, off-the-shelf caching library for Python. Dissatisfied with what I could find, I wrote my own, and called it cachetools.

Cachetools provides a simple set/get interface to cache objects manually, either in-memory or to a filesystem shelve-based store; a caching decorator for functions, that indexes return values based on the sha1 hash of a pickle of the call arguments; and some logging facilities. In the future I plan to add proper expiry logic (right now it supports simple LRU, only for in-memory caches) and use SQLite as a backend, since it can easily store and index metadata for cached objects.

I'd love to hear if you find cachetools useful, or if you have any ideas for improvements.

Created:
9 Jun 2009, 11:45

Tiny Python web service

I had an excessive amount of fun today doing some yak shaving. There's this CSS mangling script I'm writing for Naaya, that compares different versions of a file, and we need to run it on many sites, so I decided to package the main code as a web service, and deploy a minimal runner script on each site.

The shaved yak was, of course, a spiffy web service wrapper. It's running behind mod_wsgi, with URL dispatching and request/response objects by Werkzeug, logging to a StringIO that can be sent out with the response, a view that serves static content, another one that serves Genshi templates, a couple of others for JSON and repr output, and, the application auto-reloads when a source file is changed – all this in 99 lines of code, including blank lines and comments. Python is pure awesomeness. :)

The code is up on gist: wsgiapp.py. I think it's useful as a working example of a nicely rounded web service using the latest and greatest libraries from the Python world.

Created:
5 Jun 2009, 00:06
« older articles newer articles »