cosapp.utils.parsing

Functions

find_selector(expression)

Decompose a string expression into basename and selector, where selector is a suitable mask expression for an array.

multi_join(expressions, separators[, add_space])

Reciprocal of multi_split.

multi_split(expression, separators)

Extension of str.split, accounting for more than one split separators.

cosapp.utils.parsing.find_selector(expression: str) tuple[str, str][source]

Decompose a string expression into basename and selector, where selector is a suitable mask expression for an array.

Parameters:

expression [str]: the expression to be parsed.

Returns:

baseline, selector [str, str]

cosapp.utils.parsing.multi_join(expressions: list[str], separators: list[str], add_space=False) str[source]

Reciprocal of multi_split.

Parameters:
  • [list[str]] (- separators) – Expression to be split.

  • [list[str]] – list of separators.

  • [bool (- add_space) – If True, a space character is inserted before and after each separator. Defaults to False.

  • optional] – If True, a space character is inserted before and after each separator. Defaults to False.

Returns:

expression [str]

Return type:

Joined expression.

Examples

>>> multi_join(['a', 'b', 'c', 'd', 'e'], ['+', '-', '-', '*'])
'a+b-c-d*e'
>>> multi_join(['a', 'b', 'c', 'd', 'e'], ['+', '-', '-', '*'], add_space=True)
'a + b - c - d * e'
cosapp.utils.parsing.multi_split(expression: str, separators: Collection[str]) tuple[list[str], list[str]][source]

Extension of str.split, accounting for more than one split separators.

Parameters:
  • [str] (- expression) – Expression to be split.

  • [collection[str]] (- separators) – list/tuple/set of separators.

Returns:

  • - expressions [list[str]] – list of n split expressions.

  • - separators [list[str]] – Sequence of (n - 1) separators between split expressions.

Examples

>>> multi_split('a+b-c-d*e', list('+-'))
['a', 'b', 'c', 'd*e'], ['+', '-', '-']
>>> multi_split('a+b-c-d*e', list('*+-'))
['a', 'b', 'c', 'd', 'e'], ['+', '-', '-'; '*']