Types and iterators
FindallIter
ComposedType
- class formatparse.ComposedType(parser)[source]
Wrap a compiled
FormatParserfor use as oneextra_typesconverter.Instances expose
patternandregex_group_countfor the parent’s regex builder (GitHub issue #7). Prefer constructing viacomposed_type().Pickling a parent
FormatParserstill does not restoreextra_types; rebuild composed mappings after unpickling.- 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 bycompile().- Return type:
- Returns:
Callable with
pattern/regex_group_countset 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
FormatParserwith optionalvalidators/pipelineonparse().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 asparse()keyword arguments.- parse(string, case_sensitive=False, extra_types=None, evaluate_result=True, *, validators=None, pipeline=None, validation_mode='strict')[source]
Parse
stringwith optionalvalidatorsorpipeline(same rules asparse()).
- parse_with_validation(string, pipeline, *, extra_types=None, case_sensitive=False, evaluate_result=True, validation_mode='strict')[source]
Parse
stringwith the inner parser, thenpipeline(seeparse_with_validation()).- Return type: