id_translation.offline.parse_format_string#
Utility module for parsing raw Format input strings.
Functions
|
Split a format string into elements. |
Classes
|
Information about a single block in a |
|
Output type of |
Exceptions
|
Errors raised due to mismatched delimiters. |
Error raised for improper optional blocks. |
- exception BadDelimiterError(format_string, open_idx, idx)[source]#
Bases:
MalformedOptionalBlockErrorErrors raised due to mismatched delimiters.
- class Element(part, placeholders, required, positional_part)[source]#
Bases:
objectInformation about a single block in a
Formatspecification.- classmethod make(fmt, in_optional_block)[source]#
Create an
Elementfrom an input string s.- Parameters:
fmt – Input data.
in_optional_block – Flag indicating whether s was found inside an optional block.
- Returns:
A new
Element.
- classmethod parse_block(block, defaults=None)[source]#
Parse an entire block with optional defaults for placeholders found.
Hint
With defaults, the value of the
ParseBlockResult.parsed_blockis more or less what you’d expect if the built-instr.format_map()-method allowed missing keys.Anonymous fields are not permitted.
- Output with
defaults == None: When defaults are
None, placeholder names are stripped from theparsed_block.>>> block, placeholders = Element.parse_block("{id!s:.8}:{name!r}") >>> print(f"{block=} has {placeholders=}") block='{!s:.8}:{!r}' has placeholders=['id', 'name']
Field names in block are returned as
ParseBlockResult.placeholders, in the order in which they appeared in the input block. The field names ofparsed_blockwill be anonymous.>>> block.format(UUID(int=10**38), "Morran Borran") "4b3b4ca8:'Morran Borran'"
- Output with
defaults != None: When defaults are given, all placeholders in the block which are present in the defaults are replaced with
defaults[field_name]in theparsed_block.>>> block, placeholders = Element.parse_block( ... "{id!s:.8}:{name!r}", ... defaults={"name": "Morran Borran"}, ... ) >>> print(f"{block=} has {placeholders=}") block="{id!s:.8}:'Morran Borran'" has placeholders=['id']
Field names without defaults will be present both in
placeholders, and as named fields in theparsed_block.>>> block.format(id=UUID(int=10**38)) "4b3b4ca8:'Morran Borran'"
- Parameters:
block – A block to parse.
defaults – A dict
{placeholder: value}.
- Returns:
A
ParseBlockResulttuple.- Raises:
ValueError – If block contains anonymous fields.
- Output with
- part#
String literal.
- placeholders#
Placeholder names in part, if any.
- positional_part#
Return a positional version of the part attribute.
- required#
Flag indicating whether the element may be excluded.
- exception MalformedOptionalBlockError[source]#
Bases:
ValueErrorError raised for improper optional blocks.
- class ParseBlockResult(parsed_block, placeholders)[source]#
Bases:
NamedTupleOutput type of
Element.parse_block().- parsed_block#
Processed parts of the block.
- placeholders#
Names of the
Format.placeholdersin parsed_block, in the order in which they appear.
- get_elements(fmt)[source]#
Split a format string into elements.
- Parameters:
fmt – User input string.
- Returns:
A list of parsed elements.
- Raises:
BadDelimiterError – For unbalanced optional block delimitation characters.