Types and iterators

FindallIter

class formatparse.FindallIter(*args, **kwargs)

Stub for FindallIter (extension not built).

__init__(*args, **kwargs)

Accept construction like the real Rust-backed type (docs build stub).

ComposedType

class formatparse.ComposedType(parser)[source]

Wrap a compiled FormatParser for use as one extra_types converter.

Instances expose pattern and regex_group_count for the parent’s regex builder (GitHub issue #7). Prefer constructing via composed_type().

Pickling a parent FormatParser still does not restore extra_types; rebuild composed mappings after unpickling.

__init__(parser)[source]
pattern
regex_group_count

composed_type

formatparse.composed_type(parser)[source]

Wrap a compiled parser for embedding in another pattern’s extra_types.

The parent pattern refers to a custom type name; the value is this wrapper, which delegates parsing of the captured substring to parser.

Example:

>>> from formatparse import compile, composed_type
>>> ts = compile("{year:d}-{month:02d}-{day:02d}")
>>> log = compile(
...     "{ts:Timestamp} [{level}] {msg}",
...     extra_types={"Timestamp": composed_type(ts)},
... )
>>> r = log.parse("2024-01-15 [ERROR] oops")
>>> r.named["level"]
'ERROR'
>>> r.named["msg"]
'oops'
>>> inner = r.named["ts"]
>>> inner.named["year"], inner.named["month"], inner.named["day"]
(2024, 1, 15)
Parameters:

parser (FormatParser) – Child parser produced by compile().

Return type:

ComposedType

Returns:

Callable with pattern / regex_group_count set for composition.

Note

Pattern +, inheritance, and flattening nested fields into the parent result are not implemented yet (see issue #7).

ValidatedParser

class formatparse.ValidatedParser(parser)[source]

Thin wrapper around FormatParser with optional validators / pipeline on parse().

Also provides parse_with_validation() for the compile-once + pipeline case.

Other attributes and methods are forwarded to the inner parser (e.g. search, pattern). Use when you compile once and want the same validation ergonomics as parse() keyword arguments.

__init__(parser)[source]
parse(string, case_sensitive=False, extra_types=None, evaluate_result=True, *, validators=None, pipeline=None, validation_mode='strict')[source]

Parse string with optional validators or pipeline (same rules as parse()).

Parameters:

validation_mode (Literal['strict', 'collect', 'lenient']) – "strict", "collect", or "lenient" (see parse()).

Return type:

Optional[ParseResult]

parse_with_validation(string, pipeline, *, extra_types=None, case_sensitive=False, evaluate_result=True, validation_mode='strict')[source]

Parse string with the inner parser, then pipeline (see parse_with_validation()).

Return type:

Optional[ParseResult]