Skip to content

search module

Utilities for searching.


any_in_obj function

any_in_obj(
    obj,
    match_func,
    excl_types=None,
    incl_types=None,
    max_len=None,
    max_depth=None,
    **kwargs
)

Return whether there is any match in an object in a recursive manner.

See find_in_obj() for arguments.


find_and_replace_in_obj function

find_and_replace_in_obj(
    obj,
    match_func,
    replace_func,
    excl_types=None,
    incl_types=None,
    max_len=None,
    max_depth=None,
    make_copy=True,
    check_any_first=True,
    **kwargs
)

Find and replace matches in an object in a recursive manner.

See find_in_obj() for arguments.

Note

If the object is deep (such as a dict or a list), creates a copy of it if any match found inside, thus losing the reference to the original. Make sure to do a deep or hybrid copy of the object before proceeding for consistent behavior, or disable make_copy to override the original in place.


find_in_obj function

find_in_obj(
    obj,
    match_func,
    excl_types=None,
    incl_types=None,
    max_len=None,
    max_depth=None,
    **kwargs
)

Find matches in an object in a recursive manner.

Traverses dicts, tuples, lists and (frozen-)sets. Does not look for matches in keys.

If excl_types is not None, uses is_instance_of() to check whether the object is one of the types that are blacklisted. If so, the object is simply returned. Same for incl_types for whitelisting, but it has a priority over excl_types.

If max_len is not None, processes any object only if it's shorter than the specified length.

If max_depth is not None, processes any object only up to a certain recursion level. Level of 0 means dicts and other iterables are not processed, only matches are expected.

Returns a map of keys (multiple levels get represented by a tuple) to their respective values.

For defaults, see search.


replace_in_obj function

replace_in_obj(
    obj,
    match_dct
)

Replace matches in an object in a recursive manner.

See find_in_obj() for match_dct (returned value).