bittensor.utils#

Submodules#

Attributes#

Exceptions#

VersionCheckError

Common base class for all non-exit exceptions.

Functions#

use_torch()

Force the use of torch over numpy for certain operations.

version_checking([timeout])

Deprecated, kept for backwards compatibility. Use check_version() instead.

check_version([timeout])

Check if the current version of Bittensor is up to date with the latest version on PyPi.

get_ss58_format(ss58_address)

Returns the ss58 format of the given ss58 address.

is_valid_ss58_address(address)

Checks if the given address is a valid ss58 address.

is_valid_ed25519_pubkey(public_key)

Checks if the given public_key is a valid ed25519 key.

is_valid_bittensor_address_or_public_key(address)

Checks if the given address is a valid destination address.

create_identity_dict([display, legal, web, riot, ...])

Creates a dictionary with structure for identity extrinsic. Must fit within 64 bits.

decode_hex_identity_dict(info_dictionary)

ss58_to_vec_u8(ss58_address)

_unbiased_topk(values, k[, dim, sorted, largest, ...])

Selects topk as in torch.topk but does not bias lower indices when values are equal.

unbiased_topk(values, k[, dim, sorted, largest, axis])

Selects topk as in torch.topk but does not bias lower indices when values are equal.

strtobool_with_default(default)

Creates a strtobool function with a default value.

strtobool(val)

Converts a string to a boolean value.

get_explorer_root_url_by_network_from_map(network, ...)

Returns the explorer root url for the given network name from the given network map.

get_explorer_url_for_network(network, block_hash, ...)

Returns the explorer url for the given block hash and network.

ss58_address_to_bytes(ss58_address)

Converts a ss58 address to a bytes object.

U16_NORMALIZED_FLOAT(x)

U64_NORMALIZED_FLOAT(x)

u8_key_to_ss58(u8_key)

Converts a u8-encoded account key to an ss58 address.

hash(content[, encoding])

format_error_message(error_message)

Formats an error message from the Subtensor error information to using in extrinsics.

Package Contents#

bittensor.utils.torch#
bittensor.utils.use_torch()[source]#

Force the use of torch over numpy for certain operations.

Return type:

bool

bittensor.utils.version_checking(timeout=15)[source]#

Deprecated, kept for backwards compatibility. Use check_version() instead.

Parameters:

timeout (int)

bittensor.utils.check_version(timeout=15)[source]#

Check if the current version of Bittensor is up to date with the latest version on PyPi. Raises a VersionCheckError if the version check fails.

Parameters:

timeout (int)

exception bittensor.utils.VersionCheckError[source]#

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.

bittensor.utils.get_ss58_format(ss58_address)[source]#

Returns the ss58 format of the given ss58 address.

Parameters:

ss58_address (str)

Return type:

int

bittensor.utils.is_valid_ss58_address(address)[source]#

Checks if the given address is a valid ss58 address.

Parameters:

address (str) – The address to check.

Returns:

True if the address is a valid ss58 address for Bittensor, False otherwise.

Return type:

bool

bittensor.utils.is_valid_ed25519_pubkey(public_key)[source]#

Checks if the given public_key is a valid ed25519 key.

Parameters:

public_key (Union[str, bytes]) – The public_key to check.

Returns:

True if the public_key is a valid ed25519 key, False otherwise.

Return type:

bool

bittensor.utils.is_valid_bittensor_address_or_public_key(address)[source]#

Checks if the given address is a valid destination address.

Parameters:

address (Union[str, bytes]) – The address to check.

Returns:

True if the address is a valid destination address, False otherwise.

Return type:

bool

bittensor.utils.create_identity_dict(display='', legal='', web='', riot='', email='', pgp_fingerprint=None, image='', info='', twitter='')[source]#

Creates a dictionary with structure for identity extrinsic. Must fit within 64 bits.

Parameters:
  • display (str) – String to be converted and stored under ‘display’.

  • legal (str) – String to be converted and stored under ‘legal’.

  • web (str) – String to be converted and stored under ‘web’.

  • riot (str) – String to be converted and stored under ‘riot’.

  • email (str) – String to be converted and stored under ‘email’.

  • pgp_fingerprint (str) – String to be converted and stored under ‘pgp_fingerprint’.

  • image (str) – String to be converted and stored under ‘image’.

  • info (str) – String to be converted and stored under ‘info’.

  • twitter (str) – String to be converted and stored under ‘twitter’.

Returns:

A dictionary with the specified structure and byte string conversions.

Return type:

dict

Raises: ValueError: If pgp_fingerprint is not exactly 20 bytes long when encoded.

bittensor.utils.decode_hex_identity_dict(info_dictionary)[source]#
bittensor.utils.RAOPERTAO = 1000000000.0#
bittensor.utils.U16_MAX = 65535#
bittensor.utils.U64_MAX = 18446744073709551615#
bittensor.utils.ss58_to_vec_u8(ss58_address)[source]#
Parameters:

ss58_address (str)

Return type:

List[int]

bittensor.utils._unbiased_topk(values, k, dim=0, sorted=True, largest=True, axis=0, return_type='numpy')[source]#

Selects topk as in torch.topk but does not bias lower indices when values are equal. :param values: (np.ndarray) if using numpy, (torch.Tensor) if using torch:

Values to index into.

Parameters:
  • k (int) – (int): Number to take.

  • dim – (int): Dimension to index into (used by Torch)

  • sorted – (bool): Whether to sort indices.

  • largest – (bool): Whether to take the largest value.

  • axis – (int): Axis along which to index into (used by Numpy)

  • return_type (str) – (str): Whether or use torch or numpy approach

  • values (wallet_utils.Union[numpy.ndarray, registration.torch.Tensor])

Returns:

(np.ndarray) if using numpy, (torch.Tensor) if using torch:

topk k values.

indices: (np.ndarray) if using numpy, (torch.LongTensor) if using torch:

indices of the topk values.

Return type:

topk

bittensor.utils.unbiased_topk(values, k, dim=0, sorted=True, largest=True, axis=0)[source]#

Selects topk as in torch.topk but does not bias lower indices when values are equal. :param values: (np.ndarray) if using numpy, (torch.Tensor) if using torch:

Values to index into.

Parameters:
  • k (int) – (int): Number to take.

  • dim (int) – (int): Dimension to index into (used by Torch)

  • sorted (bool) – (bool): Whether to sort indices.

  • largest (bool) – (bool): Whether to take the largest value.

  • axis (int) – (int): Axis along which to index into (used by Numpy)

  • values (wallet_utils.Union[numpy.ndarray, registration.torch.Tensor])

Returns:

(np.ndarray) if using numpy, (torch.Tensor) if using torch:

topk k values.

indices: (np.ndarray) if using numpy, (torch.LongTensor) if using torch:

indices of the topk values.

Return type:

topk

bittensor.utils.strtobool_with_default(default)[source]#

Creates a strtobool function with a default value.

Parameters:

default (bool) – The default value to return if the string is empty.

Returns:

The strtobool function with the default value.

Return type:

Callable[[str], wallet_utils.Union[bool, Literal[‘==SUPRESS==’]]]

bittensor.utils.strtobool(val)[source]#

Converts a string to a boolean value.

truth-y values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false-y values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’.

Raises ValueError if ‘val’ is anything else.

Parameters:

val (str)

Return type:

wallet_utils.Union[bool, Literal[‘==SUPRESS==’]]

bittensor.utils.get_explorer_root_url_by_network_from_map(network, network_map)[source]#

Returns the explorer root url for the given network name from the given network map.

Parameters:
  • network (str) – The network to get the explorer url for.

  • network_map (Dict[str, str]) – The network map to get the explorer url from.

Returns:

The explorer url for the given network. Or None if the network is not in the network map.

Return type:

wallet_utils.Optional[Dict[str, str]]

bittensor.utils.get_explorer_url_for_network(network, block_hash, network_map)[source]#

Returns the explorer url for the given block hash and network.

Parameters:
  • network (str) – The network to get the explorer url for.

  • block_hash (str) – The block hash to get the explorer url for.

  • network_map (Dict[str, Dict[str, str]]) – The network maps to get the explorer urls from.

Returns:

The explorer url for the given block hash and network. Or None if the network is not known.

Return type:

wallet_utils.Optional[List[str]]

bittensor.utils.ss58_address_to_bytes(ss58_address)[source]#

Converts a ss58 address to a bytes object.

Parameters:

ss58_address (str)

Return type:

bytes

bittensor.utils.U16_NORMALIZED_FLOAT(x)[source]#
Parameters:

x (int)

Return type:

float

bittensor.utils.U64_NORMALIZED_FLOAT(x)[source]#
Parameters:

x (int)

Return type:

float

bittensor.utils.u8_key_to_ss58(u8_key)[source]#

Converts a u8-encoded account key to an ss58 address.

Parameters:

u8_key (List[int]) – The u8-encoded account key.

Return type:

str

bittensor.utils.hash(content, encoding='utf-8')[source]#
bittensor.utils.format_error_message(error_message)[source]#

Formats an error message from the Subtensor error information to using in extrinsics.

Parameters:

error_message (dict) – A dictionary containing the error information from Subtensor.

Returns:

A formatted error message string.

Return type:

str