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:
- property alphabet
The encoding alphabet.
Example usage:
>>> encoding.base32.alphabet StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', case_sensitive=False)
- Return type:
- property base
The base for this encoding (the length of the alphabet).
Example usage:
>>> encoding.base32.base 32
- Return type:
- canonical_bytes(b)[source]
Returns a canonical version of the bytestring
b
: this is the bytestring obtained by first encodingb
and then decoding it.(This method is overridden by subclasses with more efficient implementations.)
- canonical_string(s)[source]
Returns a canonical version of the string
s
: this is the string obtained by first decodings
and then encoding it.(This method is overridden by subclasses with more efficient implementations.)
- property case_sensitive
Determines whether the decoder is case sensitive.
Example usage:
>>> encoding.base32.case_sensitive False
- Return type:
- 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:
- 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:
- 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:
- 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'}
- 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:
- 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:
- 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:
case_sensitive (
bool
) – case sensitivity for the new encodingself (
BaseEncodingSubclass
) –
- Return type:
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 bytesbyte (
int
, optional) – optionally, a leading byte value to strip instead of zero
- Raises:
ValueError – if
byte not in range(256)
- Return type: