Light parentheses parser in Python. pip install parentheses
ParenthesesSymbol = str
ParseFlag = int
PARENTHESES_ALL: ParseFlag = 1
PARENTHESES_OPEN: ParseFlag = 2
PARENTHESES_CLOSE: ParseFlag = 3
PARENTHESES_ROUND_OPEN: ParenthesesSymbol = '('
PARENTHESES_ROUND_CLOSE: ParenthesesSymbol = ')'
PARENTHESES_SQUARE_OPEN: ParenthesesSymbol = '['
PARENTHESES_SQUARE_CLOSE: ParenthesesSymbol = ']'
PARENTHESES_CURLY_OPEN: ParenthesesSymbol = '{'
PARENTHESES_CURLY_CLOSE: ParenthesesSymbol = '}'
PARENTHESES_DOUBLE_QUOTE: ParenthesesSymbol = '"'
PARENTHESES_SINGLE_QUOTE: ParenthesesSymbol = '\''
PARENTHESES_OPEN_SYMBOLS: list[ParenthesesSymbol] = [
PARENTHESES_ROUND_OPEN,
PARENTHESES_SQUARE_OPEN,
PARENTHESES_CURLY_OPEN
]
PARENTHESES_CLOSE_SYMBOLS: list[ParenthesesSymbol] = [
PARENTHESES_ROUND_CLOSE,
PARENTHESES_SQUARE_CLOSE,
PARENTHESES_CURLY_CLOSE
]
PARENTHESES_SYMBOLS = PARENTHESES_OPEN_SYMBOLS + PARENTHESES_CLOSE_SYMBOLS
PARENTHESES_REGEX = r'\(.*?\)|\[.*?\]|\{.*?\}'
Get is parentheses in string valid. (Slower but gives more accurate result).
Examples:
parse('(x)').valid_proc()
=> True
parse('(x').valid_proc()
=> False
Get is parentheses in string valid. (Faster but gives less accurate result).
Examples:
parse('(x)').valid()
=> True
parse('(x').valid()
=> False
Get is quoted parentheses valid.
Examples:
parse('"x"').valid_quotes()
=> True
parse('"x\""').valid_quotes(True)
=> True
parse('"x').valid_quotes()
=> False
parse('"x\""').valid_quotes()
=> False
Count braces in string.
Examples:
parse('(x)').count()
=> 2
parse('(x)').count(PARENTHESES_OPEN)
=> 1
parse('(x)').count(PARENTHESES_CLOSE)
=> 1
Autoclose brackets.
Examples:
parse('{[(x').autoclose()
=> {[(x)]}
Get content in braces.
Examples:
parse('(x) [y] {z}').find()
=> ['(x)', '[y]', '{z}']
parse('(x) [y] {z}').find(True)
=> ['x', 'y', 'z']
Remove braces in string.
Examples:
parse('(x) [y] {z}').remove_braces()
=> 'x y z'
Remove everything in parentheses.
Examples:
parse('(x) [y] {z}').remove()
=> ' '
parse('(x) [y] {z}').remove(True)
=> '() [] {}'
Get string.
Examples:
parse('x').as_str()
=> 'x'
Parse string.
Examples:
parse('(x)')
=> PPString('x')
Add new parentheses symbols.
Examples:
new_parentheses_symbols('<', '>')
=> None
Remove parentheses symbols.
Examples:
remove_parentheses_symbols('<', '>')
=> None