DeepDiff 5.0.0 documentation!

DeepSearch

class deepdiff.search.grep(item, **kwargs)

grep is a more user friendly interface for DeepSearch. It takes exactly the same arguments as DeepSearch. And it works just like grep in linux shell!

Examples

Importing
>>> from deepdiff import grep
>>> from pprint import pprint
Search in list for string
>>> obj = ["long somewhere", "string", 0, "somewhere great!"]
>>> item = "somewhere"
>>> ds = obj | grep(item)
>>> print(ds)
{'matched_values': {'root[3]', 'root[0]'}
Search in nested data for string
>>> obj = ["something somewhere", {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}]
>>> item = "somewhere"
>>> ds = obj | grep(item, verbose_level=2)
>>> pprint(ds, indent=2)
{ 'matched_paths': {"root[1]['somewhere']": 'around'},
  'matched_values': { 'root[0]': 'something somewhere',
                      "root[1]['long']": 'somewhere'}}
class deepdiff.search.DeepSearch(obj, item, exclude_paths=OrderedSet(), exclude_regex_paths=OrderedSet(), exclude_types=OrderedSet(), verbose_level=1, case_sensitive=False, match_string=False, **kwargs)

DeepSearch

Deep Search inside objects to find the item matching your criteria.

Parameters

obj : The object to search within

item : The item to search for

verbose_levelint >= 0, default = 1.

Verbose level one shows the paths of found items. Verbose level 2 shows the path and value of the found items.

exclude_paths: list, default = None.

List of paths to exclude from the report.

exclude_types: list, default = None.

List of object types to exclude from the report.

case_sensitive: Boolean, default = False

match_string: Boolean, default = False

If True, the value of the object or its children have to exactly match the item. If False, the value of the item can be a part of the value of the object or its children

Returns

A DeepSearch object that has the matched paths and matched values.

Supported data types

int, string, unicode, dictionary, list, tuple, set, frozenset, OrderedDict, NamedTuple and custom objects!

Examples

Importing
>>> from deepdiff import DeepSearch
>>> from pprint import pprint
Search in list for string
>>> obj = ["long somewhere", "string", 0, "somewhere great!"]
>>> item = "somewhere"
>>> ds = DeepSearch(obj, item, verbose_level=2)
>>> print(ds)
{'matched_values': {'root[3]': 'somewhere great!', 'root[0]': 'long somewhere'}}
Search in nested data for string
>>> obj = ["something somewhere", {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}]
>>> item = "somewhere"
>>> ds = DeepSearch(obj, item, verbose_level=2)
>>> pprint(ds, indent=2)
{ 'matched_paths': {"root[1]['somewhere']": 'around'},
  'matched_values': { 'root[0]': 'something somewhere',
                      "root[1]['long']": 'somewhere'}}

Back to DeepDiff 5.0.0 documentation!