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 — 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 tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • deepdiff
    • search.py
  • tests
    • test_search.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎deepdiff/search.py‎

Copy file name to clipboardExpand all lines: deepdiff/search.py
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class DeepSearch(dict):
4343
If False, the value of the item can be a part of the value of the object or its children
4444
4545
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.
4649
4750
**Returns**
4851
@@ -88,6 +91,7 @@ def __init__(self,
8891
case_sensitive=False,
8992
match_string=False,
9093
use_regexp=False,
94+
strict_checking=True,
9195
**kwargs):
9296
if kwargs:
9397
raise ValueError((
@@ -112,6 +116,7 @@ def __init__(self,
112116
self.use_regexp = use_regexp
113117
if self.use_regexp:
114118
item = re.compile(item)
119+
self.strict_checking = strict_checking
115120

116121
# Cases where user wants to match exact string item
117122
self.match_string = match_string
@@ -266,7 +271,11 @@ def __search_str(self, obj, item, parent):
266271
self.__report(report_key='matched_values', key=parent, value=obj)
267272

268273
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+
):
270279
self.__report(report_key='matched_values', key=parent, value=obj)
271280

272281
def __search_tuple(self, obj, item, parent, parents_ids):
Collapse file

‎tests/test_search.py‎

Copy file name to clipboardExpand all lines: tests/test_search.py
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def test_number_in_list3(self):
4141
result = {"matched_values": {'root[1]'}}
4242
assert DeepSearch(obj, item, verbose_level=1) == result
4343

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+
4450
def test_string_in_root(self):
4551
obj = "long string somewhere"
4652
result = {"matched_values": {'root'}}
@@ -425,11 +431,25 @@ def test_regex_in_string_in_set_verbose(self):
425431
ds = DeepSearch(obj, item, verbose_level=2, use_regexp=True)
426432
assert list(ds["matched_values"].values())[0] == "somewhere"
427433

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+
428441
def test_regex_in_int_in_dictionary(self):
429442
obj = {"long": "somewhere", "num": 232, 0: 0, "somewhere": "around"}
430443
item = "2.*"
431444
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)
433453
assert ds == result
434454

435455
class TestGrep:

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.