Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:seperman/deepdiff into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Sep 1, 2023
2 parents 96847f2 + 5ee7d6c commit 25b723a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 10 deletions.
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
23 changes: 15 additions & 8 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from math import isclose as is_close
from collections.abc import Mapping, Iterable, Sequence
from collections import defaultdict
from inspect import getmembers
from itertools import zip_longest
from ordered_set import OrderedSet
from deepdiff.helper import (strings, bytes_type, numbers, uuids, datetimes, ListItemRemovedOrAdded, notpresent,
Expand Down Expand Up @@ -417,20 +418,25 @@ def _diff_enum(self, level, parents_ids=frozenset(), local_tree=None):

def _diff_obj(self, level, parents_ids=frozenset(), is_namedtuple=False, local_tree=None):
"""Difference of 2 objects"""
processing_error = False
try:
if is_namedtuple:
t1 = level.t1._asdict()
t2 = level.t2._asdict()
else:
elif all('__dict__' in dir(t) for t in level):
t1 = detailed__dict__(level.t1, ignore_private_variables=self.ignore_private_variables)
t2 = detailed__dict__(level.t2, ignore_private_variables=self.ignore_private_variables)
except AttributeError:
try:
elif all('__slots__' in dir(t) for t in level):
t1 = self._dict_from_slots(level.t1)
t2 = self._dict_from_slots(level.t2)
except AttributeError:
self._report_result('unprocessed', level, local_tree=local_tree)
return
else:
t1 = {k: v for k, v in getmembers(level.t1) if not callable(v)}
t2 = {k: v for k, v in getmembers(level.t2) if not callable(v)}
except AttributeError:
processing_error = True
if processing_error is True:
self._report_result('unprocessed', level, local_tree=local_tree)
return

self._diff_dict(
level,
Expand Down Expand Up @@ -876,7 +882,8 @@ def _diff_by_forming_pairs_and_comparing_one_by_one(
x,
y,
child_relationship_class=child_relationship_class,
child_relationship_param=j)
child_relationship_param=j
)
self._diff(next_level, parents_ids_added, local_tree=local_tree)

def _diff_ordered_iterable_by_difflib(
Expand Down Expand Up @@ -1529,7 +1536,7 @@ def _diff(self, level, parents_ids=frozenset(), _original_type=None, local_tree=
if isinstance(level.t1, booleans):
self._diff_booleans(level, local_tree=local_tree)

if isinstance(level.t1, strings):
elif isinstance(level.t1, strings):
self._diff_str(level, local_tree=local_tree)

elif isinstance(level.t1, datetimes):
Expand Down
4 changes: 4 additions & 0 deletions deepdiff/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ def __setattr__(self, key, value):
else:
self.__dict__[key] = value

def __iter__(self):
yield self.t1
yield self.t2

@property
def repetition(self):
return self.additional['repetition']
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
2 changes: 1 addition & 1 deletion requirements-cli.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
click==8.1.3
pyyaml==6.0
pyyaml==6.0.1
59 changes: 59 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import pytest
import logging
import re
import uuid
from enum import Enum
from typing import List
Expand Down Expand Up @@ -605,6 +606,64 @@ class MyEnum(Enum):
}
assert ddiff == result

def test_precompiled_regex(self):

pattern_1 = re.compile('foo')
pattern_2 = re.compile('foo')
pattern_3 = re.compile('foo', flags=re.I)
pattern_4 = re.compile('(foo)')
pattern_5 = re.compile('bar')

# same object
ddiff = DeepDiff(pattern_1, pattern_1)
result = {}
assert ddiff == result

# same pattern, different object
ddiff = DeepDiff(pattern_1, pattern_2)
result = {}
assert ddiff == result

# same pattern, different flags
ddiff = DeepDiff(pattern_1, pattern_3)
result = {
'values_changed': {
'root.flags': {
'new_value': 34,
'old_value': 32,
},
}
}
assert ddiff == result

# same pattern, different groups
ddiff = DeepDiff(pattern_1, pattern_4)
result = {
'values_changed': {
'root.pattern': {
'new_value': '(foo)',
'old_value': 'foo',
},
'root.groups': {
'new_value': 1,
'old_value': 0,
},
}
}
assert ddiff == result

# different pattern
ddiff = DeepDiff(pattern_1, pattern_5)
result = {
'values_changed': {
'root.pattern': {
'new_value': 'bar',
'old_value': 'foo',
},
}
}
assert ddiff == result

def test_custom_objects_change(self):
t1 = CustomClass(1)
t2 = CustomClass(2)
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

0 comments on commit 25b723a

Please sign in to comment.