bases.alphabet.string_alphabet

Alphabets explicitly specified by strings.

StringAlphabet

class StringAlphabet(chars, *, case_sensitive=True)[source]

Bases: Alphabet

Class for alphabets explicitly specified by a string of (unique) characters and optional case sensitivity (default: case-sensitive).

Example usage:

>>> from bases.alphabet import StringAlphabet
>>> StringAlphabet("0123456789abcdef", case_sensitive=False)
StringAlphabet('0123456789abcdef', case_sensitive=False)
>>> StringAlphabet("0123").case_sensitive
True
Parameters:
  • chars (str) – a string excplicitly listing all characters in the alphabet

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

Raises:
  • ValueError – if the alphabet contains fewer than 2 characters

  • ValueError – if chars contains repeated characters

  • ValueError – if the alphabet is case-insensitive and it contains both uppercase and lowercase versions of the same character

property chars

The characters that define this alphabet.

Example usage:

>>> alphabet.base16.chars
'0123456789ABCDEF'
Return type:

str

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:

StringAlphabet

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]

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:

StringAlphabet

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:

StringAlphabet