Skip to main content

bases: a Python library for general Base-N encodings.

Generic badge PyPI version shields.io PyPI status Checked with Mypy Python package standard-readme compliant

Table of Contents

Install

You can install this package with pip:

pip install bases

Usage

The core functionality of the library is performed by the encode and decode methods of base encodings, instances of BaseEncoding (or, more precisely, of its concrete subclasses).

Common encodings are associated to pre-defined constants:

>>> from bases import base32
>>> base32
FixcharBaseEncoding(
    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                   case_sensitive=False),
    pad_char='=', padding='include')

The encode method can be used to encode bytestrings into strings:

>>> b = bytes([70, 98, 190, 187, 66, 224, 178])
>>> base32.encode(b)
'IZRL5O2C4CZA===='

The decode method can be used to decode strings into bytestrings:

>>> s = 'IZRL5O2C4CZA===='
>>> base32.decode(s)
b'Fb\xbe\xbbB\xe0\xb2'
>>> list(base32.decode(s))
[70, 98, 190, 187, 66, 224, 178]

The get(name), has(name), make(...) and table(prefix="") functions provide an interface for the programmatic management of encodings. The get(name) function can be used to obtain an existing encoding by name:

>>> import bases
>>> bases.get("base32")
FixcharBaseEncoding(
    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                   case_sensitive=False),
    pad_char='=', padding='include')

The make(...) function can be used to create a new encoding from a given alphabet, encoding kind and options:

>>> bases.make("0123", kind="zeropad-enc", block_nchars=4, name="base4")
ZeropadBaseEncoding(StringAlphabet('0123'), block_nchars=4)

The optional keyword argument name can be used to register with the library for future retrieval. The has(name) function can be used to check whether an encoding exists by a given name:

>>> bases.has("base4")
True

The table(prefix="") function can be used to iterate through the existing encoding, optionally filtering by name prefix:

>>> dict(bases.table(prefix="base32"))
{'base32':      FixcharBaseEncoding(
                    StringAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
                                   case_sensitive=False),
                    pad_char='=', padding='include'),
 'base32hex':   FixcharBaseEncoding(
                    StringAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV',
                                   case_sensitive=False),
                    pad_char='=', padding='include'),
 'base32z':     FixcharBaseEncoding(
                    StringAlphabet('ybndrfg8ejkmcpqxot1uwisza345h769',
                                   case_sensitive=False))
}

For further information, please refer to the API documentation.

API

The API documentation for this package is automatically generated by pdoc.

Contributing

Please see the contributing file.

License

MIT © Hashberg Ltd.

Metadata

Release files for bases 0.2.0.post1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bases 0.2.0.post1
File Size Uploaded
bases-0.2.0.post1.tar.gz 115.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bases 0.2.0.post1
File Interpreter ABI Platform
bases-0.2.0.post1-py3-none-any.whl Python 3 none any Details

Total release size: 149.4 kB

Release files / bases-0.2.0.post1.tar.gz

Download URL bases-0.2.0.post1.tar.gz
Size 115.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c1290ace9f9565648f754c6dd972fd7aec095e00af6857e96a8929ac51fffb11
BLAKE2b-256 checksum
How to use checksums
441623ef2fe5f5261f7b560b1e98889e5dabbd19caf847702eb0ba267256adb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

Release files / bases-0.2.0.post1-py3-none-any.whl

Download URL bases-0.2.0.post1-py3-none-any.whl
Size 34.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d27d6516bb0b38085a372df0409694c83078bf5f7bfef78f68d57ece527be72d
BLAKE2b-256 checksum
How to use checksums
0f49e13efb525c022a1f5449d05a7fcead90b5a4b9a5dd247d0ad137b9ab8935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.1

2 release files

This release

0.2.0.post1 This release

2 release files

0.1.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page