Skip to content

search_ module

Utilities for searching.


FIRST_TOKEN_REGEX Pattern

First token regex for parse_path_str.

Matches the same as PATH_TOKEN_REGEX but at the start.


PATH_TOKEN_REGEX Pattern

Path token regex for parse_path_str.

Matches .key, ['key'], ["key"], [0], .0, etc.


search_config ReadonlyConfig

Config of functions that can be used in searching and replacement.

ReadonlyConfig(
    find_exact=<function find_exact at 0x122392020>,
    find_start=<function find_start at 0x1223920c0>,
    find_end=<function find_end at 0x122392160>,
    find_substring=<function find_substring at 0x122392200>,
    find_regex=<function find_regex at 0x1223922a0>,
    find_fuzzy=<function find_fuzzy at 0x122392340>,
    find_rapidfuzz=<function find_rapidfuzz at 0x122392480>,
    find=<function find at 0x122392520>,
    replace_exact=<function replace_exact at 0x1223925c0>,
    replace_start=<function replace_start at 0x122392660>,
    replace_end=<function replace_end at 0x122392700>,
    replace_substring=<function replace_substring at 0x1223927a0>,
    replace_regex=<function replace_regex at 0x122392840>,
    replace_fuzzy=<function replace_fuzzy at 0x1223928e0>,
    replace=<function replace at 0x122392980>
)

combine_path_str function

combine_path_str(
    path_str1,
    path_str2
)

Combine two path strings into one.


combine_pathlike_keys function

combine_pathlike_keys(
    key1,
    key2,
    resolve=False,
    minimize=False
)

Combine two path-like keys.


contains_in_obj function

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

Return whether there is any match in an object in an iterative manner.

Argument traversal can be "DFS" for depth-first search or "BFS" for breadth-first search.

See find_in_obj for arguments.


find function

find(
    target,
    string,
    mode='substring',
    ignore_case=False,
    return_type='bool',
    **kwargs
)

Find a target within a string using the specified mode and return type.

The following return types are supported:

  • "bool": True for match, False for no match
  • "start": List of start indices of matches
  • "range": List of (start, end) tuples of matches
  • "match": List of matched strings

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_end function

find_end(
    target,
    string,
    ignore_case=False,
    return_type='bool'
)

Find at the end of a string.


find_exact function

find_exact(
    target,
    string,
    ignore_case=False,
    return_type='bool'
)

Find a string.


find_fuzzy function

find_fuzzy(
    target,
    string,
    ignore_case=False,
    threshold=70,
    max_insertions=None,
    max_substitutions=None,
    max_deletions=None,
    max_l_dist=None,
    return_type='bool'
)

Find a substring in a string using fuzzysearch.


find_in_obj function

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

Find matches in an object in an iterative manner.

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

Argument traversal can be "DFS" for depth-first search or "BFS" for breadth-first search.

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.


find_rapidfuzz function

find_rapidfuzz(
    target,
    string,
    ignore_case=False,
    processor=None,
    threshold=70,
    return_type='bool'
)

Find a substring in a string using RapidFuzz.


find_regex function

find_regex(
    pattern,
    string,
    ignore_case=False,
    flags=0,
    group=None,
    return_type='bool'
)

Find a RegEx pattern in a string.


find_start function

find_start(
    target,
    string,
    ignore_case=False,
    return_type='bool'
)

Find at the start of a string.


find_substring function

find_substring(
    target,
    string,
    ignore_case=False,
    return_type='bool'
)

Find a substring in a string.


flatten_obj function

flatten_obj(
    obj,
    traversal=None,
    annotate_all=False,
    excl_types=None,
    incl_types=None,
    stringify_keys=False,
    max_len=None,
    max_depth=None
)

Flatten object.

Argument traversal can be "DFS" for depth-first search or "BFS" for breadth-first search.

See find_in_obj for arguments.


get_pathlike_key function

get_pathlike_key(
    obj,
    key,
    keep_path=False
)

Get the value under a path-like key in an object.

Paths can be tuples out of individual tokens, or a string with tokens. Each token can be either a key in a mapping, a position in a sequence, or an attribute.

Usage

>>> obj = dict(a=[dict(b="cde")])
>>> vbt.utils.search_.get_pathlike_key(obj, "a")
[{'b': 'cde'}]

>>> vbt.utils.search_.get_pathlike_key(obj, ("a", 0))
>>> vbt.utils.search_.get_pathlike_key(obj, "a.0")
>>> vbt.utils.search_.get_pathlike_key(obj, "a[0]")
{'b': 'cde'}

>>> vbt.utils.search_.get_pathlike_key(obj, ("a", 0, "b"))
>>> vbt.utils.search_.get_pathlike_key(obj, "a[0].b")
>>> vbt.utils.search_.get_pathlike_key(obj, "a[0]['b']")
'cde'

>>> vbt.utils.search_.get_pathlike_key(obj, ("a", 0, "b", 1))
>>> vbt.utils.search_.get_pathlike_key(obj, "a[0].b[1]")
'd'

minimize_pathlike_key function

minimize_pathlike_key(
    key
)

Minimize a path-like key.


parse_path_str function

parse_path_str(
    path_str
)

Parse the path string into a list of tokens.


remove_pathlike_key function

remove_pathlike_key(
    obj,
    key,
    make_copy=True,
    prev_keys=None
)

Remove the value under a path-like key in an object.


replace function

replace(
    target,
    replacement,
    string,
    mode='substring',
    ignore_case=False,
    **kwargs
)

Replace a target string within a source string using the specified mode.


replace_end function

replace_end(
    target,
    replacement,
    string,
    ignore_case=False
)

Replace at the end of a string.


replace_exact function

replace_exact(
    target,
    replacement,
    string,
    ignore_case=False
)

Replace a string.


replace_fuzzy function

replace_fuzzy(
    target,
    replacement,
    string,
    ignore_case=False,
    threshold=70,
    max_insertions=None,
    max_substitutions=None,
    max_deletions=None,
    max_l_dist=None
)

Replace a substring in a string using fuzzysearch.


replace_in_obj function

replace_in_obj(
    obj,
    path_dct
)

Replace matches in an object in a recursive manner using a path dictionary.

Keys in the path dictionary can be path-like keys.


replace_regex function

replace_regex(
    target,
    replacement,
    string,
    ignore_case=False,
    flags=0
)

Replace a target in a string using RegEx.


replace_start function

replace_start(
    target,
    replacement,
    string,
    ignore_case=False
)

Replace at the start of a string.


replace_substring function

replace_substring(
    target,
    replacement,
    string,
    ignore_case=False
)

Replace a substring in a string.


resolve_pathlike_key function

resolve_pathlike_key(
    key,
    minimize=False
)

Convert a path-like key into a path key.


set_pathlike_key function

set_pathlike_key(
    obj,
    key,
    value,
    make_copy=True,
    prev_keys=None
)

Set the value under a path-like key in an object.


stringify_pathlike_key function

stringify_pathlike_key(
    key
)

Convert a path-like key into a string.


unflatten_obj function

unflatten_obj(
    path_dct
)

Unflatten object in a recursive manner using a path dictionary.

Keys in the path dictionary can be path-like keys.


Not class

Not(
    *args,
    **kwargs
)

Class representing a negation when searching.

Superclasses

Inherited members


value field

Value.