filefinder.format#

Generate regex from string format, and parse strings.

Parameters of the format-string are retrieved. See Format Mini Language Specification.

Thoses parameters are then used to generate a regular expression, or to parse a string formed from the format.

Only ‘s’, ‘d’, ‘f’, ‘e’ and ‘E’ formats types are supported.

The width of the format string is not respected when matching with a regular expression.

Module Attributes

FORMAT_REGEX

The regular expression used to parse a format string.

Functions

Format(format)

Parse format parameters and return appropriate Format object.

get_format(format)

Parse format parameters and return appropriate Format object.

Classes

FormatAbstract(fmt, params)

Represent a format string.

FormatFloat(*args, **kwargs)

Represent a format string for floats (type f, e, E).

FormatInteger(*args, **kwargs)

Represent a format string for integers (type d).

FormatNumberAbstract(*args, **kwargs)

Represent a format string for numbers (type d, f, e, E).

FormatString(*args, **kwargs)

Represent a format string for strings (type s).

Exceptions

DangerousFormatError

Dangerous format-string leading to ambiguities.

FormatError

Error related to Format object.

FormatParsingError

Could not parse a format-string.

FormatValueParsingError

Could not parse value.

InvalidFormatTypeError

Unsupported type of format-string.

exception DangerousFormatError#

Dangerous format-string leading to ambiguities.

exception FormatError#

Error related to Format object.

exception FormatParsingError#

Could not parse a format-string.

exception FormatValueParsingError#

Could not parse value.

exception InvalidFormatTypeError#

Unsupported type of format-string.

class FormatAbstract(fmt, params)#

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

class FormatFloat(*args, **kwargs)#

Represent a format string for floats (type f, e, E).

generate_expression(capture=False)#

Generate a regular expression matching strings created with this format.

Return type:

str

get_left_of_decimal()#

Get regex for the numbers left of decimal point.

Will deal with grouping if present. Some simplifications for eE formats. Only use groupings for ‘0=’ alignment and enforce single digits grouping.

Return type:

str

get_right_of_decimal()#

Return regex for numbers after decimal points.

Including the decimal point itself. It will respect ‘alternate’ option and the specified precision.

Return type:

str

parse(s)#

Parse string generated with format.

Parameters:

s (str) –

Return type:

float

class FormatInteger(*args, **kwargs)#

Represent a format string for integers (type d).

generate_expression(capture=False)#

Generate regex from format string.

Return type:

str

parse(s)#

Parse string generated with format.

Parameters:

s (str) –

Return type:

int

class FormatNumberAbstract(*args, **kwargs)#

Represent a format string for numbers (type d, f, e, E).

get_left_of_decimal()#

Get regex for the numbers left of decimal point.

Will deal with grouping if present.

Return type:

str

get_sign_regex(capture=False)#

Get sign regex with approprite zero padding.

Return type:

str

prepare_parse(s)#

Remove special characters.

Remove characters that throw off int() and float() parsing: fill/alignment characters and grouping symbols.

Returns:

s – a string ready to be casted to the appropriate type.

Parameters:

s (str) –

Return type:

str

class FormatString(*args, **kwargs)#

Represent a format string for strings (type s).

generate_expression(capture=False)#

Generate a regular expression matching strings created with this format.

Return type:

str

parse(s)#

Parse string generated with this format into an appropriate value.

Parameters:

s (str) –

Return type:

str

Format(format)#

Parse format parameters and return appropriate Format object.

Parameters:

format (str) –

Return type:

FormatAbstract

get_format(format)#

Parse format parameters and return appropriate Format object.

Parameters:

format (str) –

Return type:

FormatAbstract

FORMAT_REGEX = '((?P<fill>.)?(?P<align>[<>=^]))?(?P<sign>[-+ ])?(?P<z>z)?(?P<alternate>#)?(?P<zero>0)?(?P<width>\\d+?)?(?P<grouping>[,_])?(?P<precision>\\.\\d+?)?(?P<type>[a-zA-Z])'#

The regular expression used to parse a format string.

Follows the Format Specification Mini-Language. [[fill]align][sign]["z"]["#"]["0"][width][grouping_option]["." precision][type]