NativeFormatter

class dictIO.formatter.NativeFormatter

Bases: Formatter

Formatter to serialize a dict into a string in dictIO native file format.

__init__() → None

Define default configuration for NativeFormatter.

Return type:

None

Methods

__init__()

Define default configuration for NativeFormatter.

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_dict(arg[, tab_len, level, sep, ...])

Serialize a mapping, sequence, or scalar value.

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.

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(arg)

Format multiple values.

get_formatter([target_file])

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

insert_block_comments(s_dict, s)

Replace block-comment placeholders with their original text.

insert_includes(s_dict, s)

Replace include placeholders with their source directives.

insert_line_comments(s_dict, s)

Replace line-comment placeholders with their original comments.

make_default_block_comment([block_comment])

Ensure a native-format header precedes the supplied block comment.

remove_trailing_spaces(s)

Remove trailing spaces from all lines.

to_string(arg)

Create a string representation of the passed in dict in dictIO native file format.

format_bool(arg: bool) → str

Format a boolean.

Parameters:

arg (bool) – the boolean value to be formatted

Returns:

the formatted string representation of the passed in boolean value

Return type:

str

format_dict(arg: object, tab_len: int = 4, level: int = 0, sep: str = ' ', items_per_line: int = 10, end: str = '\n', ancestry: type[object] = MutableMapping) → str

Serialize a mapping, sequence, or scalar value.

Parameters:
  • arg (object) – Value or nested container to serialize.

  • tab_len (int) – Number of indentation characters per level. Defaults to 4.

  • level (int) – Initial indentation level. Defaults to 0.

  • sep (str) – Character used between keys and values. Defaults to “ “.

  • items_per_line (int) – Maximum number of sequence items written on a line. Defaults to 10.

  • end (str) – Line ending appended to each serialized element. Defaults to the newline character.

  • ancestry (type[object]) – Container type that determines whether a sequence is serialized as a top-level value or as a mapping value. Defaults to MutableMapping.

Returns:

Serialized representation of the input.

Return type:

str

format_empty_string(arg: str) → str

Format an empty string.

Parameters:

arg (str) – the empty string to be formatted

Returns:

the formatted empty string

Return type:

str

format_expression_string(arg: str) → str

Format an expression.

Parameters:

arg (str) – the expression to be formatted

Returns:

the formatted expression

Return type:

str

format_multi_word_string(arg: str) → str

Format a multi word string.

Parameters:

arg (str) – the multi word string to be formatted

Returns:

the formatted multi word string

Return type:

str

format_none() → str

Format None.

Returns:

the formatted string representation of None

Return type:

str

format_string_with_nested_string(arg: str) → str

Format a string that contains a nested string.

Parameters:

arg (str) – the string with a nested string to be formatted

Returns:

the formatted string with a nested string

Raises:

ValueError – If the input does not contain a nested quote.

Return type:

str

insert_block_comments(s_dict: SDict[K, V], s: str) → str

Replace block-comment placeholders with their original text.

The first inserted block comment receives the default dict header when it does not already contain a C++ marker. If no comment placeholder is present, insert the default header at the start of the output.

Parameters:
  • s_dict (SDict[K, V]) – Dictionary containing block-comment text.

  • s (str) – Formatted text containing block-comment placeholders.

Returns:

Text with block-comment placeholders replaced.

Return type:

str

insert_includes(s_dict: SDict[K, V], s: str) → str

Replace include placeholders with their source directives.

Parameters:
  • s_dict (SDict[K, V]) – Dictionary containing include metadata.

  • s (str) – Formatted text containing include placeholders.

Returns:

Text with include placeholders replaced.

Return type:

str

insert_line_comments(s_dict: SDict[K, V], s: str) → str

Replace line-comment placeholders with their original comments.

Parameters:
  • s_dict (SDict[K, V]) – Dictionary containing line comments.

  • s (str) – Formatted text containing line-comment placeholders.

Returns:

Text with line-comment placeholders replaced.

Return type:

str

make_default_block_comment(block_comment: str = '') → str

Ensure a native-format header precedes the supplied block comment.

Parameters:

block_comment (str) – Existing block comment. If it contains a C++ marker, leave it unchanged. Defaults to “”.

Returns:

Block comment with the native-format header prepended when needed.

Return type:

str

remove_trailing_spaces(s: str) → str

Remove trailing spaces from all lines.

Parameters:

s (str) – Text whose lines should be stripped of trailing whitespace.

Returns:

Text with trailing spaces removed and line endings preserved.

Return type:

str

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

Create a string representation of the passed in dict in dictIO native file format.

Parameters:

arg (MutableMapping[K, V]) – dict to be formatted

Returns:

string representation of the dict in dictIO native file format

Return type:

str