bases.encoding.base

Abstract base encodings.

BaseEncoding

class BaseEncoding(alphabet, *, case_sensitive=None)[source]

Bases: ABC

Abstract superclass for base encodings. Instances can always be constructed from an alphabet (with optional change of case sensitivity) and a number of additional options specified by subclasses.

Parameters:
  • alphabet (str, range or Alphabet) – the alphabet to use for the encoding

  • case_sensitive (bool or None, optional) – optional case sensitivity (if None, the one from the alphabet is used)

property alphabet

The encoding alphabet.

Example usage:

>>> encoding.base32.alphabet
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
               case_sensitive=False)
Return type:

Alphabet

property base

The base for this encoding (the length of the alphabet).

Example usage:

>>> encoding.base32.base
32
Return type:

int

canonical_bytes(b)[source]

Returns a canonical version of the bytestring b: this is the bytestring obtained by first encoding b and then decoding it.

(This method is overridden by subclasses with more efficient implementations.)

Parameters:

b (BytesLike) – the bytestring

Return type:

bytes

canonical_string(s)[source]

Returns a canonical version of the string s: this is the string obtained by first decoding s and then encoding it.

(This method is overridden by subclasses with more efficient implementations.)

Parameters:

s (str) – the string

Return type:

str

property case_sensitive

Determines whether the decoder is case sensitive.

Example usage:

>>> encoding.base32.case_sensitive
False
Return type:

bool

decode(s)[source]

Decodes a string into a bytestring.

Example usage:

>>> s = 'IZRL5O2C4CZA===='
>>> list(encoding.base32.decode(s))
[70, 98, 190, 187, 66, 224, 178]
Parameters:

s (str) – the string

Raises:

DecodingError – if the string is invalid

Return type:

bytes

encode(b)[source]

Encodes a bytestring into a string.

Example usage:

>>> b = bytes([70, 98, 190, 187, 66, 224, 178])
>>> encoding.base32.encode(b)
'IZRL5O2C4CZA===='
>>> s = 'IZRL5O2C4CZA===='
>>> list(base32.decode(s))
[70, 98, 190, 187, 66, 224, 178]
Parameters:

b (BytesLike) – the bytestring

Raises:

EncodingError – if the bytestring is invalid

Return type:

str

lower()[source]

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

Example usage:

>>> encoding.base32
FixcharBaseEncoding(
    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                   case_sensitive=False),
    pad_char='=', padding='include')
>>> encoding.base32.lower()
FixcharBaseEncoding(
    StringAlphabet('abcdefghijklmnopqrstuvwxyz234567',
                   case_sensitive=False),
    pad_char='=', padding='include')
Return type:

BaseEncodingSubclass

Parameters:

self (BaseEncodingSubclass) –

abstract options(skip_defaults=False)[source]

The options used to construct this particular encoding.

Example usage:

>>> encoding.base32.options()
{'char_nbits': 'auto', 'pad_char': '=', 'padding': 'include'}
>>> encoding.base32.options(skip_defaults=True)
{'pad_char': '=', 'padding': 'include'}
Parameters:

skip_defaults (bool, optional) – if set to True, only options with non-default values are included in the mapping

Return type:

Mapping[str, Any]

upper()[source]

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

Example usage:

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

BaseEncodingSubclass

Parameters:

self (BaseEncodingSubclass) –

with_alphabet(alphabet, *, case_sensitive=None)[source]

Returns a new encoding with the same kind and options as this one, but a different alphabet and/or case sensitivity.

Parameters:
Return type:

BaseEncodingSubclass

with_case_sensitivity(case_sensitive)[source]

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

Example usage:

>>> encoding.base32
FixcharBaseEncoding(
    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                   case_sensitive=False),
    pad_char='=', padding='include')
>>> encoding.base32.with_case_sensitivity(True)
FixcharBaseEncoding(
    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'),
    pad_char='=', padding='include')
Parameters:
Return type:

BaseEncodingSubclass

with_options(**options)[source]

Returns a new encoding with the same kind, alphabet and case sensitivity as this one, but different options.

Parameters:
Return type:

BaseEncodingSubclass

property zero_char

The zero digit for this encoding (first character in the alphabet).

Example usage:

>>> encoding.base32.alphabet
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
               case_sensitive=False)
>>> encoding.base32.zero_char
'A'
Return type:

str

BaseEncodingSubclass

BaseEncodingSubclass = ~BaseEncodingSubclass

Type variable for subclasses of BaseEncoding.

BytesLike

BytesLike

Type alias for bytes-like objects.

alias of Union[bytes, bytearray, memoryview]

byteslike

byteslike = (<class 'bytes'>, <class 'bytearray'>, <class 'memoryview'>)

Tuple of bytes-like objects types (for use with isinstance checks).

lstrip_memview

lstrip_memview(b, byte=0)[source]

Returns a new memoryview obtained by slicing away all leading zero bytes from the given memoryview b.

Example usage:

>>> b = bytes([0, 0, 1, 0, 2, 0, 3, 0, 0])
>>> b
b'\x00\x00\x01\x00\x02\x00\x03\x00\x00'
>>> m = memview(b)
>>> m
<memory at 0x0000024A3AB9EB80>
>>> bytes(m)
b'\x00\x00\x01\x00\x02\x00\x03\x00\x00'
>>> ms = lstrip_memview(m)
>>> ms
<memory at 0x0000024A3AB9EC40>
>>> bytes(ms)
b'\x01\x00\x02\x00\x03\x00\x00'
Parameters:
  • b (memoryview) – the memoryview from which to strip leading zero bytes

  • byte (int, optional) – optionally, a leading byte value to strip instead of zero

Raises:

ValueError – if byte not in range(256)

Return type:

memoryview