Created
April 28, 2015 20:42
-
-
Save davidrios/87f7b2a5d93aedcecb85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PersistentDict(object): | |
| def __init__(self, store_file): | |
| self._store_file = store_file | |
| self._is_modified = False | |
| self.reload() | |
| def reload(self): | |
| if self._is_modified: | |
| raise Exception('data was modified and not persisted') | |
| with open(self._store_file, 'r') as fp: | |
| self._data = json.loads(fp.read()) | |
| def persist(self): | |
| with open(self._store_file, 'w') as fp: | |
| fp.write(json.dumps(self._data)) | |
| self._is_modified = False | |
| def __getitem__(self, key): | |
| self.reload() | |
| return self._data[key] | |
| def __setitem__(self, key, value): | |
| self._is_modified = True | |
| self._data[key] = value | |
| def __delitem__(self, key): | |
| del self._data[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment