From ed5fd140ebec002295d0bc11657ed2d1006bd9d0 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 27 Feb 2023 15:28:17 -0500 Subject: [PATCH] Optimize shallow_compare_dict() --- netbox/utilities/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index 23c2666df..aec0d896c 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -359,18 +359,18 @@ def prepare_cloned_fields(instance): return QueryDict(urlencode(params), mutable=True) -def shallow_compare_dict(source_dict, destination_dict, exclude=None): +def shallow_compare_dict(source_dict, destination_dict, exclude=tuple()): """ Return a new dictionary of the different keys. The values of `destination_dict` are returned. Only the equality of the first layer of keys/values is checked. `exclude` is a list or tuple of keys to be ignored. """ difference = {} - for key in destination_dict: - if source_dict.get(key) != destination_dict[key]: - if isinstance(exclude, (list, tuple)) and key in exclude: - continue - difference[key] = destination_dict[key] + for key, value in destination_dict.items(): + if key in exclude: + continue + if source_dict.get(key) != value: + difference[key] = value return difference