Formatter

class dictIO.formatter.Formatter

Bases: object

Abstract Base Class for formatters.

Formatters serialize a dict into a string applying a specific format.

counter

Shared placeholder counter used during formatting.

__init__() → None

Initialize a formatter with a shared placeholder counter.

Return type:

None

Methods

__init__()

Initialize a formatter with a shared placeholder counter.

add_double_quotes(arg)

Add double quotes to a string.

add_single_quotes(arg)

Add single quotes to a string.

format_bool(arg)

Format a boolean.

format_empty_string(arg)

Format an empty string.

format_expression_string(arg)

Format an expression.

format_float(arg)

Format a floating point number.

format_int(arg)

Format an integer.

format_key(arg)

Format a key.

format_multi_word_string(arg)

Format a multi word string.

format_none()

Format None value.

format_reference_string(arg)

Format a reference.

format_single_word_string(arg)

Format a single word string.

format_string(arg)

Format a string.

format_string_with_nested_string(arg)

Format a string that contains a nested string.

format_value(arg)

Format a single value.

format_values(-> M)

Format multiple values.

get_formatter([target_file])

Return a Formatter instance matching the type of the target file to be formatted (factory method).

to_string(arg)

Create a string representation of the passed in dict.

classmethod get_formatter(target_file: Path | None = None) → Formatter

Return a Formatter instance matching the type of the target file to be formatted (factory method).

Parameters:

target_file (Path | None) – Target path used to choose a format-specific formatter. If None, return the native formatter. Defaults to None.

Returns:

Formatter selected for the target file’s suffix.

Return type:

Formatter

add_double_quotes(arg: str) → str

Add double quotes to a string.

This helper is available to subclasses when quoting formatted values.

Parameters:

arg (str) – String to wrap in double quotes.

Returns:

String wrapped in double quotes.

Return type:

str

add_single_quotes(arg: str) → str

Add single quotes to a string.

This helper is available to subclasses when quoting formatted values.

Parameters:

arg (str) – String to wrap in single quotes.

Returns:

String wrapped in single quotes.

Return type:

str

format_bool(arg: bool) → str

Format a boolean.

Override this method for format-specific boolean representations.

Parameters:

arg (bool) – Boolean value to format.

Returns:

Formatted boolean as a string.

Return type:

str

format_empty_string(arg: str) → str

Format an empty string.

Override this method for a format-specific representation of an empty string.

Parameters:

arg (str) – Empty string to format.

Returns:

Formatted empty string.

Return type:

str

format_expression_string(arg: str) → str

Format an expression.

Override this method for format-specific expression handling.

Parameters:

arg (str) – Expression string to format.

Returns:

Formatted expression.

Return type:

str

format_float(arg: float) → str

Format a floating point number.

Override this method for format-specific floating-point representations.

Parameters:

arg (float) – Floating-point value to format.

Returns:

Formatted value as a string.

Return type:

str

format_int(arg: int) → str

Format an integer.

Override this method for format-specific integer representations.

Parameters:

arg (int) – Integer to format.

Returns:

Formatted integer as a string.

Return type:

str

format_key(arg: TKey) → str

Format a key.

Parameters:

arg (TKey) – Key to format.

Returns:

Formatted key as a string.

Return type:

str

format_multi_word_string(arg: str) → str

Format a multi word string.

Override this method for a format-specific representation of a multi-word string.

Parameters:

arg (str) – Multi-word string to format.

Returns:

Formatted string.

Return type:

str

format_none() → str

Format None value.

Override this method for a format-specific representation of None.

Returns:

Formatted None value as a string.

Return type:

str

format_reference_string(arg: str) → str

Format a reference.

Override this method for format-specific reference handling.

Parameters:

arg (str) – Reference string to format.

Returns:

Formatted reference.

Return type:

str

format_single_word_string(arg: str) → str

Format a single word string.

Override this method for a format-specific representation of a single-word string.

Parameters:

arg (str) – Single-word string to format.

Returns:

Formatted string.

Return type:

str

format_string(arg: str) → str

Format a string.

Parameters:

arg (str) – String to format.

Returns:

Formatted string.

Return type:

str

format_string_with_nested_string(arg: str) → str

Format a string that contains a nested string.

Override this method for a format-specific representation of a string containing nested quotes.

Parameters:

arg (str) – String containing nested quotes.

Returns:

Formatted string.

Return type:

str

format_value(arg: object) → str

Format a single value.

Format strings, booleans, numbers, paths, and None with their format-specific handlers. Other values are converted with str.

Parameters:

arg (object) – Value to format.

Returns:

Formatted value as a string.

Return type:

str

format_values(arg: M) → M
format_values(arg: S) → S

Format multiple values.

Traverse nested mappings and sequences recursively. The input object is not modified.

Parameters:

arg (M | S) – Mapping or sequence whose values should be formatted.

Returns:

Shallow copy of the input container with formatted values.

Return type:

M | S

to_string(arg: MutableMapping[K, V]) → str

Create a string representation of the passed in dict.

Override this method in a format-specific formatter.

Parameters:

arg (MutableMapping[K, V]) – Mapping to format.

Returns:

Formatted mapping as a string.

Return type:

str