Unofficial Python Module of the Week #1: shelve

Here we are, the second Unofficial Python Module of the Week. Yes, the second – we started from zero (obviously!). This week we are covering the shelve module. Shelve provides you with a very simple Python object store. You can use it where you need quick persistent storage of objects between program runs, it’s much less overhead than using a database – even SQLite. Anyway, lets dive straight into it:

>>> import shelve
>>> shelf = shelve.open("myshelf.db", writeback=True)

Here we import the shelve module (its in the standard library, so there’s no installation required). Then we open our persistent object store, supplying the filename that we want to store the objects in and the writeback parameter, which allows mutable objects to be stored more conveniently (otherwise they are only written when an assignment is performed). The writeback parameter also causes data to be cached in memory, which can be quite memory intensive, so you should call shelf.sync() every so on to flush everything to disk.

You can store anything that can be handled by the Python pickle module in a shelf:

>>> shelf['thedict'] = {'one': 1, 'two': 2, 'three': 3}
>>> shelf.sync()

As you can see, using a shelf is just like using a dictionary. The only real limit is that the keys must be strings. You can also read back values from the shelf as with a dictionary:

>>> print(shelf['thedict'])
{'one': 1, 'two': 2, 'three': 3}

That’s just about it, just remember to close the shelf when you’re finished with it:

>>> shelf.close()

If you want to find out more have a look at the official Python docs for shelve and Doug Hellmann’s PyMOTW posting on the subject.

One response to “Unofficial Python Module of the Week #1: shelve”

  1. Paradiesstaub Avatar
    Paradiesstaub

    Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Bad Behavior has blocked 788 access attempts in the last 7 days.