bases.alphabet.abstract

Abstract alphabets.

Alphabet

class Alphabet(case_sensitive=True)[source]

Bases: ABC, Sequence[str]

Abstract superclass for alphabets with specified case sensitivity. It contains a mapping of digits (numbers in range(len(self))) to characters and a reverse mapping of characters to digits:

>>> alphabet.base16
StringAlphabet('0123456789ABCDEF')
>>> alphabet.base16[12]
'C'
>>> alphabet.base16.index("C")
12
Parameters:

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

property case_sensitive

Case sensitivity of the alphabet (default: True). If set to False, characters will be deemed in the alphabet regardless of case. However, accessing characters from the alphabet will yield the case originally specified:

>>> alphabet.base32
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
               case_sensitive=False)
>>> "a" in alphabet.base32
True
>>> alphabet.base32[0]
'A'
Return type:

bool

count(char)[source]

Returns 1 if char is in the alphabet and 0 otherwise.

Parameters:

char (str) – the character to count for

Return type:

int

index(char, start=0, stop=None)[source]

Returns the index of a character in the alphabet. Optionally allows a starting index (included) and stopping index (excluded).

Example usage:

>>> alphabet.base32
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
               case_sensitive=False)
>>> alphabet.base32.index("F")
5
>>> alphabet.base32[5]
'F'
>>> alphabet.base32.index("f") # case-insensitive
5
Parameters:
  • char (str) – the character to look for

  • start (int; default = 0) – optional start index (included) for the search range

  • startint, optional

  • start – optional end index (excluded) for the search range

  • startint or None, optional

  • stop (Optional[int]) –

Raises:

ValueError – if the character is not in the alphabet

Return type:

int

abstract lower()[source]

Returns a new alphabet with all cased characters turned to lowercase.

Example usage:

>>> alphabet.base16
StringAlphabet('0123456789ABCDEF')
>>> alphabet.base16.lower()
StringAlphabet('0123456789abcdef')
Return type:

Alphabet

abstract property revdir

Reverse directory for the alphabet, mapping characters to their index.

Example usage:

>>> alphabet.base16.revdir
mappingproxy({'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
              '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
              'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15})

For case-insensitive alphabets, this contains both cases of any cased characters:

>>> alphabet.base16.with_case_sensitivity(False).revdir
mappingproxy({'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
              '5': 5, '6': 6, '7': 7,'8': 8, '9': 9,
              'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15,
              'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15})
Return type:

Mapping[str, int]

abstract upper()[source]

Returns a new alphabet with all cased characters turned to uppercase.

Example usage:

>>> alphabet.base32z
StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769',
               case_sensitive=False)
>>> alphabet.base32z.upper()
StringAlphabet('YBNDRFG8EJKMCPQXOT1UWISZA345H769',
               case_sensitive=False)
Return type:

Alphabet

abstract with_case_sensitivity(case_sensitive)[source]

Returns a new alphabet with the same characters as this one but with specified case sensitivity.

Example usage:

>>> alphabet.base32
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
               case_sensitive=False)
>>> alphabet.base32.with_case_sensitivity(True)
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')
Parameters:

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

Return type:

Self