Skip to main content

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

Project description

ktav (Python)

PyPI CI License: MIT OR Apache-2.0 Playground

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

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

Playground: convert JSON / YAML / TOML / INI ⇄ Ktav in your browser at ktav-lang.github.io.

Specification: this package implements Ktav. 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: 8080
ratio: 0.75
tls: true
tags: [
    prod
    eu-west-1
]
db.host: primary.internal
db.timeout: 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: 8443
# tls: true
# ratio: 0.95
# upstreams: [
#     { host: a.example  port: 1080 }
#     { host: b.example  port: 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
bare integer int
bare decimal float
other scalar str
[ ... ] list
{ ... } dict

Ktav types numbers by lexical form — a bare port: 8080 is an int, ratio: 0.5 a float, and anything that isn't a bare number stays a str. Force a numeric-looking value to stay a string with :: (zip:: 01007).

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

Serialisation is the inverse:

  • Python int → bare integer (including arbitrary-precision bigints).
  • Python float → bare decimal (decimal point always present; NaN / ±Infinity are rejected — Ktav 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.1.tar.gz (57.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.1-cp39-abi3-win_arm64.whl (204.9 kB view details)

Uploaded CPython 3.9+Windows ARM64

ktav-0.6.1-cp39-abi3-win_amd64.whl (212.9 kB view details)

Uploaded CPython 3.9+Windows x86-64

ktav-0.6.1-cp39-abi3-musllinux_1_2_x86_64.whl (511.0 kB view details)

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

ktav-0.6.1-cp39-abi3-musllinux_1_2_aarch64.whl (463.0 kB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

ktav-0.6.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (299.3 kB view details)

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

ktav-0.6.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (285.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

ktav-0.6.1-cp39-abi3-macosx_11_0_arm64.whl (277.5 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

ktav-0.6.1-cp39-abi3-macosx_10_12_x86_64.whl (290.7 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: ktav-0.6.1.tar.gz
  • Upload date:
  • Size: 57.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.1.tar.gz
Algorithm Hash digest
SHA256 058f4a579c6a18453a3f7db8915e0b87a3c64966ffff72a82f5eac03e27862b9
MD5 79b8689a7fb117e12a020a7f80fe4810
BLAKE2b-256 8f9923c73a0585e30a66d57ca82233b7976d0f3a611b01e13bddb406050ecc8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1.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.1-cp39-abi3-win_arm64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-win_arm64.whl
  • Upload date:
  • Size: 204.9 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.1-cp39-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 3b7522d65fdc88e5c653092abfbf6fca4c753ab9bb2a63375d3e04ee748913b7
MD5 c3b77362702ed0bf28779a8ee5bf49b8
BLAKE2b-256 2a9479832e4e6d765aac70eaf1a146c1adce18907b3175c71ddf1179a7572112

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 212.9 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.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 da50654309e388fdb5b133227d7a469a1b0e60995b2b1b4625c42ebed6632656
MD5 c42a1750bead6c24fc392c8474200b14
BLAKE2b-256 8c53963344c5c50fdc6dbb0d042154c97dea3052c4ec333f7565be7ebde1b94b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 511.0 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.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4f4517c3d2e91866ef73eacd967bb1527d19e5c676125811f6f3b69f728b0f36
MD5 3a348baff506b409ee9f293bb037fa99
BLAKE2b-256 d60b46074ff1a0431c6300120d4cb766d922e37af37349258766f18a7f924087

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-musllinux_1_2_aarch64.whl
  • Upload date:
  • Size: 463.0 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.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 e5e5026d0819e3eebdc9f5b11324ef2c79e608635f30e3c8ada90071dc1e5b71
MD5 0bc594231fb76aa3fce87505b98d9ff5
BLAKE2b-256 c69286a5db05127844660a51a1bf0a8740e86a918623bd1cd74f6e86a4288cb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ktav-0.6.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 296c39f61e1af98d8e17885a6184b1d6a16c1d2b0ab738551b10e19622787390
MD5 41beca883c38b8e35760c3e9f4b54a43
BLAKE2b-256 04137e7d1f0a745e343e0a846177da3cc0517de6062ea5f4848eca6b081e358b

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ktav-0.6.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e156ebabc5145b08998a346f92ed82c46f04634eb19d4ce9590442d3c6024022
MD5 a692d2e0be232a34ac63824b2b7629ce
BLAKE2b-256 90146238802632dbf0101994767a4c2806852719113d27f1ded66d49f0424858

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 277.5 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.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4c009bc4d492e901abd0f6468d15578a51e14ed7e5fb9bbd49c12365506a82e
MD5 45f2dcb314474999be4e1cf22d3931d3
BLAKE2b-256 cc2c0170cddddad1833cf84599336c2632e0696b3f9f4c2dbd4ac7b62dee310f

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: ktav-0.6.1-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 290.7 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.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 dff27503427964f4da548c9d98318c6dc84284f62f20307964cbce772ec6d44e
MD5 537c7e75e3cc8c35f21f9437104aa76a
BLAKE2b-256 fe6c4da3829ee7f5da527db06c7b2c1ed73b1101858ef66498e90e77f22254ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for ktav-0.6.1-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