Skip to main content

Token-Optimized Notation Language - A compact serialization format for LLM contexts

Project description

PyTONL

Token-Optimized Notation Language - A compact serialization format designed for LLM contexts, human readability, and perfect JSON compatibility.

PyTONL is a pure-Python implementation of the TONL format. It aims to be compatible with the official TypeScript TONL library (https://github.com/tonl-dev/tonl) and to follow the same transformation rules and semantics wherever possible.

Features

  • 🎯 Token Efficient: 32-45% smaller than JSON for LLM contexts
  • 👁️ Human Readable: Clean, tabular format for structured data
  • 🔄 Bidirectional: Perfect JSON roundtrip compatibility
  • 📊 Tabular Arrays: Reduces redundancy in uniform object arrays
  • 🎨 Smart Quoting: Minimal quoting with automatic delimiter selection
  • 📝 Type Hints: Optional type annotations for validation

Installation

pip install pytonl

For development:

pip install pytonl[dev]

Quick Start

Encoding JSON to TONL

import pytonl

data = {
    "users": [
        {"id": 1, "name": "Alice", "role": "admin"},
        {"id": 2, "name": "Bob", "role": "user"}
    ]
}

tonl_str = pytonl.encode(data)
print(tonl_str)

Output:

#version 1.0
users[2]{id,name,role}:
  1, Alice, admin
  2, Bob, user

Decoding TONL to JSON

import pytonl

tonl_str = """#version 1.0
users[2]{id,name,role}:
  1, Alice, admin
  2, Bob, user"""

data = pytonl.decode(tonl_str)
print(data)

Output:

{
    "users": [
        {"id": 1, "name": "Alice", "role": "admin"},
        {"id": 2, "name": "Bob", "role": "user"}
    ]
}

Custom Options

from pytonl import encode, EncodeOptions

# Use custom delimiter
options = EncodeOptions(delimiter="|", include_types=True)
tonl_str = encode(data, options)

# Decode with options
from pytonl import decode, DecodeOptions
data = decode(tonl_str, DecodeOptions(strict=True))

Format Overview

TONL uses several strategies to minimize tokens:

Tabular Format for Uniform Arrays

Instead of repeating keys for each object:

[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]

TONL uses a table format:

items[2]{id,name}:
  1, Alice
  2, Bob

Smart Quoting

Values are only quoted when necessary (contains delimiters, special chars, or looks like a keyword).

Nested Blocks

Hierarchical data uses indentation for clarity:

config{database,cache}:
  cache: true
  database{host,port}:
    host: localhost
    port: 5432

API Reference

Main Functions

  • encode(data, options=None): Convert Python object to TONL string
  • decode(src, options=None): Convert TONL string to Python object

Classes

  • TONLEncoder: Encoder class for advanced usage
  • TONLDecoder: Decoder class for advanced usage
  • EncodeOptions: Configuration for encoding
  • DecodeOptions: Configuration for decoding

Types and Enums

  • TONLType: Enum for type hints (null, bool, u32, i32, f64, str, obj, list)

The encoder currently supports the following delimiters: "," (default), "|", ";", and the tab character "\t".

Documentation

For the complete specification and implementation details, see the IMPLEMENTATION_REFERENCE.md.

Development

Setup

# Clone the repository
git clone <repository-url>
cd pytonl

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=pytonl --cov-report=html

# Run specific test file
pytest tests/test_encoder.py -v

Code Quality

# Format code
black pytonl/ tests/

# Lint
ruff check pytonl/ tests/

# Type check
mypy pytonl/

Examples

See the tests directory for comprehensive examples covering:

  • Simple objects and arrays
  • Nested structures
  • Special characters and quoting
  • Type preservation
  • Roundtrip conversion

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

pytonl-1.0.0.tar.gz (67.1 kB view details)

Uploaded Source

Built Distribution

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

pytonl-1.0.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file pytonl-1.0.0.tar.gz.

File metadata

  • Download URL: pytonl-1.0.0.tar.gz
  • Upload date:
  • Size: 67.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytonl-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0c1eaa379cb09beb253aec40acf3ba31775b614cf1eb43a74404e0e387d1ec65
MD5 a40deedc21d5a05532ad05414d61362c
BLAKE2b-256 fd81f7b3c5d3c19a45c15a418fe9a52d636c5f02c2fd916de00bd6cb84305ddb

See more details on using hashes here.

File details

Details for the file pytonl-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytonl-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytonl-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15730d179a36fe75daa79209521353b33abb0a8171173fd7af4d981fb71700c2
MD5 1e636f3f515de599386d28e2c63f7bf6
BLAKE2b-256 260a4bbade97e227650063171d334fda3d646fc8393a85c98706094dc5949759

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