Tutorial by Examples

To shelve an object, first import the module and then assign the object value as follows: import shelve database = shelve.open(filename.suffix) object = Object() database['key'] = object
import shelve d = shelve.open(filename) # open -- file may get suffix added by low-level # library d[key] = data # store data at key (overwrites old data if # using an existing key) data = d[key] # retrieve a C...
The simplest way to use shelve is via the DbfilenameShelf class. It uses anydbm to store the data. You can use the class directly, or simply call shelve.open(): import shelve s = shelve.open('test_shelf.db') try: s['key1'] = { 'int': 10, 'float':9.5, 'string':'Sample data' } finally: ...
Shelves do not track modifications to volatile objects, by default. That means if you change the contents of an item stored in the shelf, you must update the shelf explicitly by storing the item again. import shelve s = shelve.open('test_shelf.db') try: print s['key1'] s['key1']['new_...

Page 1 of 1