Last active
May 21, 2016 18:10
-
-
Save IuryAlves/83692c57376b2a8555c4cdf171949086 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
{"id": 1} |
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
# coding: utf-8 | |
import json | |
def load_conf(conf_file="conf.json"): | |
with open(conf_file) as conf: | |
try: | |
return json.load(conf) | |
except ValueError: | |
return {} | |
def save_conf(data, conf_file="conf.json"): | |
conf = load_conf(conf_file) | |
conf.update(data) | |
with open(conf_file, "w") as file_: | |
json.dump(conf, file_) | |
if __name__ == '__main__': | |
from tempfile import NamedTemporaryFile | |
with NamedTemporaryFile() as temp: | |
save_conf({'id': 1}, conf_file=temp.name) | |
data = load_conf(temp.name) | |
assert data == {'id': 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment