pamoja-codec
CBOR, JSON, and raw codecs behind one trait, and batch packing for metered links: delta and varint for integers, and a quantizer for f32 readings. One capability of pamoja, one memory-safe Rust core with bindings for TypeScript, Python, and C#.
Install
pip install pamoja-codec
from pamoja import codec
This pulls in pamoja-native, the compiled engine. pip install pamoja is the whole framework in one package.
Example
The script the test suite runs, spliced here as it ran.
From bindings/python/guides/codec.py:
import json
import math
from pamoja.codec import Quantizer, from_cbor, pack_samples, to_cbor, unpack_samples
from pamoja.core import PamojaError
from pamoja.lora import plan_for
# The gauge reports over LoRaWAN in the US915 plan, at the slowest data rate because it
# reaches farthest. An uplink there carries only a few bytes of payload.
budget = plan_for("US915").max_payload(0).application
def fits(size: int) -> str:
return "fits one uplink" if size <= budget else "too big for one uplink"
print(f"uplink carries {budget} bytes at the slowest US915 data rate")
# One reading as the JSON a web service would take. CBOR carries the same document in
# fewer bytes, but every key name still rides along with every reading.
reading = {"depth_cm": 142.5, "air_c": -6.5, "battery_mv": 3712}
as_json = json.dumps(reading, separators=(",", ":")).encode()
cbor = to_cbor(reading)
print(f"json {len(as_json)} bytes, {fits(len(as_json))}")
print(f"cbor {len(cbor)} bytes, {fits(len(cbor))}")
print(f"cbor reads back as {json.dumps(from_cbor(cbor), separators=(',', ':'))}")
# A batch the gauge and the server agree on needs no key names. Six hourly depths, kept to
# the millimeter, pack to a count, the first depth, and five small steps.
quantizer = Quantizer(10)
depths = [142.5, 143.8, 145.2, 146.0, 145.7, 145.5]
depth_batch = quantizer.encode(depths)
depth_bytes = len(depth_batch)
print(f"depths {len(depths)} readings in {depth_bytes} bytes, {fits(depth_bytes)}")
depths_back = [f"{depth:.1f}" for depth in quantizer.decode(depth_batch)]
print(f"depths read back as {', '.join(depths_back)}")
# Battery millivolts are whole numbers already, so they pack with no scale, and a falling
# voltage packs as small as a rising one.
battery = [3712, 3709, 3705, 3702, 3698, 3695]
battery_batch = pack_samples(battery)
battery_bytes = len(battery_batch)
print(f"battery {len(battery)} readings in {battery_bytes} bytes, {fits(battery_bytes)}")
print(f"battery reads back as {', '.join(map(str, unpack_samples(battery_batch)))}")
# Heavy snowfall can swallow the sensor's echo, leaving no depth at all. The quantizer
# refuses the batch rather than send the gap as a depth.
try:
quantizer.encode([145.5, math.nan])
except PamojaError as error:
print(f"depths refused a batch with a missing depth: {error}")
The same capability in every language
| Language | Package | Reference |
|---|---|---|
| Rust | pamoja-codec |
reference, docs.rs, install |
| TypeScript | @pamoja/codec |
reference, install |
| Python | pamoja-codec |
reference, install |
| C# | Pamoja.Codec |
reference, install |
Documentation
pamoja.codecreference, every class and function in this module.- The Codecs guide, with the same example in Rust, TypeScript, and C#.
- Every capability, and the install page.
License
MIT
Metadata
Release files for pamoja-codec 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pamoja_codec-0.2.1.tar.gz | 4.9 kB | Without provenance | Details for pamoja_codec-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pamoja_codec-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for pamoja_codec-0.2.1-py3-none-any.whl |
Total release size: 10.5 kB
Release files / pamoja_codec-0.2.1.tar.gz
| Download URL | pamoja_codec-0.2.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a192888b4234c1ec09eb3ebf2629b723a11da1512f8f4d2aae10001dae45327
|
|
BLAKE2b-256 checksum How to use checksums |
776a8e3121d5cd54572d0e582447ed90530abf19e58f142ac2e3a22dde8d940c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Release files / pamoja_codec-0.2.1-py3-none-any.whl
| Download URL | pamoja_codec-0.2.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a19b565f5ad45702b8f071d5ce7f7dc9be6af7df63a4fc87963d3ca480f35c5b
|
|
BLAKE2b-256 checksum How to use checksums |
05b96a52fc63c06568d8e91df9e61642500711f63e55ead4fb71fe845b1066b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.18
|
Security for pamoja-codec 0.2.1
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.