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 all commits
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_iterable_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_iterable_order = ignore_iterable_order

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_iterable_order:
result = sorted(result)
result = ','.join(result)
result = KEY_TO_VAL_STR.format(type(obj).__name__, result)

Expand Down
2 changes: 2 additions & 0 deletions docs/deephash_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ ignore_private_variables: Boolean, default = True
ignore_encoding_errors: Boolean, default = False
If you want to get away with UnicodeDecodeError without passing explicit character encodings, set this option to True. If you want to make sure the encoding is done properly, keep this as False and instead pass an explicit list of character encodings to be considered via the encodings parameter.

ignore_iterable_order: Boolean, default = True
If order of items in an iterable should not cause the hash of the iterable to be different.

number_format_notation : string, default="f"
number_format_notation is what defines the meaning of significant digits. The default value of "f" means the digits AFTER the decimal point. "f" stands for fixed point. The other option is "e" which stands for exponent notation or scientific notation.
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_iterable_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_ignore_iterable_order(self, list1, list2, ignore_iterable_order, is_equal):
list1_hash = DeepHash(list1, ignore_iterable_order=ignore_iterable_order)
list2_hash = DeepHash(list2, ignore_iterable_order=ignore_iterable_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