Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Test if dictionary (dict) entry has been modified
    text
    copied!<p>I have written a small wrapper for the <code>dict</code> built-in class that loads entries (values) of a dictionary from <code>cPickle</code>d files as soon as the respective key is first accessed. When the dictionary is destroyed all loaded entries are written back to disk.</p> <p>Now it would be convenient if I could check if any of the values has been changed and write out only those that in fact have been. <strong>My question therefore is: Does a dictionary know if a value has been changed? Or is there a clever way to transparently implement this?</strong></p> <p>For completeness I attach the code I use. It's called with the path where the files are stored (keys are used as filenames) and with a list of keys for which files exist.</p> <pre><code>import cPickle class DictDB(dict): def __init__(self, path, folders): self.picklepath = path # path to files on disk self.folders = folders # available folders self.loaded_folders = {} def has_key(self, key): return key in self.folders def get(self, key): if not key in self.loaded_folders.keys(): if not key in self.folders: raise KeyError("Folder "+key+" not available") # load from disk self.loaded_folders[key] = cPickle.load(file(self.picklepath + key + ".cpickle2")) return self.loaded_folders[key] def __getitem__(self, key): return self.get(key) def close(self): for folder in self.loaded_folders.keys(): # write back cPickle.dump(self.loaded_folders[folder], file(picklepath + folder + '.cpickle2', 'w'), 2) def __del__(self): self.close() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload