GeezerVison is showing github.com — feat: add the strick_checking flag to DeepSearch · adjust/deepdiff@282a567
This repository was archived by the owner on Dec 11, 2023. It is now read-only.
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/search.py +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,9 @@ class DeepSearch(dict): | 43 | 43 | If False, the value of the item can be a part of the value of the object or its children | 44 | 44 |
| 45 | 45 | use_regexp: Boolean, default = False | | 46 | + strict_checking: Boolean, default = False | | 47 | + If True, it won't check the type of the object to match, so '1234' will match | | 48 | + the int 1234. | 46 | 49 |
| 47 | 50 | **Returns** | 48 | 51 |
| @@ -88,6 +91,7 @@ def __init__(self, | 88 | 91 | case_sensitive=False, | 89 | 92 | match_string=False, | 90 | 93 | use_regexp=False, | | 94 | + strict_checking=True, | 91 | 95 | **kwargs): | 92 | 96 | if kwargs: | 93 | 97 | raise ValueError(( | @@ -112,6 +116,7 @@ def __init__(self, | 112 | 116 | self.use_regexp = use_regexp | 113 | 117 | if self.use_regexp: | 114 | 118 | item = re.compile(item) | | 119 | + self.strict_checking = strict_checking | 115 | 120 |
| 116 | 121 | # Cases where user wants to match exact string item | 117 | 122 | self.match_string = match_string | @@ -266,7 +271,11 @@ def __search_str(self, obj, item, parent): | 266 | 271 | self.__report(report_key='matched_values', key=parent, value=obj) | 267 | 272 |
| 268 | 273 | def __search_numbers(self, obj, item, parent): | 269 | | - if item == obj or (self.use_regexp and item.search(str(obj))): | | 274 | + if ( | | 275 | + item == obj | | 276 | + or (not self.strict_checking and item == str(obj)) | | 277 | + or (not self.strict_checking and self.use_regexp and item.search(str(obj))) | | 278 | + ): | 270 | 279 | self.__report(report_key='matched_values', key=parent, value=obj) | 271 | 280 |
| 272 | 281 | def __search_tuple(self, obj, item, parent, parents_ids): | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_search.py +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,12 @@ def test_number_in_list3(self): | 41 | 41 | result = {"matched_values": {'root[1]'}} | 42 | 42 | assert DeepSearch(obj, item, verbose_level=1) == result | 43 | 43 |
| | 44 | + def test_number_in_list_strict_false(self): | | 45 | + obj = ["a", "10", 10, 20] | | 46 | + item = "20" | | 47 | + result = {"matched_values": {'root[3]'}} | | 48 | + assert DeepSearch(obj, item, verbose_level=1, strict_checking=False) == result | | 49 | + | 44 | 50 | def test_string_in_root(self): | 45 | 51 | obj = "long string somewhere" | 46 | 52 | result = {"matched_values": {'root'}} | @@ -425,11 +431,25 @@ def test_regex_in_string_in_set_verbose(self): | 425 | 431 | ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True) | 426 | 432 | assert list(ds["matched_values"].values())[0] == "somewhere" | 427 | 433 |
| | 434 | + def test_regex_in_int_in_dictionary_with_strict_checking(self): | | 435 | + obj = {"long": "somewhere", "num": 232, 0: 0, "somewhere": "around"} | | 436 | + item = "2.*" | | 437 | + result = {} | | 438 | + ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True) | | 439 | + assert ds == result | | 440 | + | 428 | 441 | def test_regex_in_int_in_dictionary(self): | 429 | 442 | obj = {"long": "somewhere", "num": 232, 0: 0, "somewhere": "around"} | 430 | 443 | item = "2.*" | 431 | 444 | result = {"matched_values": {"root['num']"}} | 432 | | - ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True) | | 445 | + ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True, strict_checking=False) | | 446 | + assert ds == result | | 447 | + | | 448 | + def test_regex_in_int_in_dictionary_returns_partial_match(self): | | 449 | + obj = {"long": "somewhere", "num": 1123456, 0: 0, "somewhere": "around"} | | 450 | + item = "1234" | | 451 | + result = {"matched_values": {"root['num']"}} | | 452 | + ds = DeepSearch(obj, item, verbose_level=1, use_regexp=True, strict_checking=False) | 433 | 453 | assert ds == result | 434 | 454 |
| 435 | 455 | class TestGrep: | |
|