persistence - Python 'shelve' that writes itself to disk after each operation -
i'm doing project in python involves lot of interactive work persistent dictionary. if weren't doing interactive development, use contextlib.closing , confident shelf written disk eventually. stands there's no chunk of code can wrap within 'with' statement. i'd prefer not have trust myself call close() on shelf @ end of session.
the amount of data involved not large, , happily sync shelf disk after each operation. i've found myself writing wrapper shelve i'm not strong enough python programmer identify correct set of dict methods i'd need override. , seems me if i'm doing idea, it's been done before. paradigmatically correct way handle this?
i'll add using shelve because it's simple module, , comes python. if possible i'd prefer avoid doing requires (for example) pulling in complicated library dealing databases.
i'm using winxp sp3, python 2.7.5 via anaconda 1.6.2 (32-bit), , running inside spyder. can tell looking @ modified times file backing shelf shelf isn't updating until call sync or close.
what can subclass dbfilenameshelf
, override __setitem__
, __delitem__
automatically sync after each change. work (untested):
from shelve import dbfilenameshelf class autosyncshelf(dbfilenameshelf): # default newer pickle protocol , writeback=true def __init__(self, filename, protocol=2, writeback=true): dbfilenameshelf.__init__(self, filename, protocol=protocol, writeback=writeback) def __setitem__(self, key, value): dbfilenameshelf.__setitem__(self, key, value) self.sync() def __delitem__(self, key): dbfilenameshelf.__delitem__(self, key) self.sync() my_shelf = autosyncshelf("myshelf")
i can't vouch performance of this, of course.
Comments
Post a Comment