string

class StringConverter[source]

Bases: abc.ABC

Abstraction for a string conversion mechanism

abstract to_string(x) str
dict_string(d: Mapping, brackets: Optional[str] = None, converter: Optional[sensai.util.string.StringConverter] = None)[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

list_string(l: Iterable[Any], brackets='[]', quote: Optional[str] = None, converter: Optional[sensai.util.string.StringConverter] = None)[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

to_string(x, converter: Optional[sensai.util.string.StringConverter] = None, apply_converter_to_non_complex_objects=True, context=None)[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

object_repr(obj, member_names_or_dict: Union[List[str], Dict[str, Any]])[source]
or_regex_group(allowed_names: Sequence[str])[source]
Parameters

allowed_names – strings to include as literals in the regex

Returns

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

function_name(x: Callable) str[source]
class ToStringMixin[source]

Bases: object

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.

_tostring_class_name()
Returns

the string use for <class name> in the string representation "<class name>[<object info]"

_tostring_object_info() str

Override this method to use a fully custom definition of the <object info> part in the full string representation "<class name>[<object info>]" to be generated. As soon as this method is overridden, any property-based exclusions, inclusions, etc. will have no effect (unless the implementation is specifically designed to make use of them - as is the default implementation). NOTE: Overrides must not internally use super() because of a technical limitation in the proxy object that is used for nested object structures.

Returns

a string containing the string to use for <object info>

_tostring_excludes() List[str]

Makes the string representation exclude the returned attributes. This method can be conveniently overridden by subclasses which can call super and extend the list returned.

This method will only have no effect if _toStringObjectInfo() is overridden to not use its result.

Returns

a list of attribute names

_tostring_exclude_exceptions() List[str]

Defines attribute names which should not be excluded even though other rules (particularly the exclusion of private members via _toStringExcludePrivate()) would otherwise exclude them.

Returns

a list of attribute names

_tostring_includes() List[str]

Makes the string representation include only the returned attributes (i.e. introduces inclusion semantics); By default, the list contains only a marker element, which is interpreted as “all attributes included”.

This method can be conveniently overridden by sub-classes which can call super and extend the list returned. Note that it is not a problem for a list containing the aforementioned marker element (which stands for all attributes) to be extended; the marker element will be ignored and only the user-added elements will be considered as included.

Note: To add an included attribute in a sub-class, regardless of any super-classes using exclusion or inclusion semantics, use _toStringIncludesForced instead.

This method will have no effect if _toStringObjectInfo() is overridden to not use its result.

Returns

a list of attribute names to be included in the string representation

_tostring_includes_forced() List[str]

Defines a list of attribute names that are required to be present in the string representation, regardless of the instance using include semantics or exclude semantics, thus facilitating added inclusions in sub-classes.

This method will have no effect if _toStringObjectInfo() is overridden to not use its result.

Returns

a list of attribute names

_tostring_additional_entries() Dict[str, Any]
Returns

a dictionary of entries to be included in the <object info> part of the string representation

_tostring_exclude_private() bool
Returns

whether to exclude properties that are private (start with an underscore); explicitly included attributes will still be considered - as will properties exempt from the rule via toStringExcludeException().

pprint(file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)

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
Returns

a prettily formatted string representation with line breaks and indentations

pretty_string_repr(s: Any, initial_indentation_level=0, indentation_string='    ')[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

class TagBuilder(*initial_components: str, glue='_')[source]

Bases: object

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

__init__(*initial_components: str, glue='_')
Parameters
  • initial_components – initial components to always include at the beginning

  • glue – the glue string which joins components

with_component(component: str)
with_conditional(cond: bool, component: str)

Conditionally adds the given component

Parameters
  • cond – the condition

  • component – the component to add if the condition holds

Returns

the builder

with_alternative(cond: bool, true_component: str, false_component: str)

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

build()
Returns

the string (with all components joined)