Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 1.69 KB

File metadata and controls

70 lines (52 loc) · 1.69 KB

Compact · Python

Encoding, section decoding, schema helpers, and a command-line interface.

Install

From this directory:

python -m pip install -e .

For development:

python -m pip install -e ".[dev]"
python -m pytest

The codec has no third-party runtime dependencies. Pytest is a development dependency; Hatchling builds the package.

Usage

from compact import (
    decode_stream, encode_bitfield, encode_small_ints,
    encode_text, stream, to_base64url,
)

code = to_base64url(stream([
    encode_text("NODE-A7F3"),
    encode_small_ints([5, 2, 1], width=3),
    encode_bitfield([True, False, True, True]),
]))

assert code == "A_oE8QnJ6IilqCboxmjHUUuw"
name, choices, flags = decode_stream(code)
assert name.as_text() == "NODE-A7F3"
assert choices.as_ints() == (5, 2, 1)
assert flags.as_bitfield() == (True, False, True, True)

CLI

python -m compact encode --text "hi"
python -m compact encode --ints 3,1,4,1,5
python -m compact decode BEhcQ0NI

Installing the package also exposes the equivalent compact command. The encode command groups repeated options by section type: text, fixed bits, dynamic chunks, and integers, in that order. Use the Python API when section order differs from that grouping.

Examples

python examples/settings_token.py
python examples/device_beacon.py

settings_token.py maps application choices and flags into a short text token and recovers the settings. device_beacon.py combines an identifier, positive counters, and health flags.

The examples demonstrate schema mapping, not complete input validation. See the input contract and wire specification.