GeezerVison is showing github.com — flexible ignorance · FOSSAware/deepdiff@3ca9461
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: deepdiff/deephash.py +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -69,6 +69,7 @@ def __init__(self, | 69 | 69 | ignore_numeric_type_changes=False, | 70 | 70 | ignore_type_subclasses=False, | 71 | 71 | ignore_string_case=False, | | 72 | + exclude_obj_callback=None, | 72 | 73 | number_to_string_func=None, | 73 | 74 | **kwargs): | 74 | 75 | if kwargs: | @@ -101,6 +102,7 @@ def __init__(self, | 101 | 102 | self.ignore_string_type_changes = ignore_string_type_changes | 102 | 103 | self.ignore_numeric_type_changes = ignore_numeric_type_changes | 103 | 104 | self.ignore_string_case = ignore_string_case | | 105 | + self.exclude_obj_callback = exclude_obj_callback | 104 | 106 | # makes the hash return constant size result if true | 105 | 107 | # the only time it should be set to False is when | 106 | 108 | # testing the individual hash functions for different types of objects. | @@ -197,9 +199,10 @@ def _skip_this(self, obj, parent): | 197 | 199 | elif self.exclude_regex_paths and any( | 198 | 200 | [exclude_regex_path.search(parent) for exclude_regex_path in self.exclude_regex_paths]): | 199 | 201 | 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 | 203 | 206 |
| 204 | 207 | return skip | 205 | 208 |
| |
Collapse file Copy file name to clipboardExpand all lines: deepdiff/diff.py +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,7 @@ def __init__(self, | 58 | 58 | ignore_numeric_type_changes=False, | 59 | 59 | ignore_type_subclasses=False, | 60 | 60 | ignore_string_case=False, | | 61 | + exclude_obj_callback=None, | 61 | 62 | number_to_string_func=None, | 62 | 63 | ignore_nan_inequality=False, | 63 | 64 | verbose_level=1, | @@ -88,6 +89,7 @@ def __init__(self, | 88 | 89 | self.ignore_type_subclasses = ignore_type_subclasses | 89 | 90 | self.type_check_func = type_is_subclass_of_type_group if ignore_type_subclasses else type_in_type_group | 90 | 91 | self.ignore_string_case = ignore_string_case | | 92 | + self.exclude_obj_callback = exclude_obj_callback | 91 | 93 | self.number_to_string = number_to_string_func or number_to_string | 92 | 94 | self.ignore_nan_inequality = ignore_nan_inequality | 93 | 95 | self.hashes = {} | @@ -219,10 +221,12 @@ def __skip_this(self, level): | 219 | 221 | elif self.exclude_regex_paths and any( | 220 | 222 | [exclude_regex_path.search(level.path()) for exclude_regex_path in self.exclude_regex_paths]): | 221 | 223 | 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 | 226 | 230 |
| 227 | 231 | return skip | 228 | 232 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/test_diff_text.py +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1628,6 +1628,16 @@ def test_skip_regexp(self): | 1628 | 1628 | result = {} | 1629 | 1629 | assert result == ddiff | 1630 | 1630 |
| | 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 | + | 1631 | 1641 | def test_skip_str_type_in_dictionary(self): | 1632 | 1642 | t1 = {1: {2: "a"}} | 1633 | 1643 | t2 = {1: {}} | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_hash.py +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -529,6 +529,16 @@ def test_skip_regex_path(self): | 529 | 529 | assert 2 in t1_hash | 530 | 530 | assert t1_hash[2] == t2_hash[2] | 531 | 531 |
| | 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 | + | 532 | 542 | def test_string_case(self): | 533 | 543 | t1 = "Hello" | 534 | 544 |
| |
|