string#


Copy of sensai.util.string from sensAI

class StringConverter[source]#

Abstraction for a string conversion mechanism.

abstract to_string(x: Any) str[source]#
class TagBuilder(*initial_components: str, glue: str = '_')[source]#

Assists in building strings made up of components that are joined via a glue string.

build() str[source]#
Returns:

the string (with all components joined)

with_alternative(cond: bool, true_component: str, false_component: str) Self[source]#

Adds a component depending on a condition.

Parameters:
  • cond – the condition

  • true_component – the component to add if the condition holds

  • false_component – the component to add if the condition does not hold

Returns:

the builder

with_component(component: str) Self[source]#
with_conditional(cond: bool, component: str) Self[source]#

Conditionally adds the given component.

Parameters:
  • cond – the condition

  • component – the component to add if the condition holds

Returns:

the builder

class ToStringMixin[source]#

Provides implementations for __str__ and __repr__ which are based on the format "<class name>[<object info>]" and "<class name>[id=<object id>, <object info>]" respectively, where <object info> is usually a list of entries of the form "<name>=<value>, ...".

By default, <class name> will be the qualified name of the class, and <object info> will include all properties of the class, including private ones starting with an underscore (though the underscore will be dropped in the string representation).

  • To exclude private properties, override _toStringExcludePrivate() to return True. If there are exceptions (and some private properties shall be retained), additionally override _toStringExcludeExceptions().

  • To exclude a particular set of properties, override _toStringExcludes().

  • To include only select properties (introducing inclusion semantics), override _toStringIncludes().

  • To add values to the properties list that aren’t actually properties of the object (i.e. derived properties), override _toStringAdditionalEntries().

  • To define a fully custom representation for <object info> which is not based on the above principles, override _toStringObjectInfo().

For well-defined string conversions within a class hierarchy, it can be a good practice to define additional inclusions/exclusions by overriding the respective method once more and basing the return value on an extended version of the value returned by superclass. In some cases, the requirements of a subclass can be at odds with the definitions in the superclass: The superclass may make use of exclusion semantics, but the subclass may want to use inclusion semantics (and include only some of the many properties it adds). In this case, if the subclass used _toStringInclude(), the exclusion semantics of the superclass would be void and none of its properties would actually be included. In such cases, override _toStringIncludesForced() to add inclusions regardless of the semantics otherwise used along the class hierarchy.

pprint(file: ~typing.Any = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>) None[source]#

Prints a prettily formatted string representation of the object (with line breaks and indentations) to stdout or the given file.

Parameters:

file – the file to print to

pprints() str[source]#
Returns:

a prettily formatted string representation with line breaks and indentations

dict_string(d: Mapping, brackets: str | None = None, converter: StringConverter | None = None) str[source]#

Converts a dictionary to a string of the form “<key>=<value>, <key>=<value>, …”, optionally enclosed by brackets.

Parameters:
  • d – the dictionary

  • brackets – a two-character string containing the opening and closing bracket to use, e.g. "{}"; if None, do not use enclosing brackets

  • converter – the string converter to use for values

Returns:

the string representation

function_name(x: Callable) str[source]#

Attempts to retrieve the name of the given function/callable object, taking the possibility of the function being defined via functools.partial into account.

Parameters:

x – a callable object

Returns:

name of the function or str(x) as a fallback

list_string(l: Iterable[Any], brackets: str | None = '[]', quote: str | None = None, converter: StringConverter | None = None) str[source]#

Converts a list or any other iterable to a string of the form “[<value>, <value>, …]”, optionally enclosed by different brackets or with the values quoted.

Parameters:
  • l – the list

  • brackets – a two-character string containing the opening and closing bracket to use, e.g. "[]"; if None, do not use enclosing brackets

  • quote – a 1-character string defining the quote to use around each value, e.g. "'".

  • converter – the string converter to use for values

Returns:

the string representation

object_repr(obj: Any, member_names_or_dict: list[str] | dict[str, Any]) str[source]#

Creates a string representation for the given object based on the given members.

The string takes the form “ClassName[attr1=value1, attr2=value2, …]”

or_regex_group(allowed_names: Sequence[str]) str[source]#
Parameters:

allowed_names – strings to include as literals in the regex

Returns:

a regular expression string of the form (<name_1>| …|<name_N>), which any of the given names

pretty_string_repr(s: Any, initial_indentation_level: int = 0, indentation_string: str = '    ') str[source]#

Creates a pretty string representation (using indentations) from the given object/string representation (as generated, for example, via ToStringMixin). An indentation level is added for every opening bracket.

Parameters:
  • s – an object or object string representation

  • initial_indentation_level – the initial indentation level

  • indentation_string – the string which corresponds to a single indentation level

Returns:

a reformatted version of the input string with added indentations and line breaks

to_string(x: Any, converter: StringConverter | None = None, apply_converter_to_non_complex_objects: bool = True, context: Any = None) str[source]#

Converts the given object to a string, with proper handling of lists, tuples and dictionaries, optionally using a converter. The conversion also removes unwanted line breaks (as present, in particular, in sklearn’s string representations).

Parameters:
  • x – the object to convert

  • converter – the converter with which to convert objects to strings

  • apply_converter_to_non_complex_objects – whether to apply/pass on the converter (if any) not only when converting complex objects but also non-complex, primitive objects; use of this flag enables converters to implement their conversion functionality using this function for complex objects without causing an infinite recursion.

  • context – context in which the object is being converted (e.g. dictionary key for case where x is the corresponding dictionary value), only for debugging purposes (will be reported in log messages upon recursion exception)

Returns:

the string representation