Created
March 29, 2016 16:20
-
-
Save rockneurotiko/53d3e0c4fce5df41160b to your computer and use it in GitHub Desktop.
Class that allows to save and load from json
This file contains 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 DictSave(dict): | |
def save(self, fpath): | |
with open(fpath, 'w') as f: | |
json.dump(self, f) | |
def load(self, fpath): | |
with open(fpath, 'r') as f: | |
self = json.load(f) | |
return self | |
# Examples | |
if __name__ == "__main__": | |
a = DictSave() | |
a["test"] = 1 | |
assert(a.get("test") == 1) | |
a.save("testjson.json") | |
b = DictSave().load("testjson.json") | |
assert(b.get("test") == 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment