py-multicodec

1.0.0 · active · verified Thu Apr 16

Multicodec is a self-describing multiformat that wraps other formats with a tiny bit of self-description. A multicodec identifier is a varint and the code identifying the following data. This Python implementation provides functions for adding and removing prefixes, and for managing type-safe codec handling using `Code` objects. It is currently at version 1.0.0, released after a significant development period.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates adding and removing multicodec prefixes, retrieving codec names, and using the `Code` object for type-safe codec handling and listing all supported codecs.

from multicodec import add_prefix, remove_prefix, get_codec, Code, known_codes
from multicodec.code_table import SHA2_256

# Basic prefix operations
prefixed_data = add_prefix('sha2-256', b'Some raw data')
print(f"Prefixed data: {prefixed_data.hex()}")

raw_data = remove_prefix(prefixed_data)
print(f"Raw data: {raw_data}")

codec_name = get_codec(prefixed_data)
print(f"Codec name: {codec_name}")

# Type-safe Code management
sha2_256_code = SHA2_256
print(f"SHA2-256 Code: {sha2_256_code} (int: {int(sha2_256_code)}, str: {str(sha2_256_code)})")

code_from_string = Code.from_string("sha2-256")
print(f"Code from string: {code_from_string}")

all_known_codes = known_codes()
print(f"Number of known codes: {len(all_known_codes)}")

view raw JSON →