Skip to content

About

Multicodec implementation in Python

Resources

Contributing

Stars

19 stars

Watchers

10 watching

Forks

Repository files navigation

py-multicodec

https://github.com/multiformats/py-multicodec/actions/workflows/tox.yml/badge.svg?branch=master Documentation Status

Multicodec implementation in Python

multicodec is a self-describing multiformat, it wraps other formats with a tiny bit of self-description.

A multicodec identifier is both a varint and the code identifying the following data, this means that the most significant bit of every multicodec code is reserved to signal the continuation.

You can check the table here for the list of supported codecs by py-multicodec.

Installation

$ pip install py-multicodec

Sample Usage

>>> from multicodec import add_prefix, remove_prefix, get_codec
>>> # adding a prefix to existing data
>>> add_prefix('sha2-256', b'EiC5TSe5k00')
b'\x12EiC5TSe5k00'
>>> # removing prefix from prefixed data
>>> remove_prefix(b'\x12EiC5TSe5k00')
EiC5TSe5k00
>>> # get codec used to prefix the prefixed data
>>> get_codec(b'\x12EiC5TSe5k00')
'sha2-256'

Code Management

In addition to the basic prefix operations, py-multicodec provides type-safe codec management functionality:

>>> from multicodec import Code, known_codes
>>> from multicodec.code_table import SHA2_256, DAG_CBOR

>>> # Use named constants for type-safe codec handling
>>> code = SHA2_256
>>> str(code)
'sha2-256'
>>> int(code)
18

>>> # Create Code from string (name or hex)
>>> code = Code.from_string("sha2-256")
>>> code = Code.from_string("0x12")  # hex also works

>>> # List all known codecs
>>> all_codes = known_codes()
>>> len(all_codes)
460

Updating the lookup table

Updating the lookup table is done with a script. The authoritative table is vendored in the multicodec-spec git submodule (table.csv). After a fresh clone, run git submodule update --init --recursive so the submodule and spec compliance test are available.

To refresh the registry from upstream, bump the submodule pin and regenerate multicodec/constants.py:

$ git -C multicodec-spec fetch origin
$ git -C multicodec-spec checkout origin/master
$ git add multicodec-spec
$ ./tools/update-table.py < multicodec-spec/table.csv

Alternatively, pipe the latest upstream CSV directly:

$ curl -X GET https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv | ./tools/update-table.py

Commit the updated constants.py together with the new submodule commit SHA.

About

Multicodec implementation in Python

Resources

Contributing

Stars

19 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages