string_filter¶
String filtering module.
Main idea is to provide a common interface for string filtering, that can make possible working with filters like with objects rather than with functions/lambdas.
- pytermor.util.string_filter.apply_filters(string: AnyStr, *args: StringFilter[AnyStr] | Type[StringFilter[AnyStr]]) AnyStr¶
Method for applying dynamic filter list to a target str/bytes. Example (will replace all \x1b control characters to E and make SGR params visible):
>>> apply_filters(span.red('test'), ReplaceSGR(r'E\2\3\5'))'E[31mtestE[39m'Note that type of
stringargument must correspond to StringFilter’s types, i.e.ReplaceNonAsciiBytesis StringFilter[bytes] type, so you can apply it only to bytes-type strings.- Parameters
string (AnyStr) – String for filter application (str or bytes-type).
args – StringFilter instances or StringFilter types.
- Returns
String with applied filters.
- class pytermor.util.string_filter.StringFilter(repl: AnyStr)¶
Common string modifier interface.
- abstract apply(s: AnyStr) AnyStr¶
- class pytermor.util.string_filter.ReplaceSGR(repl: AnyStr = '')¶
Find all SGR seqs (e.g. ‘ESC[1;4m’) and replace with given string.
More specific version of
ReplaceCSI.- Parameters
repl – Replacement, can contain regexp groups (see apply_filters).
- apply(s: str) str¶
- class pytermor.util.string_filter.ReplaceCSI(repl: AnyStr = '')¶
Find all CSI seqs (i.e. ‘ESC[*’) and replace with given string.
Less specific version of
ReplaceSGR, as CSI consists of SGR and many other sequence subtypes.- Parameters
repl – Replacement, can contain regexp groups (see apply_filters).
- apply(s: str) str¶