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.
Release files for pytonl 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytonl-1.0.1.tar.gz | 67.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytonl-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 83.4 kB
Release files / pytonl-1.0.1.tar.gz
| Download URL | pytonl-1.0.1.tar.gz |
|---|---|
| Size | 67.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a3ac5d2e0995cc7150e7f2e8c8d00186b952af7d4f1bd40fa5cf57e44db19367
|
|
BLAKE2b-256 checksum How to use checksums |
45097a3317f9aad6cb5c8b7a58aaa01a2c3aaa0ad15e8ad767895aa95984bd39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / pytonl-1.0.1-py3-none-any.whl
| Download URL | pytonl-1.0.1-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cddd9e50214fb5bd4059f9db50700b7613f52c06a9cf95381c166df17747949a
|
|
BLAKE2b-256 checksum How to use checksums |
bc68b856341872d7d1a14f1955bc9e6e98ccb27a071d15b6772da3929bb81b6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|