GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — flexible ignorance · FOSSAware/deepdiff@3ca9461

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • deepdiff
    • deephash.py
    • diff.py
  • tests
    • test_diff_text.py
    • test_hash.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎deepdiff/deephash.py‎

Copy file name to clipboardExpand all lines: deepdiff/deephash.py
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(self,
6969
ignore_numeric_type_changes=False,
7070
ignore_type_subclasses=False,
7171
ignore_string_case=False,
72+
exclude_obj_callback=None,
7273
number_to_string_func=None,
7374
**kwargs):
7475
if kwargs:
@@ -101,6 +102,7 @@ def __init__(self,
101102
self.ignore_string_type_changes = ignore_string_type_changes
102103
self.ignore_numeric_type_changes = ignore_numeric_type_changes
103104
self.ignore_string_case = ignore_string_case
105+
self.exclude_obj_callback = exclude_obj_callback
104106
# makes the hash return constant size result if true
105107
# the only time it should be set to False is when
106108
# testing the individual hash functions for different types of objects.
@@ -197,9 +199,10 @@ def _skip_this(self, obj, parent):
197199
elif self.exclude_regex_paths and any(
198200
[exclude_regex_path.search(parent) for exclude_regex_path in self.exclude_regex_paths]):
199201
skip = True
200-
else:
201-
if self.exclude_types_tuple and isinstance(obj, self.exclude_types_tuple):
202-
skip = True
202+
elif self.exclude_types_tuple and isinstance(obj, self.exclude_types_tuple):
203+
skip = True
204+
elif self.exclude_obj_callback and self.exclude_obj_callback(obj, parent):
205+
skip = True
203206

204207
return skip
205208

Collapse file

‎deepdiff/diff.py‎

Copy file name to clipboardExpand all lines: deepdiff/diff.py
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(self,
5858
ignore_numeric_type_changes=False,
5959
ignore_type_subclasses=False,
6060
ignore_string_case=False,
61+
exclude_obj_callback=None,
6162
number_to_string_func=None,
6263
ignore_nan_inequality=False,
6364
verbose_level=1,
@@ -88,6 +89,7 @@ def __init__(self,
8889
self.ignore_type_subclasses = ignore_type_subclasses
8990
self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group
9091
self.ignore_string_case = ignore_string_case
92+
self.exclude_obj_callback = exclude_obj_callback
9193
self.number_to_string = number_to_string_func or number_to_string
9294
self.ignore_nan_inequality = ignore_nan_inequality
9395
self.hashes = {}
@@ -219,10 +221,12 @@ def __skip_this(self, level):
219221
elif self.exclude_regex_paths and any(
220222
[exclude_regex_path.search(level.path()) for exclude_regex_path in self.exclude_regex_paths]):
221223
skip = True
222-
else:
223-
if self.exclude_types_tuple and (isinstance(level.t1, self.exclude_types_tuple) or
224-
isinstance(level.t2, self.exclude_types_tuple)):
225-
skip = True
224+
elif self.exclude_types_tuple and \
225+
(isinstance(level.t1, self.exclude_types_tuple) or isinstance(level.t2, self.exclude_types_tuple)):
226+
skip = True
227+
elif self.exclude_obj_callback and \
228+
(self.exclude_obj_callback(level.t1, level.path()) or self.exclude_obj_callback(level.t2, level.path())):
229+
skip = True
226230

227231
return skip
228232

Collapse file

‎tests/test_diff_text.py‎

Copy file name to clipboardExpand all lines: tests/test_diff_text.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,16 @@ def test_skip_regexp(self):
16281628
result = {}
16291629
assert result == ddiff
16301630

1631+
def test_skip_exclude_obj_callback(self):
1632+
def exclude_obj_callback(obj, path):
1633+
return True if "skip" in path or isinstance(obj, int) else False
1634+
1635+
t1 = {"x": "a", "y": "b", "z": "c", "skip_1": 0}
1636+
t2 = {"x": "a", "y": "b", "z": "c", "skip_2": 1}
1637+
ddiff = DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback)
1638+
result = {}
1639+
assert result == ddiff
1640+
16311641
def test_skip_str_type_in_dictionary(self):
16321642
t1 = {1: {2: "a"}}
16331643
t2 = {1: {}}
Collapse file

‎tests/test_hash.py‎

Copy file name to clipboardExpand all lines: tests/test_hash.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,16 @@ def test_skip_regex_path(self):
529529
assert 2 in t1_hash
530530
assert t1_hash[2] == t2_hash[2]
531531

532+
def test_skip_exclude_obj_callback(self):
533+
def exclude_obj_callback(obj, parent):
534+
return True if parent == "root['x']" or obj == 2 else False
535+
536+
dic1 = {"x": 1, "y": 2, "z": 3}
537+
t1 = [dic1]
538+
t1_hash = DeepHashPrep(t1, exclude_obj_callback=exclude_obj_callback)
539+
assert t1_hash == {'y': 'str:y', 'z': 'str:z', 3: 'int:3',
540+
get_id(dic1): 'dict:{str:z:int:3}', get_id(t1): 'list:dict:{str:z:int:3}'}
541+
532542
def test_string_case(self):
533543
t1 = "Hello"
534544

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.