Deep Difference and Search of any Python object/data with delta and hash capabilities.
—
Search for objects within nested data structures using flexible matching criteria. Provides both class-based search functionality and pipe-friendly grep operations for finding specific values, patterns, or objects within complex Python data structures.
Search for items within nested Python objects with configurable matching options and path filtering.
class DeepSearch:
def __init__(
self,
obj: Any,
item: Any,
exclude_paths: Union[SetOrdered, Set[str], List[str]] = SetOrdered(),
exclude_regex_paths: Union[SetOrdered, Set[Union[str, Pattern[str]]], List[Union[str, Pattern[str]]]] = SetOrdered(),
exclude_types: Union[SetOrdered, Set[type], List[type]] = SetOrdered(),
verbose_level: int = 1,
case_sensitive: bool = False,
match_string: bool = False,
use_regexp: bool = False,
strict_checking: bool = True,
**kwargs
):
"""
Search for objects within nested data structures.
Parameters:
- obj: Object to search within
- item: Item to search for
- exclude_paths: Paths to exclude from search
- exclude_regex_paths: Regex patterns for paths to exclude
- exclude_types: Types to exclude from search
- verbose_level: Level of detail in output (0-2)
- case_sensitive: Whether string matching should be case sensitive (default False)
- match_string: Whether to match strings partially or exactly
- use_regexp: Whether to treat search item as regular expression
- strict_checking: Use strict type checking for matches
"""Methods for analyzing search results and extracting information about found matches.
def get_stats(self) -> Dict[str, Any]:
"""
Get statistics about the search results.
Returns:
Dict containing search statistics including match counts and coverage information.
"""
def get_paths(self) -> List[str]:
"""
Get list of all paths where matches were found.
Returns:
List of path strings indicating where matches were found.
"""Pipe-friendly search function that can be used with the | operator for functional-style searching.
class grep:
def __init__(self, item: Any, **kwargs: Any) -> None:
"""
Create a pipe-friendly search object.
Parameters:
- item: Item to search for
- **kwargs: Same parameters as DeepSearch class
"""
def __ror__(self, other: Any) -> "DeepSearch":
"""
Support for | operator piping.
Usage:
result = data | grep("search_term")
Parameters:
- other: Object to search within
Returns:
DeepSearch result object
"""from deepdiff import DeepSearch
# Search for a value in nested structure
data = {
"users": [
{"id": 1, "name": "John", "email": "john@example.com"},
{"id": 2, "name": "Jane", "email": "jane@example.com"}
],
"settings": {"theme": "dark", "language": "en"}
}
# Search for specific value
search = DeepSearch(data, "John")
print(search)
# {'matched_values': {"root['users'][0]['name']"}}
# Search for partial string match
search = DeepSearch(data, "john", case_sensitive=False, match_string=True)
print(search)
# Finds both "John" and "john@example.com"import re
# Search using regular expressions
email_pattern = re.compile(r'[\w\.-]+@[\w\.-]+\.\w+')
search = DeepSearch(data, email_pattern, use_regexp=True)
print(search)
# Finds all email addresses in the data structure# Exclude specific paths from search
search = DeepSearch(
data,
"John",
exclude_paths=["root['users'][0]['email']"]
)
# Include only specific paths
search = DeepSearch(
data,
"dark",
include_paths=["root['settings']"]
)from deepdiff import grep
# Functional-style searching with pipe operator
result = data | grep("John")
print(result)
# Chain multiple operations
result = data | grep("@", match_string=True) | len
print(f"Found {result} email addresses")
# Search with options
result = data | grep("JOHN", case_sensitive=False)# Exclude certain types from search
data_with_numbers = {
"name": "John",
"age": 30,
"scores": [85, 90, 95],
"metadata": {"created": 2023}
}
# Search excluding numeric types
search = DeepSearch(
data_with_numbers,
30,
exclude_types=[int, float]
)search = DeepSearch(data, "John")
# Get detailed statistics
stats = search.get_stats()
print(f"Total matches: {stats['total_matches']}")
print(f"Matched paths: {stats['matched_paths']}")
# Get all paths where matches were found
paths = search.get_paths()
for path in paths:
print(f"Found match at: {path}")
# Access the raw results
if 'matched_values' in search:
for path, value in search['matched_values'].items():
print(f"At {path}: {value}")# Return paths instead of values
search = DeepSearch(data, "John", return_paths=True)
# Verbose output for debugging
search = DeepSearch(data, "John", verbose_level=2)
# Strict type checking
search = DeepSearch(data, 1, strict_checking=True) # Only matches exactly int(1)
search = DeepSearch(data, 1, strict_checking=False) # Matches 1, 1.0, "1", etc.# Search result structure
SearchResult = Dict[str, Dict[str, Any]]
# Possible result keys:
# - 'matched_values': Dict mapping paths to found values
# - 'matched_paths': Dict mapping paths to match information
# - 'unmatched_paths': Dict of paths that didn't match
# Grep function type
GrepFunction = Callable[[Any], SearchResult]
# Search statistics type
SearchStats = Dict[str, Union[int, List[str], Dict[str, Any]]]Install with Tessl CLI
npx tessl i tessl/pypi-deepdiff