Python library for general Base-N encodings.
Project Links
Meta
Author: hashberg
Requires Python: >=3.7
Classifiers
Development Status
- 4 - Beta
Programming Language
- Python :: 3.12
- Python :: 3.11
- Python :: 3.10
- Python :: 3.9
- Python :: 3.8
- Python :: 3.7
Operating System
- OS Independent
Natural Language
- English
Typing
- Typed
Bases provides a customisable, parametric implementation of several common styles of Base-N encoding, covering all cases appearing in the multibase specification (except for proquints).
Install
You can install the latest release from PyPI as follows:
$ pip install --upgrade bases
Usage
We suggest you import bases as follows:
>>> import bases
Below are some basic usage examples, to get you started: for detailed documentation, see https://bases.readthedocs.io/
Base encoding objects
>>> from bases import base32
>>> base32
FixcharBaseEncoding(
StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
case_sensitive=False),
pad_char='=', padding='include')
Encoding
>>> b = bytes([70, 98, 190, 187, 66, 224, 178]) >>> base32.encode(b) 'IZRL5O2C4CZA===='
Decoding
>>> s = 'IZRL5O2C4CZA====' >>> base32.decode(s) b'Fb\xbe\xbbB\xe0\xb2' >>> list(base32.decode(s)) [70, 98, 190, 187, 66, 224, 178]
Case/padding variations
>>> b = bytes([70, 98, 190, 187, 66, 224, 178])
>>> base32.encode(b)
'IZRL5O2C4CZA===='
>>> base32lower = base32.lower()
>>> base32lower
FixcharBaseEncoding(
StringAlphabet('abcdefghijklmnopqrstuvwxyz234567',
case_sensitive=False),
pad_char='=', padding='include')
>>> base32lower.encode(b)
'izrl5o2c4cza===='
>>> base32nopad = base32.nopad()
>>> base32nopad.encode(b)
'IZRL5O2C4CZA'
Case sensitivity variations
>>> s = 'IZRL5O2C4CZA===='
>>> base32lower.decode(s)
b'Fb\xbe\xbbB\xe0\xb2'
>>> base32lower_casesensitive = base32lower.with_case_sensitivity(True)
>>> base32lower_casesensitive.decode(s)
bases.encoding.errors.NonAlphabeticCharError: Invalid character 'I'
encountered for alphabet StringAlphabet('abcdefghijklmnopqrstuvwxyz234567').
Custom base encodings
>>> base4 = bases.make("0123", kind="zeropad-enc", block_nchars=4)
>>> base4
ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4)
API
For the full API documentation, see https://bases.readthedocs.io/
Contributing
Please see CONTRIBUTING.md.
License
0.3.0
Dec 18, 2023
0.2.1
Dec 30, 2021
0.2.0.post1
Dec 13, 2021
0.1.1.post5
Dec 13, 2021
0.1.1.post3
Dec 12, 2021
0.1.0.post2
Oct 23, 2021
0.1.0.post1
Oct 20, 2021
0.1.0
Oct 20, 2021
0.0.0
Oct 20, 2021