From 09fb2374793b0382e4ff827682178eac8373e002 Mon Sep 17 00:00:00 2001 From: dh00601 <dh00601@surrey.ac.uk> Date: Wed, 1 Dec 2021 12:03:18 +0000 Subject: [PATCH] removed all the deepcopy calls. They slow things down, alot. and I think they are not necessary --- binarycpython/utils/dicts.py | 47 ++++++++---------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/binarycpython/utils/dicts.py b/binarycpython/utils/dicts.py index fd5a27ad8..63a2209e5 100644 --- a/binarycpython/utils/dicts.py +++ b/binarycpython/utils/dicts.py @@ -61,11 +61,11 @@ def recursive_change_key_to_float(input_dict): try: num_key = float(key) new_dict[num_key] = recursive_change_key_to_float( - copy.deepcopy(input_dict[key]) + input_dict[key] ) except ValueError: new_dict[key] = recursive_change_key_to_float( - copy.deepcopy(input_dict[key]) + input_dict[key] ) else: try: @@ -89,11 +89,11 @@ def recursive_change_key_to_string(input_dict): if isinstance(key, (int, float)): string_key = "{:g}".format(key) new_dict[string_key] = recursive_change_key_to_string( - copy.deepcopy(input_dict[key]) + input_dict[key] ) else: new_dict[key] = recursive_change_key_to_string( - copy.deepcopy(input_dict[key]) + input_dict[key] ) else: if isinstance(key, (int, float)): @@ -223,10 +223,8 @@ def subtract_dicts(dict_1: dict, dict_2: dict) -> dict: if new_dict[key] == 0: del new_dict[key] - # Else, to be safe we should deepcopy them elif isinstance(dict_1[key], dict): - copy_dict = copy.deepcopy(dict_1[key]) - new_dict[key] = copy_dict + new_dict[key] = dict_1[key] else: msg = "Error: using unsupported type for key {}: {}".format( key, type(dict_1[key]) @@ -289,7 +287,6 @@ def subtract_dicts(dict_1: dict, dict_2: dict) -> dict: if new_dict[key] == 0: del new_dict[key] - # Else, to be safe we should deepcopy them elif isinstance(dict_1[key], dict): new_dict[key] = subtract_dicts(dict_1[key], dict_2[key]) @@ -428,22 +425,10 @@ def merge_dicts(dict_1: dict, dict_2: dict) -> dict: # Add the unique keys to the new dict for key in unique_to_dict_1: - # If these items are ints or floats, then just put them in - if isinstance(dict_1[key], (float, int)): - new_dict[key] = dict_1[key] - # Else, to be safe we should deepcopy them - else: - copy_dict = copy.deepcopy(dict_1[key]) - new_dict[key] = copy_dict + new_dict[key] = dict_1[key] for key in unique_to_dict_2: - # If these items are ints or floats, then just put them in - if isinstance(dict_2[key], (float, int)): - new_dict[key] = dict_2[key] - # Else, to be safe we should deepcopy them - else: - copy_dict = copy.deepcopy(dict_2[key]) - new_dict[key] = copy_dict + new_dict[key] = dict_2[key] # Go over the common keys: for key in overlapping_keys: @@ -566,22 +551,10 @@ def update_dicts(dict_1: dict, dict_2: dict) -> dict: # Add the unique keys to the new dict for key in unique_to_dict_1: - # If these items are ints or floats, then just put them in - if isinstance(dict_1[key], (float, int)): - new_dict[key] = dict_1[key] - # Else, to be safe we should deepcopy them - else: - copy_dict = copy.deepcopy(dict_1[key]) - new_dict[key] = copy_dict + new_dict[key] = dict_1[key] for key in unique_to_dict_2: - # If these items are ints or floats, then just put them in - if isinstance(dict_2[key], (float, int)): - new_dict[key] = dict_2[key] - # Else, to be safe we should deepcopy them - else: - copy_dict = copy.deepcopy(dict_2[key]) - new_dict[key] = copy_dict + new_dict[key] = dict_2[key] # Go over the common keys: for key in overlapping_keys: @@ -684,7 +657,7 @@ def custom_sort_dict(input_dict): sorted_keys = sorted(keys) for key in sorted_keys: - new_dict[key] = custom_sort_dict(copy.deepcopy(input_dict[key])) + new_dict[key] = custom_sort_dict(input_dict[key]) return new_dict return input_dict -- GitLab