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()
assert User.from_cbf(cbf) == 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.1.tar.gz (141.5 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.1-cp312-cp312-macosx_11_0_arm64.whl (551.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: surp-1.0.1.tar.gz
  • Upload date:
  • Size: 141.5 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.1.tar.gz
Algorithm Hash digest
SHA256 5f9fe8af1d281af2d001a4140bef2ff17055e2921842a73d3e060846ee1c6e46
MD5 b9696908f91b873f36955655820599cd
BLAKE2b-256 2eb31e10ac1cd5c9eb04616c3560057d56aa36fa337e51b57d2d5fbe8ae30a6c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for surp-1.0.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 973115e2747783627ba9b497144be7e40d8753d6f82351739322eba2f12a1e45
MD5 29dca97a54eb8966058cec502e315a5c
BLAKE2b-256 38f1dc9ff55b50e4d03bd900020019f8cfb825a1a2a0d317d376669d20014cba

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