multiformats.multibase.raw
Implementation of raw data encodings used by multibase encodings.
The majority of the encodings is provided by the bases library,
as instances of its BaseEncoding
class. The following custom encodings are also implemented:
multibase identity
multibase proquints
Core functionality is provided by the get
and exists
functions,
which can be used to check whether a raw encoding with given name is known, and if so to get the corresponding object:
>>> from multiformats.multibase import raw_encoding
>>> raw_encoding.exists("base10")
True
>>> raw_encoding.get("base10")
ZeropadBaseEncoding(StringAlphabet('0123456789'))
The raw encoding objects have CustomEncoding.encode
and
CustomEncoding.decode
methods that can be used to convert between
bytestrings and strings (not including the multibase code):
>>> base16 = raw_encoding.get("base16")
>>> base16.encode(bytes([0xAB, 0xCD]))
'abcd'
>>> base16.decode('abcd')
b'\xab\xcd'
CustomEncoding
exists
get
- get(name)[source]
Gets the raw encoding with given name.
Example usage:
>>> raw_encoding.get("base16") ZeropadBaseEncoding( StringAlphabet('0123456789abcdef', case_sensitive=False), block_nchars=2)
register
- register(name, enc, *, overwrite=False)[source]
Registers a raw encoding by name.
Example usage:
>>> from bases import base45 >>> raw_encoding.register("base45upper", base45) >>> raw_encoding.get("base45upper") BlockBaseEncoding( StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:', case_sensitive=False), block_size={1: 2, 2: 3}, reverse_blocks=True)
- Parameters:
name (
str
) – the name for the encoding being registeredenc (
BaseEncoding
orCustomEncoding
) – the raw encoding being registeredoverwrite (
bool
; default =False
) – whether to overwrite an existing encoding with same name (defaultFalse
)
- Raises:
ValueError – if
overwrite
isFalse
and a raw encoding with the same name already exists.- Return type: