Created
October 18, 2015 21:41
-
-
Save puhoy/e036af12376fb6f9884c to your computer and use it in GitHub Desktop.
wrapper for python dict to store as json
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
| import json | |
| class StoredDict(dict): | |
| def __init__(self, filename, **kwargs): | |
| self.filename = filename | |
| file = open(filename, 'w+') | |
| try: | |
| super(StoredDict,self).__init__(json.load(file)) | |
| except ValueError: | |
| super(StoredDict,self).__init__({}) | |
| def commit(self): | |
| json.dump(self, open(self.filename, 'w+')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment