Class FormatterList

New in version 0.6.6.

class ooodev.formatters.formatter_list.FormatterList(format, idx_rule=OnlyIgnoreKind.IGNORE, idxs=<factory>, custom_formats=<factory>)[source]

FormatterList class is used to apply formatting to values based on specified formats and rules. .. attribute:: format

Format option such as .2f. Multiple formats can be added such as (".2f", "<10"). Formats are applied in the order they are added. In this case, first float is formatted as a string with two decimal places, and then the value is padded to the right with spaces.

type:

str | Tuple[str, …]

Parameters:
idx_rule

Determines what indexes are affected.

Type:

OnlyIgnoreKind

idxs

Indexes to apply formatting to or ignore, depending on index_rule.

Type:

Tuple[int, …]

custom_formats

Custom formats to apply based on specific indexes.

Type:

List[FormatListItem]

__post_init__() None[source]

Validates that all indexes in idxs are of type int.

_custom_format(current_index

int) -> FormatListItem | None: Retrieves a custom format for the given index if it exists.

apply_format(val

Any, current_index: int = -1) -> str: Applies formatting to val if format has been set.

val (Any): Any value. current_index (int, optional): When value is equal or greater than 0, formatting is applied following rules for indexes. str: Formatted string.

__init__(format, idx_rule=OnlyIgnoreKind.IGNORE, idxs=<factory>, custom_formats=<factory>)
Parameters:
Return type:

None

apply_format(val, current_index=-1)[source]

Applies formatting to val if format has been set

Parameters:
  • val (Any) – Any value

  • current_index (int, optional) – When value is equal or greater then 0 then formatting is applied following rules for indexes.

Returns:

Formatted string

Return type:

str

custom_formats: List[FormatListItem]
format: str | Tuple[str, ...]

Format option such as .2f

Multiple formats can be added such as (".2f", "<10"). Formats are applied in the order they are added. In this case first float is formatted as string with two decimal places, and then value is padded to the right with spaces.

idx_rule: OnlyIgnoreKind = 2

Determines what indexes are affected.

idxs: Tuple[int, ...]

Indexes to apply formatting to or ignore, depending on index_rule.