Skip to main content

Python bindings for Ktav — a plain configuration format. No quotes, no commas, dotted keys.

Project description

ktav (Python)

Python bindings for Ktav — a plain configuration format. JSON-shape, no quotes, no commas, dotted keys. Powered by Rust under the hood.

Languages: English · Русский · 简体中文

Specification: this package implements Ktav 0.1. The format is versioned and maintained independently of this package — see ktav-lang/spec for the formal document.


Install

pip install ktav

Wheels are published for every major platform and every supported Python version:

  • Linux (manylinux + musllinux) — x86_64, aarch64
  • macOSx86_64, arm64 (Apple Silicon)
  • Windowsx64, arm64

Python 3.9+ is required. The wheels target the stable ABI (abi3-py39), so a single wheel per platform serves every supported CPython release.

If no prebuilt wheel matches your platform, pip falls back to the source distribution and compiles it locally — you need a Rust toolchain (rustup) and the Python development headers.

Quick start

Parse — read typed fields straight off the dict

import ktav

src = """
service: web
port:i 8080
ratio:f 0.75
tls: true
tags: [
    prod
    eu-west-1
]
db.host: primary.internal
db.timeout:i 30
"""

cfg = ktav.loads(src)

service: str = cfg["service"]
port:    int = cfg["port"]
ratio: float = cfg["ratio"]
tls:    bool = cfg["tls"]
tags: list[str] = cfg["tags"]
db_host:    str = cfg["db"]["host"]
db_timeout: int = cfg["db"]["timeout"]

Walk — dispatch on the runtime type

for k, v in cfg.items():
    if v is None:              kind = "null"
    elif isinstance(v, bool):  kind = f"bool={v}"   # bool first — True is also an int!
    elif isinstance(v, int):   kind = f"int={v}"
    elif isinstance(v, float): kind = f"float={v}"
    elif isinstance(v, str):   kind = f"str={v!r}"
    elif isinstance(v, list):  kind = f"array({len(v)})"
    elif isinstance(v, dict):  kind = f"object({len(v)})"
    print(f"{k} -> {kind}")

Build & render — construct a document in code

doc = {
    "name": "frontend",
    "port": 8443,
    "tls": True,
    "ratio": 0.95,
    "upstreams": [
        {"host": "a.example", "port": 1080},
        {"host": "b.example", "port": 1080},
    ],
    "notes": None,
}
text = ktav.dumps(doc)
# name: frontend
# port:i 8443
# tls: true
# ratio:f 0.95
# upstreams: [
#     { host: a.example  port:i 1080 }
#     { host: b.example  port:i 1080 }
# ]
# notes: null

A complete runnable version lives in examples/basic.py.

Four entry points mirror the standard library json module:

Function Purpose
ktav.loads(s) Parse a Ktav string (or UTF-8 bytes).
ktav.dumps(obj) Serialise a native Python value.
ktav.load(fp) Parse from a file-like object.
ktav.dump(obj, fp) Serialise to a file-like object.

load / dump accept both text-mode and binary-mode files.

Type mapping

Ktav Python
null None
true / false bool
:i <digits> int
:f <number> float
bare scalar str
[ ... ] list
{ ... } dict

Ktav keeps "no magic types" — a bare port: 8080 stays a string at the parser level. Use the typed markers :i / :f when you want numbers, or type-coerce at the application layer.

dict preserves insertion order (Python 3.7+ guarantee), matching the ordered-object semantics of Ktav.

Serialisation is the inverse:

  • Python int:i marker (including arbitrary-precision bigints).
  • Python float:f marker (decimal point always present; NaN / ±Infinity are rejected — Ktav 0.1.0 does not represent them).
  • Python tuple is accepted as an array, for symmetry with list.
  • Non-str keys in a dict raise KtavEncodeError.

Key escaping

Since spec 0.6.0 a literal . or : inside a key segment is written with a backslash:

a\.b: v        # key is the single segment "a.b" -> {"a.b": "v"}
a\:b: v        # key contains a colon            -> {"a:b": "v"}
x.y\.z: v      # split on the first dot only     -> {"x": {"y.z": "v"}}

A literal backslash in a key is \\.

Errors

import ktav

try:
    ktav.loads("x: [")
except ktav.KtavDecodeError as e:
    print("decode:", e)

try:
    ktav.dumps({"v": float("nan")})
except ktav.KtavEncodeError as e:
    print("encode:", e)

# Catching the base class catches either.
try:
    ktav.loads("a: 1\na: 2")
except ktav.KtavError:
    ...
Exception Raised by Base
KtavError (base) Exception
KtavDecodeError loads / load KtavError
KtavEncodeError dumps / dump KtavError

Philosophy

Ktav is intentionally small. Its five design principles (from spec/CONTRIBUTING.md):

  1. Locality — a line's meaning does not depend on another line.
  2. One sentence — any new rule fits in one sentence of the spec.
  3. No whitespace sensitivity (line breaks aside).
  4. No magic types — the format never decides "8080" means a number.
  5. Explicit over clever:: is verbose on purpose.

The Python bindings honour this: they add no schema inference, no auto-casting, no defaulting. If you want typing, you do it at the boundary with your own tool — pydantic, dataclasses, attrs — against the native Python structures this library returns.

Other Ktav implementations

  • spec — specification + conformance suite
  • rust — reference Rust crate (cargo add ktav); these Python bindings are a thin PyO3 wrapper around it
  • csharp — C# / .NET (dotnet add package Ktav)
  • golang — Go (go get github.com/ktav-lang/golang)
  • java — Java / JVM (io.github.ktav-lang:ktav on Maven Central)
  • js — JS / TS (npm install @ktav-lang/ktav)
  • php — PHP (composer require ktav-lang/ktav)

Versioning

This package follows Semantic Versioning with the pre-1.0 convention that a MINOR bump is breaking. The package version and the ktav crate version move together. ktav.__spec_version__ reports the Ktav format version this binding supports.

Development

See CONTRIBUTING.md for the dev setup, test layout, and the contribution workflow.

Support the project

The author has many ideas that could be broadly useful to IT worldwide — not limited to Ktav. Realizing them requires funding. If you'd like to help, please reach out at phpcraftdream@gmail.com.

License

MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.

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

ktav-0.6.0.tar.gz (56.6 kB view details)

Uploaded Source

Built Distributions

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

ktav-0.6.0-cp39-abi3-win_arm64.whl (204.6 kB view details)

Uploaded CPython 3.9+Windows ARM64

ktav-0.6.0-cp39-abi3-win_amd64.whl (212.5 kB view details)

Uploaded CPython 3.9+Windows x86-64

ktav-0.6.0-cp39-abi3-musllinux_1_2_x86_64.whl (510.7 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

ktav-0.6.0-cp39-abi3-musllinux_1_2_aarch64.whl (462.7 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

ktav-0.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (299.0 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

ktav-0.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (285.2 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

ktav-0.6.0-cp39-abi3-macosx_11_0_arm64.whl (277.2 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

ktav-0.6.0-cp39-abi3-macosx_10_12_x86_64.whl (290.4 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file ktav-0.6.0.tar.gz.

File metadata

  • Download URL: ktav-0.6.0.tar.gz
  • Upload date:
  • Size: 56.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0.tar.gz
Algorithm Hash digest
SHA256 c13a8815e9e3cc714f558ebed5af0b8ebb691347a1abb4135a8061df625261ac
MD5 b292a9b1b84105689678c075d1d521e8
BLAKE2b-256 0b42393c183841183b1bfa6b0af5ae4c7dd8d9ff5fd5958013283e305f1b2338

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0.tar.gz:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-win_arm64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-win_arm64.whl
  • Upload date:
  • Size: 204.6 kB
  • Tags: CPython 3.9+, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 7dc86f874fec63c2f5509b8e3f45f97b805feb616b5ce6419db2ab1000c77d74
MD5 caacf5cb8f8568adf36c174ca2c684f8
BLAKE2b-256 e1cec58e5ac035a5a3f8089b51c7d82f6393670005bf4dfdfc75719460ee1cad

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-win_arm64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 212.5 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0b7acc62e5935da29ab1cf2025f0a3dc67e934b217accbda091362ceeafd6a83
MD5 b97f184d16d4fd19c24da465c20e88f5
BLAKE2b-256 bfe1374d0f556f8d455c057abe3a9ebb8c0f5352c16ba28de811aae679344bbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 510.7 kB
  • Tags: CPython 3.9+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 3e60b0fe8003126a0df004c965f8c6622f3a832a93b1ec55c03d46e3bfc6d1b2
MD5 28c8fd376e82eceb5413983243d9ce57
BLAKE2b-256 65f662f852675d535abd4cb774f6f767e7e0a24dfb1d6e1e3827bd7abf35794a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-musllinux_1_2_x86_64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 462.7 kB
  • Tags: CPython 3.9+, musllinux: musl 1.2+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 21033de8f1a2ae479c0f5948d9584d3b5ed149ef5f10021957e80a7ca7b52208
MD5 d1588b28e8e4ae78c1bd813944531059
BLAKE2b-256 c6147d197a2cd20982ab8519e9fd42fc322647f04b25276915fe08dd7538dee4

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-musllinux_1_2_aarch64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ktav-0.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1fcd6018070c4049d910ffbbec00d656b999a43ac2312eaf09707f101ea893d1
MD5 542e90a86efccdba306dd6275beb7487
BLAKE2b-256 13b6888f3ad62f6e00de9970dd436b4d79b4476154a6b60b8597c36123d4b71b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ktav-0.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5e172fd0c4b568952870a6720b42f43f07be8bd2df5b12a15519d4d373699b58
MD5 e6883252a384787faa2624d1d3cef6a5
BLAKE2b-256 634ef7786788f338f97a031a9371fe933c0ead88b8e76f74e1d458daa2e9e809

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 277.2 kB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 57e5869b82fd90b6d019a7464d7d98de6a32c778ddcd9955fdac1f60e725f45d
MD5 031f4760f2394046da1687c2ce9fbeb3
BLAKE2b-256 4258ddf04ce4fc2517ee44844406f428861b1a72efcb3c374388e22ff77305d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ktav-0.6.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: ktav-0.6.0-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 290.4 kB
  • Tags: CPython 3.9+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ktav-0.6.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 47ea8e60f26ff8f291a660ba0da6bff8427c383339f3e8d43bc76b32256de70d
MD5 277588ca31effda8f111a8f5708ef8f1
BLAKE2b-256 c1f93874eb179d668a256b0b29eccd48ee5a7a3f4b24e6a4d366757c4725555e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on ktav-lang/python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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