From 05dd3a4fa2fbf4b3c7d7827f48a5e6a4f7cffd74 Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 23 Dec 2017 21:32:08 -0500 Subject: [PATCH] Reordering of setdefault method logic --- src/utils/utils.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/utils/utils.py b/src/utils/utils.py index 5ca1346..fb7b988 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -28,6 +28,7 @@ try: with open('config.json') as infile: config = jsn.load(infile) print('LOADED : config.json') + except FileNotFoundError: with open('config.json', 'w') as outfile: jsn.dump({'client_id': 0, 'info_channel': 0, 'owner_id': 0, 'permissions': 126016, @@ -37,28 +38,30 @@ except FileNotFoundError: def setdefault(filename, default=None, json=False): - if not json: + if json: try: with open(filename, 'rb') as infile: - print('LOADED : {}'.format(filename)) - return pkl.load(infile) + print(f'LOADED : {filename}') + return jsn.load(infile) + except FileNotFoundError: with open(filename, 'wb+') as iofile: - print('FILE NOT FOUND : {} created and loaded with default values'.format(filename)) - pkl.dump(default, iofile) + print(f'FILE NOT FOUND : {filename} created and loaded with default values') + jsn.dump(default, iofile) iofile.seek(0) - return pkl.load(iofile) + return jsn.load(iofile) else: try: with open(filename, 'rb') as infile: print(f'LOADED : {filename}') - return jsn.load(infile) + return pkl.load(infile) + except FileNotFoundError: with open(filename, 'wb+') as iofile: print(f'FILE NOT FOUND : {filename} created and loaded with default values') - jsn.dump(default, iofile) + pkl.dump(default, iofile) iofile.seek(0) - return jsn.load(iofile) + return pkl.load(iofile) def load(filename, *, json=False):