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)

Parameters:
args: Any#

Alias for field number 2

kwargs: Any#

Alias for field number 3

level: int#

Alias for field number 1

str_format: str#

Alias for field number 0

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

add(str_format, *args, **kwargs)[source]#

Add a line.

Parameters:
  • str_format (str) -- The string to be process by the format() method.

  • args (Any) -- The args passed to the format() method.

  • kwargs (Any) -- The kwargs passed to the format() method.

Return type:

None

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:
  • old (str) -- The sub-string to be replaced.

  • new (str) -- The sub-string to be replaced with.

Returns:

The MultiLineString copy with replaced occurrences.

Return type:

MultiLineString

reset()[source]#

Reset the indentation.

Return type:

None

DEFAULT_LEVEL: ClassVar[int] = 0#

The default indentation level.

INDENTATION: ClassVar[str] = '   '#

The indentation increment of each indentation level.

property lines: list[MessageLine]#

The strings composing the lines.

convert_strings_to_iterable(str_or_strs)[source]#

Return strings as an iterable.

Parameters:

str_or_strs (str | Iterable[str]) -- A string or several strings.

Returns:

Names.

Return type:

Iterable[str]

filter_names(names, names_to_keep)[source]#

Filter names from a collection of other names.

Parameters:
  • names (Iterable[str]) -- The original names.

  • names_to_keep (Iterable[str]) -- The names to keep. If None, keep all.

Returns:

The filtered names.

Return type:

Iterable[str]

get_name_and_component(variable)[source]#

Return the name and the component of a variable.

Parameters:

variable (str | tuple[str, int]) -- Either a variable name or a variable name with its variable component.

Returns:

The name and the component of a variable.

Return type:

tuple[str, int]

get_variables_with_components(variables, names_to_sizes)[source]#

Convert a set of variables to tuple(str, int) objects.

Parameters:
  • variables (VariableType | Iterable[VariableType]) -- One or several variable defined as name or (name, component). When name, all the components of the variable are considered.

  • names_to_sizes (Mapping[str, int]) -- The sizes of the variables.

Returns:

The variables defined as (name, component).

Return type:

Iterator[tuple[str, int]]

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:

str

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:

str

repr_variable(name, index, size=0, simplify=False)[source]#

Return the string representation of a variable.

Parameters:
  • name (str) -- The name of the variable.

  • index (int) -- The component of the variable.

  • size (int) --

    The size of the variable if known. Use 0 if unknown.

    By default it is set to 0.

  • simplify (bool) --

    Whether to return "[i]" when i>0 instead of "name[i]".

    By default it is set to False.

Returns:

The string representation of the variable.

Return type:

str

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.