FormatAbstract#

class FormatAbstract(fmt, params)#

Bases: object

Represent a format string.

Can generate an appropriate regular expression corresponding to that format string (to some limitations), generate a string from a value, or parse such a string into a value.

Users are not meant to instanciate those objects directly, use get_format() instead (or its alias for retro-compatibility Format()).

Parameters:
  • fmt (str) – Format string.

  • params (Mapping[str, Any]) – Mapping of options/parameters of the format mini-language to their values. (type, fill, align, sign, alternate, zero, width, grouping, precision). They should not contain None values.

add_outer_alignement(rgx)#

Add necessary regex for alignement characters.

If width is not specified, does nothing.

Parameters:

rgx (str)

Return type:

str

format(value)#

Return formatted string of a value.

Parameters:

value (Any)

Return type:

str

generate_expression(capture=False)#

Generate a regular expression matching strings created with this format.

Parameters:

capture – If true, add capturing groups that will be used to parse the value by selecting only relevant information. Default is false.

Return type:

str

get_fill_regex()#

Return regex for matching fill characters.

parse(s)#

Parse string generated with this format into an appropriate value.

Parameters:

s (str)

Return type:

Any