From 44db7d922c75839e1f91391b8743cf18a9ec95cf Mon Sep 17 00:00:00 2001 From: Michael Bottini Date: Wed, 16 May 2018 19:52:44 -0700 Subject: [PATCH] Add type-checking to dialogue create_run function --- dialogue.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/dialogue.py b/dialogue.py index 7ceca3d..b10b1d6 100644 --- a/dialogue.py +++ b/dialogue.py @@ -18,6 +18,14 @@ TEMPLATE_TRANS = 'translations' TEMPLATE_DEFAULT = 'prop_options' +TYPE_CHECK_FUNC = { + 'string' : lambda x : str(x), + 'int' : lambda x : int(x), + 'integer' : lambda x : int(x), + 'float' : lambda x : float(x), + 'bool' : lambda x : x not in set(['false', 'False']), + 'boolean' : lambda x : x not in set(['false', 'False']), +} def write_json(filename, python_dict): """ @@ -116,8 +124,14 @@ def create_run(run_list, template_dict): template_dict[template_type]['annotations'][arg], template_dict[template_type]['types'][arg])) else: - new_run[RUNLIST_ARGS][arg] = user_input - break + try: + type_func = TYPE_CHECK_FUNC[ + template_dict[template_type][TEMPLATE_TYPES][arg]] + user_input = type_func(user_input) + new_run[RUNLIST_ARGS][arg] = user_input + break + except: + print('Invalid input.') except: print('Invalid input.') while True: