gemseo.utils.string_tools module#
Pretty string utils.
- class MessageLine(str_format, level, args, kwargs)[source]#
Bases:
NamedTuple
Store the raw ingredient of a string to be formatted later.
Create new instance of MessageLine(str_format, level, args, kwargs)
- class MultiLineString(lines=())[source]#
Bases:
object
Multi-line string lazy evaluator.
The creation of the string is postponed to when an instance is stringified through the __repr__ method. This is mainly used for logging complex strings or objects where the string evaluation cost may be avoided when the logging level dismisses a logging message.
A __add__ method is defined to allow the "+" operator between two instances, that implements the concatenation of two MultiLineString. If the other instance is not MultiLineString, it is first converted to string using its __str__ method and then added as a new line in the result.
- Parameters:
lines (Iterable[MessageLine]) --
The lines from which to create the multi-line string.
By default it is set to ().
- classmethod offset()[source]#
Create a temporary offset with a context manager.
- Return type:
Iterator[None]
- replace(old, new)[source]#
Return a new MultiLineString with all occurrences of old replaced by new.
- Parameters:
- Returns:
The MultiLineString copy with replaced occurrences.
- Return type:
- property lines: list[MessageLine]#
The strings composing the lines.
- get_variables_with_components(variables, names_to_sizes)[source]#
Convert a set of variables to
tuple(str, int)
objects.- Parameters:
- Returns:
The variables defined as
(name, component)
.- Return type:
- pretty_repr(obj, delimiter=', ', key_value_separator='=', sort=True, use_and=False)[source]#
Return an unambiguous string representation of an object based on
repr()
.- Parameters:
obj (Any) -- The object to represent.
delimiter (str) --
The string to separate string fields.
By default it is set to ", ".
key_value_separator (str) --
The string to separate key and value in a key-value pair of a mapping.
By default it is set to "=".
sort (bool) --
Whether to sort the elements when the object if a collection.
By default it is set to True.
use_and (bool) --
Whether to replace the last delimiter occurrence by
" and "
.By default it is set to False.
- Returns:
An unambiguous string representation of the object.
- Return type:
- pretty_str(obj, delimiter=', ', key_value_separator='=', sort=True, use_and=False)[source]#
Return a readable string representation of an object based on
str()
.- Parameters:
obj (Any) -- The object to represent.
delimiter (str) --
The string to separate string fields.
By default it is set to ", ".
key_value_separator (str) --
The string to separate key and value in a key-value pair of a mapping.
By default it is set to "=".
sort (bool) --
Whether to sort the elements when the object if a collection.
By default it is set to True.
use_and (bool) --
Whether to replace the last delimiter occurrence by
"and"
.By default it is set to False.
- Returns:
A readable string representation of the object.
- Return type:
- repr_variable(name, index, size=0, simplify=False)[source]#
Return the string representation of a variable.
- Parameters:
- Returns:
The string representation of the variable.
- Return type:
- DEFAULT_DELIMITER = ', '#
A string to separate string fields.
- DEFAULT_KEY_VALUE_SEPARATOR = '='#
A string to separate key and value in a key-value pair of a mapping.