Parser
Parser¶
- class Parser[source]¶
Bases:
abc.ABC
,Generic
[configpile.parsers._Value
]Describes a parser, that takes a string and returns either a value or an error
Methods
Returns a parser, that returns a sequence of a single value on success
Returns, when relevant, a set of inputs whose parse results cover all possible values
Returns a new parser where the empty string means None
Maps successful results of the parser through the given function that may fail
Creates a parser whose values are chosen from a set of strings
Creates a parser from a function that returns a value or an error
Creates a parser from a function that raises exceptions on parse errors
Creates a parser whose strings correspond to keys in a dictionary
Creates a parser from a parsy parser
Maps successful results of the parser through the given function
Parses a string into a result
Returns a new parser that parses values separated by a string
Returns a parser that verifies a predicate after successful parse
List of members of Parser
- abstract parse(arg)[source]¶
Parses a string into a result
This method reports parsing errors using a result type instead of raising exceptions.
Example
>>> user_input = "invalid" >>> float_parser.parse(user_input) Err1(msg="...", contexts=[]) >>> user_input = 2.0 >>> float_parser.parse(user_input) 2.0
- choices()[source]¶
Returns, when relevant, a set of inputs whose parse results cover all possible values
The definition above is written carefully: parsers may parse aliases, or normalize input when parsing (by stripping whitespace or normalizing case).
This method returns a set of strings, which does not need to be minimal, so that their parsed values cover the set of possible result values.
- as_sequence_of_one()[source]¶
Returns a parser, that returns a sequence of a single value on success
- separated_by(sep, strip=True, remove_empty=True)[source]¶
Returns a new parser that parses values separated by a string
- validated(predicate, msg)[source]¶
Returns a parser that verifies a predicate after successful parse
- static from_function_that_raises(f, *catch)[source]¶
Creates a parser from a function that raises exceptions on parse errors
- static from_choices(values, strip=True, force_case=ForceCase.NO_CHANGE)[source]¶
Creates a parser whose values are chosen from a set of strings
- static from_mapping(mapping, strip=True, force_case=ForceCase.NO_CHANGE, aliases=None)[source]¶
Creates a parser whose strings correspond to keys in a dictionary
- Parameters
mapping (
Mapping
[str
,TypeVar
(_Value
)]) – Dictionary mapping strings to valuesstrip (
bool
) – Whether to strip whitespace before looking for keysforce_case (
ForceCase
) – Whether to normalize the case of the user stringaliases (
Optional
[Mapping
[str
,TypeVar
(_Value
)]]) – Additional mappings not shown in help
- Return type
- Returns
Parameter type
- static __new__(cls, *args, **kwds)¶