The ToString class

Class for converting list[int] to strings with specified alphabet.

An instance of this class is callable and used to convert from list[int] to str. The integers in the list are converted to characters according to their position in the alphabet used to construct a ToString instance if one is provided, or using words.human_readable_letter otherwise.

>>> from libsemigroups_pybind11 import ToString
>>> to_string = ToString("bac")
>>> to_string([0, 1, 2])
'bac'
>>> to_string([0, 1, 0, 1, 0, 0, 0, 2, 0, 2])
'bababbbcbc'

>>> to_string.init()      # clear the alphabet
<ToString object with alphabet "">
>>> to_string([0, 1, 2])
'abc'

Contents

ToString

Class for converting list[int] to strings with specified alphabet.

ToString.alphabet(…)

Return the alphabet used for conversion.

ToString.can_convert_letter(…)

Check if the current ToString instance can convert a specified letter.

ToString.copy(…)

Copy a ToString object.

ToString.empty(…)

Check if the alphabet is defined.

ToString.init(…)

Overloaded function.

Full API

class ToString
__init__(*args, **kwargs)

Overloaded function.

__init__(self: ToString) None

Default constructor.

Constructs an empty object with no alphabet set.

__init__(self: ToString, alphabet: str) None

Construct with given alphabet.

Construct a ToString object with the given alphabet.

Parameters:

alphabet (str) – the alphabet.

Raises:

LibsemigroupsError – if there are repeated letters in alphabet.

__call__(self: ToString, input: list[int]) str

Convert a list[int] to a str.

This function converts its argument input into a str. The characters of input are converted using the alphabet used to construct the object or set via init(), or with words.human_readable_letter if empty() returns True.

Parameters:

input (list[int]) – the string to convert.

Returns:

The converted string.

Return type:

str

Raises:

LibsemigroupsError – if the alphabet used to define an instance of ToString is not empty and input contains letters that do not correspond to letters of the alphabet.

alphabet(self: ToString) str

Return the alphabet used for conversion.

This function returns a str corresponding to the ordered-set alphabet \(\{a_0, a_1, \dots, a_{n-1}\}\) that the initialised ToString object will use to convert from list[int] to str. Specifically, \(i \mapsto a_i\) where \(i\) will be an int in a list and \(a_i\) will be a character in a str.

If this function returns the empty string, then conversion will be performed using words.human_readable_letter.

Returns:

The alphabet.

Return type:

str

can_convert_letter(self: ToString, l: int) bool

Check if the current ToString instance can convert a specified letter.

This function returns True if l can be converted to a str using this ToString instance, and False otherwise.

Parameters:

l (int) – the letter to check the convertibility of.

Returns:

Whether the letter can be converted.

Return type:

bool

copy(self: ToString) ToString

Copy a ToString object.

Returns:

A copy.

Return type:

ToString

empty(self: ToString) bool

Check if the alphabet is defined.

This function return True if no alphabet has been defined, and False otherwise.

Returns:

A whether the alphabet is empty.

Return type:

bool

init(*args, **kwargs)

Overloaded function.

init(self: ToString) ToString

Initialize an existing ToString object.

This function puts a ToString object back into the same state as if it had been newly default constructed.

Returns:

self.

Return type:

ToString

See also

ToString()

init(self: ToString, alphabet: str) ToString

Initialize an existing Tostring object.

This function puts a ToString object back into the same state as if it had been newly constructed from alphabet.

Parameters:

alphabet (str) – the alphabet.

Returns:

self.

Return type:

ToString

Raises:

LibsemigroupsError – if there are repeated letters in alphabet.

See also

ToString(str)