Skip to main content

compact-encoding-python

Pure-Python port of compact-encoding, wire-compatible with the JavaScript implementation. All multi-byte values are little-endian.

The public surface is a deliberate subset of the JS reference - the codecs needed by hyperschema-python and bare-rpc-python. Additive JS codecs (e.g. raw, any, bigint, the IP codecs) are not ported; they can be added later without breaking the wire format.

Install

pip install git+https://github.com/holepunchto/compact-encoding-python

Usage

import compact_encoding as cenc

data = cenc.encode(cenc.uint, 42)  # -> bytes
n = cenc.decode(cenc.uint, data)  # -> 42

Codecs

Every codec exposes preencode, encode, and decode. Pass one to the encode/decode helpers, or drive it directly through a State (see Three-phase API).

Scalars

Codec Python type Notes
uint int varint, 0 .. 2**53 - 1
int int zigzag varint
bool bool single byte
buffer bytes length-prefixed
utf8 (alias string) str length-prefixed UTF-8
json any length-prefixed UTF-8 JSON

Fixed-width numbers

Little-endian, fixed size. Unsigned: uint8, uint16, uint24, uint32, uint40, uint48, uint56. Signed: int24, int40, int48, int56. IEEE 754 floats: float32, float64.

Fixed-width buffers

fixed(n) encodes exactly n bytes (no length prefix). fixed32 and fixed64 are fixed(32) and fixed(64) - 32- and 64-byte buffers, not bits.

Composites

  • array(codec) - a length-prefixed list of codec values (decode rejects lengths above 0x100000).
  • record(key_codec, value_codec) - a length-prefixed dict.
  • frame(codec) - a nested value with its encoded byte length written ahead of it.

Encode / decode

encode(codec, value) -> bytes and decode(codec, data) -> value are the one-shot helpers.

Three-phase API

To pack several values into one buffer, drive a State directly: preencode every value to measure the buffer, allocate(), then encode.

import compact_encoding as cenc

state = cenc.State()
cenc.uint.preencode(state, 42)
cenc.utf8.preencode(state, "hi")
state.allocate()
cenc.uint.encode(state, 42)
cenc.utf8.encode(state, "hi")

state = cenc.State(state.buffer)
cenc.uint.decode(state)  # -> 42
cenc.utf8.decode(state)  # -> "hi"

A State holds start, end, buffer, and a remaining property, plus allocate() and rewind(). Codec is a typing Protocol for writing your own.

Errors

CompactError is the base error; OutOfBounds (a subclass) is raised when a decode reads past the end of the buffer. Out-of-range encodes raise ValueError.

License

Apache-2.0

Download files

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

Source Distribution

compact_encoding-0.0.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

compact_encoding-0.0.1-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file compact_encoding-0.0.1.tar.gz.

File metadata

  • Download URL: compact_encoding-0.0.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for compact_encoding-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c25f3f3a2fbd409f15999ce91b79e4fedd61b915db82c264d01c38b3481be5c3
MD5 82398cb0e8ccd42fdd0974e429232a2a
BLAKE2b-256 c1c507af1400b83f9b111ea6acd58ada2ec8ade69f5035e75fe72d71a979ceb4

See more details on using hashes here.

File details

Details for the file compact_encoding-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for compact_encoding-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82a93c9073ecb56d27ee84f122611d18415850cad2a9bbfbba954d6dbd5d9044
MD5 708c77b2ebbfb389ccf4e9bd6700762f
BLAKE2b-256 0636e28a7f00b83e74e307258014f05c3280a30addc8911c9f3fde913cfba14d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page