Skip to main content

Python library for general Base-N encodings.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bases-0.1.0.post2.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

bases-0.1.0.post2-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file bases-0.1.0.post2.tar.gz.

File metadata

  • Download URL: bases-0.1.0.post2.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for bases-0.1.0.post2.tar.gz
Algorithm Hash digest
SHA256 2494144dabcf97e1525ff329776c9cb22b3ccd5b565b4973fbfa7beff94e3424
MD5 7102fcf364a17c31d5a4de90e1fb17d2
BLAKE2b-256 4f0dfe56fc98fca83262829802dd1891bdc022a6a80b8b062ed7071a206533ee

See more details on using hashes here.

File details

Details for the file bases-0.1.0.post2-py3-none-any.whl.

File metadata

  • Download URL: bases-0.1.0.post2-py3-none-any.whl
  • Upload date:
  • Size: 33.5 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for bases-0.1.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 f4ec6a67dd10d1e4f6ffefa47b7e4f89f5abfcb3032bd3363204d8bbb47b4c68
MD5 e05c52400e5140ab9a638f184e24a3e5
BLAKE2b-256 0dd1162b35d13b4e96ee5ff3200f076425f15e84cd8802abb2e3f3ed1c946d94

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page