string_tools module¶
Pretty string utils.
- class gemseo.utils.string_tools.MessageLine(str_format, level, args, kwargs)
Bases:
tuple
Create new instance of MessageLine(str_format, level, args, kwargs)
- args
Alias for field number 2
- kwargs
Alias for field number 3
- level
Alias for field number 1
- str_format
Alias for field number 0
- class gemseo.utils.string_tools.MultiLineString(lines=None)[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] | None) – The lines from which to create the multi-line string.
- add(str_format, *args, **kwargs)[source]
Add a line.
- dedent()[source]
Decrease the indentation.
- Return type:
None
- indent()[source]
Increase the indentation.
- Return type:
None
- 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:
- reset()[source]
Reset the indentation.
- Return type:
None
- DEFAULT_LEVEL = 0
- INDENTATION = ' '
- property lines: list[MessageLine]
The strings composing the lines.
- gemseo.utils.string_tools.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:
- gemseo.utils.string_tools.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:
- gemseo.utils.string_tools.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:
- gemseo.utils.string_tools.DEFAULT_DELIMITER = ', '
A string to separate string fields.
- gemseo.utils.string_tools.DEFAULT_KEY_VALUE_SEPARATOR = '='
A string to separate key and value in a key-value pair of a mapping.