ParseData: An Internal Register for Manipulating Regex Matches#

class my.regex.meta.ParseData.ParseData(*, captures: dict[str, list[str]] = {}, starts: dict[str, list[int]] = {}, field: str = '', value: list[str] = [], start: list[int] = [])#

Private data structure for intermediate storage of match data during parsing.

Holds captured values and their start positions while parsers are being applied. Supports merging, rearranging, and transforming captures based on parser functions.

It is available for use via the public API, but should only really be useful if you’re looking to extend RegexStore’s parsing functionality. The vast majority of users should never need to know that this class exists!

I Helper Methods#

ParseData.interleave(src: str, dest: str, effects: list[tuple[int, str]]) → None#

Merge new captures into destination field, maintaining position order.

Optionally consumes values from a source field, moving them to the destination field while preserving the order of all captures by their start positions.

Parameters:
  • src – Source field to consume from (empty string to skip consumption).

  • dest – Destination field to add captures to.

  • effects – List of (start_position, value) tuples to add.

II Primary Methods#

ParseData.apply_dict_parser(parser: dict[str, str], rgx: Pattern) → None#

Apply a dictionary parser that remaps captured groups.

Re-matches each captured value with the pattern, then uses the parser dict to move captures from source fields to destination fields.

Parameters:
  • parser – Mapping from source field names to the destination fields that should receive their captures.

  • rgx – Pattern to re-match captured values with.

ParseData.apply_func_parser(parser: Callable[[str], dict[str, str] | str]) → None#

Apply a function parser to transform captured values.

Supports two types of parsers: - Functions returning dicts create new named captures from each value - Functions returning strings replace values in place

Parameters:

parser – Function transforming each captured string.

Raises:

TypeError – If one invocation returns a mapping and another returns a string.

Examples

String-returning parsers rewrite the active field’s values in place:

>>> from my import ParseData
>>> pd = ParseData(captures={'n': ['1', '2']}, starts={'n': [0, 2]})
>>> pd.set_field('n')
>>> pd.apply_func_parser(lambda v: str(int(v) * 2))
>>> pd.captures
{'n': ['2', '4']}

III Public Methods#

ParseData.items() → list[tuple[str, tuple[list[int], list[str]]]]#

Get all captured items as (field_name, (starts, values)) tuples.

ParseData.keys() → list[str]#

Get all captured field names.

ParseData.values() → list[tuple[list[int], list[str]]]#

Get all captured values as (starts, values) tuples.

ParseData.set_field(field: str) → None#

Set the active field for processing, extracting its data.

Parameters:

field – Name of field to make active.

Raises:

AssertionError – If field is not in captures.