stdlib

base58_encode(key: bytes) str

Encodes a bytes value using base58.

>>> base58_encode(b'unit test')
b"2VhL46g69A1mu"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

base58_decode(key: str) bytes

Decodes a string value encoded with base58.

>>> base58_decode('2VhL46g69A1mu')
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded bytes

Return type:

bytes

base58_check_encode(key: bytes) str

Converts a bytes value to its equivalent str representation that is encoded with base-58 digits. The encoded str contains the checksum of the binary data.

>>> base58_check_encode(b'unit test')
b"AnJcKqvgBwKxsjX75o"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

base58_check_decode(key: str) bytes

Converts the specified str, which encodes binary data as base-58 digits, to an equivalent bytes value. The encoded str contains the checksum of the binary data.

>>> base58_check_decode('AnJcKqvgBwKxsjX75o')
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded bytes

Return type:

bytes

base64_encode(key: bytes) str

Encodes a bytes value using base64.

>>> base64_encode(b'unit test')
b"dW5pdCB0ZXN0"
Parameters:

key (bytes) – bytes value to be encoded

Returns:

the encoded string

Return type:

str

base64_decode(key: str) bytes

Decodes a string value encoded with base64.

>>> base64_decode("dW5pdCB0ZXN0")
b"unit test"
Parameters:

key (str) – string value to be decoded

Returns:

the decoded bytes

Return type:

bytes

serialize(item: Any) bytes

Serializes the given value into its bytes representation.

>>> serialize('42')
b'(42'
>>> serialize(42)
b'!*'
>>> serialize([2, 3, 5, 7])
b'@!!!!'
>>> serialize({1: 1, 2: 1, 3: 2})
b'H!!!!!!'
Parameters:

item (Any) – value to be serialized

Returns:

the serialized value

Return type:

bytes

Raises:

Exception – raised if the item’s type is not serializable.

deserialize(data: bytes) Any

Deserializes the given bytes value.

>>> deserialize(b'(42')
'42'
>>> deserialize(b'!*')
42
>>> deserialize(b'@!!!!')
[2, 3, 5, 7]
>>> deserialize(b'H!!!!!!')
{1: 1, 2: 1, 3: 2}
Parameters:

data (bytes) – serialized value

Returns:

the deserialized result

Return type:

Any

Raises:

Exception – raised when the date doesn’t represent a serialized value.

atoi(value: str, base: int = 10) int

Converts a character string to a specific base value, decimal or hexadecimal. The default is decimal.

>>> atoi('10')
10
>>> atoi('123')
123
>>> atoi('1f', 16)
31
>>> atoi('ff', 16)
-1
Parameters:
  • value (str) – the int value as a string

  • base (int) – the value base

Returns:

the equivalent value

Return type:

int

Raises:

Exception – raised when base isn’t 10 or 16.

itoa(value: int, base: int = 10) str

Converts the specific type of value to a decimal or hexadecimal string. The default is decimal.

>>> itoa(10)
'10'
>>> itoa(123)
'123'
>>> itoa(-1, 16)
'f'
>>> itoa(15, 16)
'0f'
Parameters:
  • value (int) – the int value

  • base (int) – the value’s base

Returns:

the converted string

Return type:

int

Searches for a given value in a given memory.

>>> memory_search('abcde', 'a', 0)
0
>>> memory_search('abcde', 'e', 0)
4
Parameters:
  • mem (bytes or str) – the memory

  • value (bytes or str) – the value

  • start (int) – the index the search should start from

  • backward (bool) – whether it should invert the memory

Returns:

the index of the value in the memory. Returns -1 if it’s not found

Return type:

int

memory_compare(mem1: Union[bytes, str], mem2: Union[bytes, str]) int

Compares a memory with another one.

>>> memory_compare('abc', 'abc')
0
>>> memory_compare('ABC', 'abc')
-1
>>> memory_compare('abc', 'ABC')
1
Parameters:
  • mem1 (bytes or str) – a memory to be compared to another one

  • mem2 (bytes or str) – a memory that will be compared with another one

Returns:

-1 if mem1 precedes mem2, 0 if mem1 and mem2 are equal, 1 if mem1 follows mem2

Return type:

int