From 5dbc768af28e9e8e0303b05f47d372411abe4301 Mon Sep 17 00:00:00 2001 From: Robert Izzard <r.izzard@surrey.ac.uk> Date: Sat, 19 Feb 2022 09:50:40 +0000 Subject: [PATCH] add check for empty dict (or None) in recursive_change_key_to_float, as for some reason this was failing. In this case, return an empty dict. --- binarycpython/utils/dicts.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/binarycpython/utils/dicts.py b/binarycpython/utils/dicts.py index 68f7c9d33..a873117ea 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 -- GitLab