Skip to main content

The MoaT-Codec library

% start synopsis % start main

This library packages various codecs used for MoaT protocols in a way that's reason- and stream-able.

% end synopsis

Supplied codecs

  • CBOR
  • msgpack
  • no-op
  • UTF-8
  • JSON
  • YAML

On CPython the CBOR codec is implemented in pure Python. MicroPython uses the ccbor C module instead (compiled into the firmware; moat micro prep builds it into the test interpreters). The wire format is identical; the C decoder is stricter than the pure-Python one in a few places:

  • duplicate map keys are an error instead of silently overwriting the earlier value,
  • unassigned CBOR simple values are an error instead of a Tag,
  • half/single-precision floats decode to their exact value instead of being rounded to a fixed number of significant digits,
  • the nesting depth is limited (default 32),
  • a not-well-formed byte in an incremental stream is skipped after the error is reported, so decoding can resynchronise.

WebSocket subprotocols

moat.lib.codec.SUBPROTO_CODEC maps WebSocket subprotocol tokens to get_codec names. Message layers (moat.lib.stream.CBORMsgBlk/CBORMsgBuf) consult this table when their bottom stream reports a negotiated subprotocol, and use the mapped codec instead of the codec configuration key:

Token Codec
cbor, moat-cbor std-cbor
msgpack, moat-msgpack std-msgpack
json json
yaml yaml
utf-8 utf8

A subprotocol not in this table is ignored; the codec configuration key (then std-cbor) applies. Only the std- codecs represent MoaT objects like moat.lib.path.Path; the plain codecs are for simple data.

% end main

Interface

Codec

  • encode(obj)

    Encode the object to a bytestream.

  • decode(bytes)

    Decode the given bytes to a single object.

  • feed(bytes, final=False)

    Data stream. Returns an array of objects.

    This method only exists when a sequence of encoded objects can be streamed, i.e. contains built-in message boundaries.

    if @final is True, raise an exception if there is an incomplete object in the buffer after this call.

  • getstate(), setstate(*args)

    A streaming decoder may store partially-decoded state (unprocessed buffer) here.

The list of accepted objects is implementation dependent.

Extension

Extension types vary by whether they carry binary data as in (msgpack), or objects (as in cbor).

  • binary (classvar): True if msgpack-style, False if cbor-style

  • encoder(cls, key, fn)

    Encode an object obj of type type. fn(codec, obj) must return a bytestring / an encodeable object. If the registration key is None the function must return a tuple with the key as first element.

  • decoder(key, fn)

    Add a decoder (key, data) to an object. fn(codec, data) is called with a bytestring / a decoded object, and returns whatever has been encoded.

Extensions get the codec as their first argument. They can use it to store data, e.g. references. The extension object is available as codec.ext.

Metadata

Release files for moat-lib-codec 0.4.24

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

Source distribution (sdist)

Source distribution for moat-lib-codec 0.4.24
File Size Uploaded Provenance Actions
moat_lib_codec-0.4.24.tar.gz 24.5 kB Without provenance Details for moat_lib_codec-0.4.24.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for moat-lib-codec 0.4.24
File Interpreter ABI Platform Actions
moat_lib_codec-0.4.24-py3-none-any.whl Python 3 none any Details for moat_lib_codec-0.4.24-py3-none-any.whl

Total release size: 51.6 kB

Release files / moat_lib_codec-0.4.24.tar.gz

Download URL moat_lib_codec-0.4.24.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
be8673ec0b54589d3bd1b344ea50e50dd042b1e4f4e9c71140985def169585df
BLAKE2b-256 checksum
How to use checksums
d4927669280f7a40c190bebb47cb13be1cc488346233d8238530f74f4ad7c255
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / moat_lib_codec-0.4.24-py3-none-any.whl

Download URL moat_lib_codec-0.4.24-py3-none-any.whl
Size 27.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17eb74545ec09dd610b6d15cbf2a4aaa9e1e6fe6eaf8dc0fdd5a5963523b94f2
BLAKE2b-256 checksum
How to use checksums
ae76e4edb0e05b6f145f37356a1875e3454eaf0ad5fa2ff4928b97fdea53824b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for moat-lib-codec 0.4.24

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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