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 stringdecode(src, options=None): Convert TONL string to Python object
Classes
TONLEncoder: Encoder class for advanced usageTONLDecoder: Decoder class for advanced usageEncodeOptions: Configuration for encodingDecodeOptions: 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytonl-1.0.1.tar.gz.
File metadata
- Download URL: pytonl-1.0.1.tar.gz
- Upload date:
- Size: 67.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3ac5d2e0995cc7150e7f2e8c8d00186b952af7d4f1bd40fa5cf57e44db19367
|
|
| MD5 |
2575842f4690306d8cf202315138cb95
|
|
| BLAKE2b-256 |
45097a3317f9aad6cb5c8b7a58aaa01a2c3aaa0ad15e8ad767895aa95984bd39
|
File details
Details for the file pytonl-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pytonl-1.0.1-py3-none-any.whl
- Upload date:
- Size: 16.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cddd9e50214fb5bd4059f9db50700b7613f52c06a9cf95381c166df17747949a
|
|
| MD5 |
c7da743c0bd7c71194a50bfade1e8549
|
|
| BLAKE2b-256 |
bc68b856341872d7d1a14f1955bc9e6e98ccb27a071d15b6772da3929bb81b6f
|