Skip to content

Commit

Permalink
Merge pull request #5385 from jenshnielsen/remove_compare_dict
Browse files Browse the repository at this point in the history
Remove deprecated compare_dictionaries
  • Loading branch information
jenshnielsen authored Sep 25, 2023
2 parents ba5f3f8 + 3c49cc2 commit 907a055
Showing 1 changed file with 0 additions and 77 deletions.
77 changes: 0 additions & 77 deletions qcodes/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,80 +41,3 @@ def warn_units(class_name: str, instance: object) -> None:
f"`units` is deprecated for the `{class_name}` "
f"class, use `unit` instead. {instance!r}"
)


@deprecate("Internal function no longer part of the public qcodes api")
def compare_dictionaries(
dict_1: dict[Hashable, Any],
dict_2: dict[Hashable, Any],
dict_1_name: Optional[str] = "d1",
dict_2_name: Optional[str] = "d2",
path: str = "",
) -> tuple[bool, str]:
"""
Compare two dictionaries recursively to find non matching elements.
Args:
dict_1: First dictionary to compare.
dict_2: Second dictionary to compare.
dict_1_name: Optional name of the first dictionary used in the
differences string.
dict_2_name: Optional name of the second dictionary used in the
differences string.
Returns:
Tuple: Are the dicts equal and the difference rendered as
a string.
"""
err = ""
key_err = ""
value_err = ""
old_path = path
for k in dict_1.keys():
path = old_path + "[%s]" % k
if k not in dict_2.keys():
key_err += f"Key {dict_1_name}{path} not in {dict_2_name}\n"
else:
if isinstance(dict_1[k], dict) and isinstance(dict_2[k], dict):
err += compare_dictionaries(
dict_1[k], dict_2[k], dict_1_name, dict_2_name, path
)[1]
else:
match = dict_1[k] == dict_2[k]

# if values are equal-length numpy arrays, the result of
# "==" is a bool array, so we need to 'all' it.
# In any other case "==" returns a bool
# TODO(alexcjohnson): actually, if *one* is a numpy array
# and the other is another sequence with the same entries,
# this will compare them as equal. Do we want this, or should
# we require exact type match?
if hasattr(match, "all"):
match = match.all()

if not match:
value_err += (
'Value of "{}{}" ("{}", type"{}") not same as\n'
' "{}{}" ("{}", type"{}")\n\n'
).format(
dict_1_name,
path,
dict_1[k],
type(dict_1[k]),
dict_2_name,
path,
dict_2[k],
type(dict_2[k]),
)

for k in dict_2.keys():
path = old_path + f"[{k}]"
if k not in dict_1.keys():
key_err += f"Key {dict_2_name}{path} not in {dict_1_name}\n"

dict_differences = key_err + value_err + err
if len(dict_differences) == 0:
dicts_equal = True
else:
dicts_equal = False
return dicts_equal, dict_differences

0 comments on commit 907a055

Please sign in to comment.