Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ignore Iterable Order Option to DeepHash #403

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion deepdiff/deephash.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def __init__(self,
parent="root",
encodings=None,
ignore_encoding_errors=False,
ignore_list_order=True,
**kwargs):
if kwargs:
raise ValueError(
Expand Down Expand Up @@ -190,6 +191,7 @@ def __init__(self,
self.ignore_private_variables = ignore_private_variables
self.encodings = encodings
self.ignore_encoding_errors = ignore_encoding_errors
self.ignore_list_order = ignore_list_order
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this to ignore_iterable_order to keep it consistent with the rest of parameter names in DeepDiff.


self._hash(obj, parent=parent, parents_ids=frozenset({get_id(obj)}))

Expand Down Expand Up @@ -424,7 +426,9 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
'{}|{}'.format(i, v) for i, v in result.items()
]

result = sorted(map(str, result)) # making sure the result items are string and sorted so join command works.
result = map(str, result) # making sure the result items are string so join command works.
if self.ignore_list_order:
result = sorted(result)
result = ','.join(result)
result = KEY_TO_VAL_STR.format(type(obj).__name__, result)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,21 @@ def test_same_sets_same_hash(self):
t2_hash = DeepHashPrep(t2)

assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]

@pytest.mark.parametrize("list1, list2, ignore_list_order, is_equal", [
([1, 2], [2, 1], False, False),
([1, 2], [2, 1], True, True),
([1, 2, 3], [1, 3, 2], False, False),
([1, [1, 2, 3]], [1, [3, 2, 1]], False, False),
([1, [1, 2, 3]], [1, [3, 2, 1]], True, True),
((1, 2), (2, 1), False, False),
((1, 2), (2, 1), True, True),
])
def test_list_ignore_order(self, list1, list2, ignore_list_order, is_equal):
list1_hash = DeepHash(list1, ignore_list_order=ignore_list_order)
list2_hash = DeepHash(list2, ignore_list_order=ignore_list_order)

assert is_equal == (list1_hash[list1] == list2_hash[list2])

@pytest.mark.parametrize("t1, t2, significant_digits, number_format_notation, result", [
({0.012, 0.98}, {0.013, 0.99}, 1, "f", 'set:float:0.0,float:1.0'),
Expand Down