Formatter¶
- class dictIO.formatter.Formatter¶
Bases:
object
Abstract Base Class for formatters.
Formatters serialize a dict into a string applying a specific format.
- __init__() None ¶
Methods
__init__
()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 an expression.
format_float
(arg)Format a floating point number.
format_int
(arg)Format an integer.
format_key
(arg)Format a key.
Format a multi word string.
Format None value.
Format a reference.
Format a single word string.
format_string
(arg)Format a string.
Format a string that contains a nested string.
Format a single value.
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.
- add_double_quotes(arg: str) str ¶
Add double quotes to a string.
Leading and trailing double quotes will added to the passed in string (i.e. it will be wrapped in double quotes). Note: Call this base class method from any specific Formatter implementation to easily add double quotes to a string when formatting.
- Parameters:
arg (str) – the string to be wrapped in double quotes
- Returns:
the string wrapped in double quotes
- Return type:
str
- add_single_quotes(arg: str) str ¶
Add single quotes to a string.
Leading and trailing single quotes will added to the passed in string (i.e. it will be wrapped in single quotes). Note: Call this base class method from any specific Formatter implementation to easily add single quotes to a string when formatting.
- Parameters:
arg (str) – the string to be wrapped in single quotes
- Returns:
the string wrapped in single quotes
- Return type:
str
- format_bool(arg: bool) str ¶
Format a boolean.
Note: Override this method for specific formatting of booleans when implementing a Formatter.
- Parameters:
arg (bool) – the boolean value to be formatted
- Returns:
the formatted string representation of the passed in boolean value
- Return type:
str
- format_empty_string(arg: str) str ¶
Format an empty string.
Note: Override this method for specific formatting of empty strings when implementing a Formatter.
- 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.
Note: Override this method for specific formatting of expressions when implementing a Formatter.
- Parameters:
arg (str) – the expression to be formatted
- Returns:
the formatted expression
- Return type:
str
- format_float(arg: float) str ¶
Format a floating point number.
Note: Override this method for specific formatting of floating point numbers when implementing a Formatter.
- Parameters:
arg (float) – the float to be formatted
- Returns:
the formatted string representation of the passed in float
- Return type:
str
- format_int(arg: int) str ¶
Format an integer.
Note: Override this method for specific formatting of integers when implementing a Formatter.
- Parameters:
arg (int) – the int to be formatted
- Returns:
the formatted string representation of the passed in int
- Return type:
str
- format_key(arg: Hashable) str ¶
Format a key.
Formats a key of type TKey = str | int
- Parameters:
arg (TKey) – the key to be formatted
- Returns:
the formatted string representation of the passed in key
- Return type:
str
- format_multi_word_string(arg: str) str ¶
Format a multi word string.
Note: Override this method for specific formatting of multi word strings when implementing a Formatter.
- Parameters:
arg (str) – the multi word string to be formatted
- Returns:
the formatted multi word string
- Return type:
str
- format_none() str ¶
Format None value.
Note: Override this method for specific formatting of None when implementing a Formatter.
- Returns:
the formatted string representation of None
- Return type:
str
- format_reference_string(arg: str) str ¶
Format a reference.
Note: Override this method for specific formatting of references when implementing a Formatter.
- Parameters:
arg (str) – the reference to be formatted
- Returns:
the formatted reference
- Return type:
str
- format_single_word_string(arg: str) str ¶
Format a single word string.
Note: Override this method for specific formatting of single word strings when implementing a Formatter.
- Parameters:
arg (str) – the single word string to be formatted
- Returns:
the formatted single word string
- Return type:
str
- format_string(arg: str) str ¶
Format a string.
- Parameters:
arg (str) – the string to be formatted
- Returns:
the formatted string
- Return type:
str
- format_string_with_nested_string(arg: str) str ¶
Format a string that contains a nested string.
Note: Override this method for specific formatting of strings with nested strings when implementing a Formatter.
- Parameters:
arg (str) – the string with a nested string to be formatted
- Returns:
the formatted string with a nested string
- Return type:
str
- format_value(arg: str | int | float | bool | None) str ¶
- format_value(arg: Any) str
Format a single value.
Formats a single value of type TSingleValue = str | int | float | bool | Path | None
- Parameters:
arg (V) – the value to be formatted
- Returns:
the formatted string representation of the passed in value, if value is of TSingleValue type = str | int | float | bool | Path | None. For all other types, the method will, as fallback, call and return str(arg). It is hence guaranteed that a string gets returned.
- Return type:
str
- format_values(arg: M) M ¶
- format_values(arg: S) S
Format multiple values.
Formats all values inside a dict or list. The function traverses the passed in dict or list recursively so that all values in also nested dicts and lists get formatted. A copy of the passed in dict or list is returned with all values formatted. (The original dict or list is not modified.)
- Parameters:
arg (MutableMapping[K, V] | MutableSequence[V]) – the dict or list containing the values to be formatted.
- Returns:
a copy of the passed in dict or list, with all values formatted.
- Return type:
MutableMapping[K, V] | MutableSequence[V]
- 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, optional) – name of the target file to be formatted, by default None
- Returns:
specific Formatter instance matching the target file type to be formatted
- Return type:
- to_string(arg: MutableMapping[K, V]) str ¶
Create a string representation of the passed in dict.
Note: Override this method when implementing a specific Formatter.
- Parameters:
arg (Union[MutableMapping[K, V]]) – dict to be formatted
- Returns:
string representation of the dict
- Return type:
str