diff --git a/binarycpython/utils/dicts.py b/binarycpython/utils/dicts.py index 68f7c9d33848eea33d25d89ef82cfa264b6a621c..a873117ea2b7a064967b7e7befaf3d636ba51591 100644 --- a/binarycpython/utils/dicts.py +++ b/binarycpython/utils/dicts.py @@ -71,23 +71,31 @@ def recursive_change_key_to_float(input_dict: dict) -> dict: Returns: new_dict: dict of which the keys have been turned to float types where possible + + If input_dict is None or empty, returns an empty dict """ new_dict = collections.OrderedDict() - for key in input_dict: - if isinstance(input_dict[key], (dict, collections.OrderedDict)): - try: - num_key = float(key) - new_dict[num_key] = recursive_change_key_to_float(input_dict[key]) - except ValueError: - new_dict[key] = recursive_change_key_to_float(input_dict[key]) - else: - try: - num_key = float(key) - new_dict[num_key] = input_dict[key] - except ValueError: - new_dict[key] = input_dict[key] + # if the input dict is None or empty, return an empty dict + if input_dict is None or not input_dict: + pass + + else: + # dict has keys, loop over them + for key in input_dict: + if isinstance(input_dict[key], (dict, collections.OrderedDict)): + try: + num_key = float(key) + new_dict[num_key] = recursive_change_key_to_float(input_dict[key]) + except ValueError: + new_dict[key] = recursive_change_key_to_float(input_dict[key]) + else: + try: + num_key = float(key) + new_dict[num_key] = input_dict[key] + except ValueError: + new_dict[key] = input_dict[key] return new_dict