Skip to main content

Native Rust-backed Surp encoder/decoder with RFC-001 CTN/CBF/CQL support

Project description

Surp Python

Rust-backed Python bindings for Surp, a compact binary serialization format with a human-readable text notation and an additive RFC-001 CTN/CBF/CQL path.

The Python distribution is named surp and is backed by the PyO3 extension module surp._surp_native.

Install

pip install surp

From a checkout:

cd surp-python
maturin develop --release
python -m pytest tests/ -v

v1 API

import surp

payload = {
    "name": "Alice",
    "age": 30,
    "active": True,
    "avatar": b"\x01\x02\x03",
}

data = surp.dumps(payload, dedup=True, sort_keys=True)
assert surp.loads(data) == payload

Supported inputs are None, bool, int, float, str, bytes, list, tuple, and dictionaries with string keys. Tuples decode as lists.

Public helpers:

  • dumps, loads, dump, load
  • encode, decode, encode_to_file, decode_from_file
  • parse_text, pretty_print
  • to_value, loads_value, parse_text_value, SurpValue
  • Encoder, SurpDecoder

Use loads_value() or parse_text_value() when you want native-backed attribute access instead of plain Python containers:

view = surp.loads_value(data)
assert view["name"].value == "Alice"
assert view["tags"][0].value == "admin"

RFC-001 Helpers

from surp import rfc001

cbf = rfc001.compile_ctn('User\n  name = "Alice"', alignment=4)
decoded = rfc001.decode_cbf(cbf)

assert decoded["header"]["magic"] == "SURP"
assert rfc001.query_cbf(cbf, ".name", as_ctn=True) == ['"Alice"']

surp.rfc001 exposes CTN parsing/normalization, CTN-to-CBF compilation, CBF decoding, CBF-to-CTN formatting, and baseline CQL path queries.

For IDE-friendly RFC access, use the model helpers:

doc = rfc001.parse_ctn_model('User\n  name = "Alice"')
user = doc.effective_root()
assert user["name"].scalar_value == "Alice"

RFC-001 Model Schemas

surp.model provides a declarative validation layer for RFC-001 products and documents. Model classes use explicit RFC-001 type markers and encode through surp.rfc001.compile_ctn().

from surp.model import Field, SurpModel
from surp.model.types import Bool, Int64, SeqOf, Str


class User(SurpModel):
    name: Str = Field(required=True)
    age: Int64 = Field(required=False, default=0)
    active: Bool = Field(required=True)
    tags: SeqOf[Str] = Field(required=False, default_factory=list)


user = User(name="Alice", active=True, tags=["admin"])
cbf = user.to_cbf()
data = user.to_surp()
assert User.from_cbf(cbf) == user
assert User.from_surp(data) == user

Typing

The wheel includes .pyi stubs and py.typed for type checkers.

License

Licensed under either MIT or Apache-2.0, at your option.

Project details


Download files

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

Source Distribution

surp-1.0.2.tar.gz (149.9 kB view details)

Uploaded Source

Built Distribution

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

surp-1.0.2-cp312-cp312-macosx_11_0_arm64.whl (560.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

Details for the file surp-1.0.2.tar.gz.

File metadata

  • Download URL: surp-1.0.2.tar.gz
  • Upload date:
  • Size: 149.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for surp-1.0.2.tar.gz
Algorithm Hash digest
SHA256 20977e55f629c86a4bb12eca7afc80842e05c91cf1821dde0cb18cd0df1b6f72
MD5 1a0db293510e83a96ada18ab9ad5da84
BLAKE2b-256 e167eb9015564f9680eb3105ce8382a13b1d4558f054263d3954a22bc162f704

See more details on using hashes here.

File details

Details for the file surp-1.0.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for surp-1.0.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 889db5144219b6e909c4433a8da7ca787faf6935efcfaf066e0a199786399e6e
MD5 0d81aeee69ef9034ef07b6313c0bcbc7
BLAKE2b-256 12b0c4ef9007627a1fc4bf7aa6e45766d41627089a8adef426f94de73fea26b2

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 Pingdom Monitoring Sentry Error logging StatusPage Status page