polymerist.genutils.textual.delimiters

Validation and parsing of text enclosed by opening and closing delimiters (i.e. parentheses-like behavior)

Attributes

COMMON_DELIMITERS

validate_braces

Classes

DelimiterBalanceState

For indication the error state in a delimiter character pair closure check

Functions

check_balanced_delimiters(→ tuple[bool, ...)

Tests if parenthesis-like start and end delimiters within a string are balanced

parse_within_delimiters(→ Generator[str, None, None])

Generates substring contained by starting and ending delimiting characters (and depth of substring) via pushdown automaton mechanism

delimit_substrs_from_regex(→ str)

Place delimiters around any substrings of "string" which match the provided regular expression

delimit_substrs_from_choices(→ str)

Place delimiters around any substrings of "string" which occur within "choices"

validate_common_delimiters(→ None)

For checking whether all braces-like delimiters (i.e. parenthesis, brackets, etc.) are fully opened and not closed early

Module Contents

class polymerist.genutils.textual.delimiters.DelimiterBalanceState[source]

Bases: enum.StrEnum

For indication the error state in a delimiter character pair closure check

BALANCED = 'balanced'
PREMATURE_CLOSURE = 'premature closure'
UNCLOSED_OPENING = 'unclosed opening'
polymerist.genutils.textual.delimiters.check_balanced_delimiters(string: str, start_char: str, end_char: str) tuple[bool, DelimiterBalanceState, int][source]

Tests if parenthesis-like start and end delimiters within a string are balanced (i.e. there are just as many closures as opening, and no closure occurs before and opening)

Return whether the string is balanced, and the index where an imbalance has occurred (-1 if no imbalance or at end)

polymerist.genutils.textual.delimiters.parse_within_delimiters(string: str, start_char: str, end_char: str) Generator[str, None, None][source]

Generates substring contained by starting and ending delimiting characters (and depth of substring) via pushdown automaton mechanism

polymerist.genutils.textual.delimiters.delimit_substrs_from_regex(string: str, regex: str | re.Pattern, start_char: str, end_char: str) str[source]

Place delimiters around any substrings of “string” which match the provided regular expression

polymerist.genutils.textual.delimiters.delimit_substrs_from_choices(string: str, choices: Iterable[str], start_char: str, end_char: str) str[source]

Place delimiters around any substrings of “string” which occur within “choices”

polymerist.genutils.textual.delimiters.COMMON_DELIMITERS
polymerist.genutils.textual.delimiters.validate_common_delimiters(string: str, pointer_char: str = '^') None[source]

For checking whether all braces-like delimiters (i.e. parenthesis, brackets, etc.) are fully opened and not closed early If an invalid delimiters are found, a ValueError is raised with a helpful error message that points to the location of the fault (and the reason)

polymerist.genutils.textual.delimiters.validate_braces