[Tutor] Shelve & immutable objects

Danny Yoo dyoo at hashcollision.org
Wed Jan 1 08:22:50 CET 2014


According to:

   http://docs.python.org/2/library/shelve.html

The shelve can be opened in 'writeback' mode, which I think might be
relevant to your question.

"By default modified objects are written only when assigned to the
shelf (see Example). If the optional writebackparameter is set to
True, all entries accessed are also cached in memory, and written back
on sync() and close(); this can make it handier to mutate mutable
entries in the persistent dictionary, but, if many entries are
accessed, it can consume vast amounts of memory for the cache, and it
can make the close operation very slow since all accessed entries are
written back (there is no way to determine which accessed entries are
mutable, nor which ones were actually mutated)."

Let's try it:

##################################################
>>> import shelve
>>> db = shelve.open('class-shelve')
>>> db['a-list'] = [1, 2, 3]
>>> db.close()
>>> db = shelve.open('class-shelve', writeback=True)
>>> db['a-list'].append("four")
>>> db.close()
>>> db = shelve.open('class-shelve')
>>> db['a-list']
[1, 2, 3, 'four']
##################################################


So yes, you should be able to use a shelve in writeback mode to
automatically persist the mutable structures.  That being said, the
docs do say to be aware of the implications: it means every accessed
entry's going to be re-persisted because the shelve does not really
watch for mutations: it just checks for access.

Happy New Year!


More information about the Tutor mailing list