bases.alphabet

Module containing classes for alphabets.

base10

base10 = StringAlphabet('0123456789')

Base-10 alphabet.

base16

base16 = StringAlphabet('0123456789ABCDEF', case_sensitive=False)

Uppercase case-insensitive base-16 alphabet.

base2

base2 = StringAlphabet('01')

Base-2 alphabet.

base32

base32 = StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False)

Uppercase case-insensitive base-32 alphabet from rfc4648.

base32hex

base32hex = StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV', case_sensitive=False)

Uppercase case-insensitive hex base-32 alphabet from rfc4648.

base32z

base32z = StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False)

Lowercase case-insensitive human-oriented base-32 alphabet from https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt

base36

base36 = StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', case_sensitive=False)

Uppercase case-insensitive base-36 alphabet.

base45

base45 = StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:', case_sensitive=False)

Uppercase case-insensitive base-45 alphabet from https://datatracker.ietf.org/doc/draft-faltstrom-base45/

base58btc

base58btc = StringAlphabet('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')

Case-sensitive base-58 alphabet used by Bitcoin.

base58flickr

base58flickr = StringAlphabet('123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ')

Case-sensitive base-58 alphabet used by Flickr.

base58ripple

base58ripple = StringAlphabet('rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz')

Case-sensitive base-58 alphabet used by Ripple.

base64

base64 = StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')

Uppercase case-insensitive base-64 alphabet from rfc4648.

base64url

base64url = StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_')

Uppercase case-insensitive url-safe base-64 alphabet from rfc4648.

base8

base8 = StringAlphabet('01234567')

Base-8 alphabet.

get

get(name)[source]

Gets an alphabet by name, raising KeyError if none exists.

Example usage:

>>> alphabet.get("base16")
StringAlphabet('0123456789ABCDEF')
Parameters:

name (str) – the alphabet name

Return type:

Alphabet

has

has(name)[source]

Checks whether an alphabet with the given name exists.

Example usage:

>>> from bases import alphabet
>>> alphabet.has("base32")
True
Parameters:

name (str) – the alphabet name

Return type:

bool

make

make(chars, *, case_sensitive=True, name=None)[source]

Utility function to create custom alphabets. Automatically creates an instance of StringAlphabet or RangeAlphabet based on whether a string or range is passed.

Example usage with string (case-insensitive base-16):

>>> alphabet.make("0123456789ABCDEF", case_sensitive=False)
StringAlphabet('0123456789ABCDEF', case_sensitive=False)

Example usage with range (extended ASCII):

>>> alphabet.make(range(0x00, 0x100))
RangeAlphabet(range(0x00, 0x100))

If the optional keyword argument name is specified, the alphabet is automatically registered using register.

Parameters:
  • chars (str or range) – the alphabet characters or codepoint range

  • case_sensitive (bool, optional) – whether the alphabet is case-sensitive

  • name (str or None, optional) – if specified, the newly created alphabet is registered with this name

Return type:

Union[StringAlphabet, RangeAlphabet]

register

register(**alphabets)[source]

Registers any number of new alphabets by name.

Example usage:

>>> from bases import alphabet
>>> alphabet.register(base16lower=alphabet.base16.lower())
>>> alphabet.get("base16lower")
StringAlphabet('0123456789abcdef')

Alphabet names must conform with:

re.match(r"^base[0-9][a-zA-Z0-9_]*$", name)
Parameters:

alphabets (Dict[str, Alphabet]) – the alphabets to register, passed by desired registration name

Raises:

KeyError – if an alphabet with one of the given names already exists

Return type:

None

table

table(*, prefix='')[source]

Iterates over all registered alphabets, optionally restricting to those with given prefix.

Example usage:

>>> from bases import alphabet
>>> dict(alphabet.table(prefix="base32"))
{'base32': StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False),
 'base32hex': StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV', case_sensitive=False),
 'base32z': StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769', case_sensitive=False)}
Parameters:
  • name (str, optional) – optional prefix to filter by when listing alphabets

  • prefix (str; default = '') –

Return type:

Iterator[Tuple[str, Alphabet]]

unregister

unregister(*names)[source]

Unregisters any number of existing alphabets by name.

Example usage:

>>> from bases import alphabet
>>> alphabet.unregister("base16", "base32")
>>> alphabet.has("base16")
False
>>> alphabet.get("base16")
KeyError: "Alphabet named 'base16' does not exist."

Note that pre-defined constants are unaffected by this:

>>> alphabet.base16
StringAlphabet('0123456789ABCDEF')
Parameters:

names (Tuple[str, …]) – the alphabet names

Raises:

KeyError – if an alphabet with one of the given names does not exists

Return type:

None