Skip to content

Commit

Permalink
Easydict is used instead of bunch. Improved exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
MG2033 committed Mar 16, 2018
1 parent 44767af commit e2c112f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
7 changes: 1 addition & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
34 changes: 25 additions & 9 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand All @@ -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


Expand Down

0 comments on commit e2c112f

Please sign in to comment.