pythonwrench.re module¶
- pythonwrench.re.compile_patterns(patterns: str | Pattern | Iterable[str | Pattern]) list[Pattern][source]¶
Compile patterns-like to a list.
- pythonwrench.re.filter_with_patterns(x: ~typing.Iterable[str], include: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern] | None = '.*', *, exclude: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern] | None = (), match_fn: ~typing.Callable[[str | ~re.Pattern, str], ~typing.Any] = <function search>) list[str][source]¶
- pythonwrench.re.find_patterns(x: str, patterns: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern], *, match_fn: ~typing.Callable[[str | ~re.Pattern, str], ~typing.Any] = <function search>, default: __SPHINX_IMMATERIAL_TYPE_VAR__V_T = -1) int | T[source]¶
Find index of a pattern that match the first argument. If no pattern matches, returns the default value (-1).
- pythonwrench.re.get_key_fn(patterns: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern], *, match_fn: ~typing.Callable[[str | ~re.Pattern, str], ~typing.Any] = <function search>) Callable[[str], int][source]¶
Generate key_fn to sorted list of string using multiple patterns.
Example¶
>>> lst = ["a", "abc", "aa", "abcd"] >>> patterns = ["^ab"] # sort list with elements starting with 'ab' first >>> list(sorted(lst, key=get_key_fn(patterns))) ... ["abc", "abcd", "a", "aa"]
- pythonwrench.re.match_patterns(x: str, include: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern] | None = '.*', *, exclude: str | ~re.Pattern | ~typing.Iterable[str | ~re.Pattern] | None = (), match_fn: ~typing.Callable[[str | ~re.Pattern, str], ~typing.Any] = <function search>) bool[source]¶
Returns True if the first argument match at least 1 included pattern and does not match any excluded pattern.
- Args:
x: String to check. include: Acceptable pattern(s) for x. If None, match all patterns with ‘.*’. defaults to ‘.*’. exclude: Forbidden pattern(s) for x. If None, match no patterns with value (). defaults to (). match_fn: Match function use to compare a pattern with argument x. defaults to re.search.