Skip to content

Commit

Permalink
Add experimental is_np_dict_equal_try_except function
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Oct 3, 2024
1 parent 18d9d53 commit dfdf737
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/pymatgen/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def is_np_dict_equal(dict1, dict2, /) -> bool:
"""Compare two dict whose value could be np arrays.
"""Compare two dict whose value could be NumPy arrays.
Args:
dict1 (dict): The first dict.
Expand All @@ -19,3 +19,25 @@ def is_np_dict_equal(dict1, dict2, /) -> bool:
return False

return all(np.array_equal(dict1[key], dict2[key]) for key in dict1)


def is_np_dict_equal_try_except(dict1, dict2, /) -> bool:
"""Another implementation with try-except.
TODO: need speed test.
"""
if dict1.keys() != dict2.keys():
return False

for key in dict1:
value1 = dict1[key]
value2 = dict2[key]

try:
if value1 != value2:
return False
except ValueError:
if not np.array_equal(value1, value2):
return False

return True

0 comments on commit dfdf737

Please sign in to comment.