# 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