Skip to content

Commit

Permalink
Merge pull request #413 from cohml/precompiled-regex-simple-diff
Browse files Browse the repository at this point in the history
Precompiled regex simple diff
  • Loading branch information
seperman authored Aug 31, 2023
2 parents de56336 + 32ec182 commit 5ee7d6c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
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 @@ -415,20 +416,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 @@ -874,7 +880,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 @@ -1527,7 +1534,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
60 changes: 59 additions & 1 deletion 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 @@ -551,6 +552,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 Expand Up @@ -1803,4 +1862,3 @@ class Bar(PydanticBaseModel):
diff = DeepDiff(t1, t2)
expected = {'values_changed': {'root.stuff[0].thing': {'new_value': 2, 'old_value': 1}}}
assert expected == diff

0 comments on commit 5ee7d6c

Please sign in to comment.