From e2c112fa9f958d70310071dcec444ac2d9929a76 Mon Sep 17 00:00:00 2001 From: Mostafa Gamal Date: Fri, 16 Mar 2018 19:44:54 +0200 Subject: [PATCH] Easydict is used instead of bunch. Improved exception handling. --- main.py | 7 +------ utils/utils.py | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index fa06a49..768cf41 100644 --- a/main.py +++ b/main.py @@ -6,12 +6,7 @@ def main(): # Parse the JSON arguments - config_args = None - try: - config_args = parse_args() - except: - print("Add a config file using \'--config file_name.json\'") - exit(1) + config_args = parse_args() tf.reset_default_graph() diff --git a/utils/utils.py b/utils/utils.py index 689865a..6760e4e 100755 --- a/utils/utils.py +++ b/utils/utils.py @@ -2,9 +2,11 @@ import numpy as np import random import tensorflow as tf -from bunch import Bunch +from easydict import EasyDict as edict +from pprint import pprint import argparse import json +import sys def parse_args(): """ @@ -13,20 +15,34 @@ def parse_args(): :rtype: tuple """ # Create a parser - parser = argparse.ArgumentParser(description="A2C Tensorflow implementation") - parser.add_argument('--version', action='version', version='%(prog)s 0.0.1') + parser = argparse.ArgumentParser(description="A2C TensorFlow Implementation") + parser.add_argument('--version', action='version', version='%(prog)s 1.0.0') parser.add_argument('--config', default=None, type=str, help='Configuration file') # Parse the arguments args = parser.parse_args() - # parse the configurations from the config json file provided - with open(args.config, 'r') as config_file: - config_args_dict = json.load(config_file) - # convert the dictionary to a namespace using bunch lib - config_args = Bunch(config_args_dict) + # Parse the configurations from the config json file provided + try: + if args.config is not None: + with open(args.config, 'r') as config_file: + config_args_dict = json.load(config_file) + else: + print("Add a config file using \'--config file_name.json\'", file=sys.stderr) + exit(1) + + except FileNotFoundError: + print("ERROR: Config file not found: {}".format(args.config), file=sys.stderr) + exit(1) + except json.decoder.JSONDecodeError: + print("ERROR: Config file is not a proper JSON file!", file=sys.stderr) + exit(1) + + config_args = edict(config_args_dict) + + pprint(config_args) + print("\n") - print(config_args) return config_args