dissec.patterns – Pattern definitions

class dissec.patterns.Pattern(*, prefix: str = '', pairs: Sequence[tuple[BasicKey | SkipKey | AppendKey | FieldNameKey | FieldValueKey, str]] = ())

Dissect pattern.

For more information, see Dissect patterns.

static check_name_value_keys(keys: Iterable[BasicKey | SkipKey | AppendKey | FieldNameKey | FieldValueKey], /) None

Check that name/value keys are defined correctly in an iterable.

This method checks that there is exactly one field name for every field value, and exactly one field value for every field name.

Raises:

ValueError – The constraint is not respected.

static determine_append_key_lengths_and_orders(keys: Iterable[BasicKey | SkipKey | AppendKey | FieldNameKey | FieldValueKey], /) tuple[Mapping[str, int], Mapping[BasicKey | SkipKey | AppendKey | FieldNameKey | FieldValueKey, int]]

Determine the append keys, and orders in such keys.

Note

As long as a key name has at least one append key attached to it, basic keys with the same key name will actually also be append keys, so we actually base ourselves on the names for both append and basic keys.

Note

The order is just a general idea of the order, and is not unique. Basic keys or append keys with no explicit order are considered to have order -1 (which cannot be specified using the append with order specifier).

Returns:

A tuple presenting key name to length mapping, and key to order mapping.

classmethod parse_key(raw: str, /, *, runk: Runk | None = None) Key

Parse a key for a dissect pattern.

Parameters:
  • raw – Raw dissect key.

  • runk – Runk instance.

Returns:

Dissect key.

classmethod parse(raw: str, /, *, runk: Runk | None = None) PatternT

Parse a pattern.

Parameters:
  • raw – Textual form of the pattern to parse.

  • runk – Runk instance to start from.

Returns:

Pattern.

property prefix: str

Prefix, i.e. chunk of text that must be ignored at the start.

property pairs: Sequence[tuple[BasicKey | SkipKey | AppendKey | FieldNameKey | FieldValueKey, str]]

Key / delimiter pairs to use to parse the string.

property pattern: Pattern

Pattern.

dissect(raw: str, /, *, append_separator: str = '') dict[str, str]

Use the pattern to dissect a string.

Parameters:
  • raw – Raw string to dissect.

  • append_separator – Separator to use with append fields.

Returns:

Extracted data.

Raises:

ValueError – Raw string dissection was not possible.