-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move compare_dictionaries fun to new module
- Loading branch information
Showing
4 changed files
with
36 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
def compare_dictionaries(dic1, dic2, atol=1e-8, rtol=1e-8, info=True): | ||
def write_info(msg): | ||
if info: | ||
print(msg) | ||
|
||
if dic1.keys() != dic2.keys(): | ||
write_info("different number of keys") | ||
return False | ||
for k in dic1.keys(): | ||
if isinstance(dic1[k], dict) and isinstance(dic2[k], dict): | ||
if not compare_dictionaries(dic1[k], dic2[k], atol, rtol, info): | ||
return False | ||
elif isinstance(dic1[k], float) and isinstance(dic2[k], float): | ||
if not abs(dic1[k] - dic2[k]) <= (atol + rtol * abs(dic2[k])): | ||
write_info(f"number mismatch for {k}") | ||
return False | ||
elif isinstance(dic1[k], list) and isinstance(dic2[k], list): | ||
if len(dic1[k]) != len(dic2[k]): | ||
write_info(f"length mismatch for {k}") | ||
return False | ||
for x, y in zip(dic1[k], dic2[k]): | ||
if x is not None and y is not None: | ||
if abs(x - y) > (atol + rtol * abs(y)): | ||
write_info( | ||
f"number mismatch of element in list {k} ({x} vs {y})" | ||
) | ||
return False | ||
elif (x is None and y is not None) or (x is not None and y is None): | ||
write_info(f"None versus non-None for {k}") | ||
return False | ||
elif dic1[k] != dic2[k]: | ||
write_info(f"difference for {k}") | ||
return False | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters