| |
| A "shelf" is a persistent, dictionary-like object. The difference with "dbm" |
| databases is that the values (not the keys!) in a shelf can be essentially |
| arbitrary Python objects --- anything that the :mod:`pickle` module can handle. |
| This includes most class instances, recursive data types, and objects containing |
| lots of shared sub-objects. The keys are ordinary strings. |
| |
| |
n | .. function:: open(filename[,flag='c'[,protocol=``None``[,writeback=``False``]]]) |
n | .. function:: open(filename[, flag='c'[, protocol=None[, writeback=False]]]) |
| |
| Open a persistent dictionary. The filename specified is the base filename for |
| the underlying database. As a side-effect, an extension may be added to the |
| filename and more than one file may be created. By default, the underlying |
| database file is opened for reading and writing. The optional *flag* parameter |
| has the same interpretation as the *flag* parameter of :func:`anydbm.open`. |
| |
| By default, version 0 pickles are used to serialize values. The version of the |
| # such key) |
| del d[key] # delete data stored at key (raises KeyError |
| # if no such key) |
| flag = d.has_key(key) # true if the key exists |
| klist = d.keys() # a list of all existing keys (slow!) |
| |
| # as d was opened WITHOUT writeback=True, beware: |
| d['xx'] = range(4) # this works as expected, but... |
t | d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!!! |
t | d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)! |
| |
| # having opened d without writeback=True, you need to code carefully: |
| temp = d['xx'] # extracts the copy |
| temp.append(5) # mutates the copy |
| d['xx'] = temp # stores the copy right back, to persist it |
| |
| # or, d=shelve.open(filename,writeback=True) would let you just code |
| # d['xx'].append(5) and have it work as expected, BUT it would also |