Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 1 | """Manage shelves of pickled objects. |
| 2 | |
| 3 | A "shelf" is a persistent, dictionary-like object. The difference |
| 4 | with dbm databases is that the values (not the keys!) in a shelf can |
| 5 | be essentially arbitrary Python objects -- anything that the "pickle" |
| 6 | module can handle. This includes most class instances, recursive data |
| 7 | types, and objects containing lots of shared sub-objects. The keys |
| 8 | are ordinary strings. |
| 9 | |
| 10 | To summarize the interface (key is a string, data is an arbitrary |
| 11 | object): |
| 12 | |
| 13 | import shelve |
Guido van Rossum | 256cbd7 | 1995-02-16 16:30:50 +0000 | [diff] [blame] | 14 | d = shelve.open(filename) # open, with (g)dbm filename -- no suffix |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 15 | |
| 16 | d[key] = data # store data at key (overwrites old data if |
| 17 | # using an existing key) |
| 18 | data = d[key] # retrieve data at key (raise KeyError if no |
| 19 | # such key) |
| 20 | del d[key] # delete data stored at key (raises KeyError |
| 21 | # if no such key) |
| 22 | flag = d.has_key(key) # true if the key exists |
| 23 | list = d.keys() # a list of all existing keys (slow!) |
| 24 | |
| 25 | d.close() # close it |
| 26 | |
| 27 | Dependent on the implementation, closing a persistent dictionary may |
| 28 | or may not be necessary to flush changes to disk. |
| 29 | """ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 30 | |
| 31 | import pickle |
| 32 | import StringIO |
| 33 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 35 | class Shelf: |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 36 | """Base class for shelf implementations. |
| 37 | |
| 38 | This is initialized with a dictionary-like object. |
| 39 | See the module's __doc__ string for an overview of the interface. |
| 40 | """ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 41 | |
| 42 | def __init__(self, dict): |
| 43 | self.dict = dict |
| 44 | |
| 45 | def keys(self): |
| 46 | return self.dict.keys() |
| 47 | |
| 48 | def __len__(self): |
| 49 | return self.dict.len() |
| 50 | |
| 51 | def has_key(self, key): |
| 52 | return self.dict.has_key(key) |
| 53 | |
| 54 | def __getitem__(self, key): |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 55 | f = StringIO.StringIO(self.dict[key]) |
| 56 | return pickle.Unpickler(f).load() |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 57 | |
| 58 | def __setitem__(self, key, value): |
| 59 | f = StringIO.StringIO() |
| 60 | p = pickle.Pickler(f) |
| 61 | p.dump(value) |
| 62 | self.dict[key] = f.getvalue() |
| 63 | |
| 64 | def __delitem__(self, key): |
| 65 | del self.dict[key] |
| 66 | |
| 67 | def close(self): |
Guido van Rossum | cebfa70 | 1995-02-27 13:15:29 +0000 | [diff] [blame] | 68 | if hasattr(self.dict, 'close'): |
| 69 | self.dict.close() |
| 70 | self.dict = None |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 72 | def __del__(self): |
| 73 | self.close() |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 75 | |
| 76 | class DbShelf(Shelf): |
| 77 | """Shelf implementation using the "anydbm" generic dbm interface. |
| 78 | |
| 79 | This is initialized with the filename for the dbm database. |
| 80 | See the module's __doc__ string for an overview of the interface. |
| 81 | """ |
| 82 | |
| 83 | def __init__(self, filename): |
| 84 | import anydbm |
| 85 | Shelf.__init__(self, anydbm.open(filename)) |
| 86 | |
| 87 | |
| 88 | def open(filename): |
| 89 | """Open a persistent dictionary for reading and writing. |
| 90 | |
| 91 | Argument is the filename for the dbm database. |
| 92 | See the module's __doc__ string for an overview of the interface. |
| 93 | """ |
| 94 | |
| 95 | return DbShelf(filename) |