Skip to main content

COON Python SDK

PyPI version Python Version License: MIT

Token-efficient compression format for Dart/Flutter code, optimized for LLM contexts.

Table of Contents

Installation

# Standard installation
pip install coon

# With CLI support
pip install coon[cli]

# For development
pip install coon[dev]

Quick Start

from coon import compress_dart, decompress_coon

# Compress Dart code
dart_code = """
class MyWidget extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(title: Text('Hello')),
            body: Center(child: Text('World')),
        );
    }
}
"""

compressed = compress_dart(dart_code)
print(f"Compressed: {compressed}")
# Output: c:MyWidget<StatelessWidget>;m:b S{a:B{t:T'Hello'},b:N{c:T'World'}}

# Decompress back to Dart
restored = decompress_coon(compressed)

Advanced Usage

Using the Compressor Class

from coon import Compressor, CompressionConfig

# Configure compression
config = CompressionConfig(
    strategy="aggressive",
    enable_metrics=True,
    validate_output=True
)

compressor = Compressor(config)
result = compressor.compress(dart_code)

print(f"Original tokens: {result.original_tokens}")
print(f"Compressed tokens: {result.compressed_tokens}")
print(f"Savings: {result.percentage_saved:.1f}%")
print(f"Strategy: {result.strategy_used}")

Code Analysis

from coon import CodeAnalyzer

analyzer = CodeAnalyzer()
analysis = analyzer.analyze(dart_code)

# Get detailed report
report = analyzer.generate_report(analysis)
print(report)

# Access metrics
print(f"Complexity: {analysis.complexity}")
print(f"Widget count: {analysis.widget_count}")
print(f"Compression opportunities: {analysis.opportunities}")

Validation

from coon import validate_round_trip

# Verify lossless compression
is_valid = validate_round_trip(dart_code)
print(f"Round-trip valid: {is_valid}")

Compression Strategies

Strategy Compression Speed Description
auto 50-70% Fast Automatic optimal selection
basic 30-40% Fastest Simple abbreviations
aggressive 60-70% Fast Maximum compression
ast_based 50-65% Moderate Syntax tree analysis
component_ref 70-80% Moderate Pattern-based references
semantic 55-65% Moderate Meaning-preserving

Strategy Selection

from coon import Compressor, CompressionStrategyType

compressor = Compressor()

# Use specific strategy
result = compressor.compress(code, strategy=CompressionStrategyType.AGGRESSIVE)

# Use auto (default)
result = compressor.compress(code, strategy=CompressionStrategyType.AUTO)

API Reference

Core Functions

compress_dart(code: str, strategy: str = "auto") -> str

Compress Dart code to COON format.

compressed = compress_dart(dart_code)
compressed = compress_dart(dart_code, strategy="aggressive")

decompress_coon(compressed: str) -> str

Decompress COON format back to Dart code.

original = decompress_coon(compressed)

Classes

Compressor

Main compression class with configuration options.

from coon import Compressor, CompressionConfig

config = CompressionConfig(
    strategy="auto",
    enable_metrics=True,
    validate_output=False
)

compressor = Compressor(config)
result = compressor.compress(code)

Decompressor

Decompression class with formatting options.

from coon import Decompressor

decompressor = Decompressor(
    format_output=True,
    indent_size=2
)

result = decompressor.decompress(compressed)

CompressionResult

Result object containing compression output and metrics.

result.compressed         # Compressed code string
result.original_tokens    # Original token count
result.compressed_tokens  # Compressed token count
result.percentage_saved   # Compression percentage
result.strategy_used      # Strategy that was used

CLI Usage

# Compress a file
coon compress app.dart -o app.coon

# Decompress
coon decompress app.coon -o app.dart

# Use specific strategy
coon compress app.dart -s aggressive -o app.coon

# Analyze for compression opportunities
coon analyze app.dart

# Compare all strategies
coon stats app.dart

# Validate round-trip integrity
coon validate app.dart

Architecture

coon/
├── core/          # Compressor, Decompressor, Config, Result
├── strategies/    # Compression strategy implementations
│   ├── base.py         # Base strategy class
│   ├── basic.py        # Basic compression
│   ├── aggressive.py   # Aggressive compression
│   ├── ast_based.py    # AST-based compression
│   └── component_ref.py # Component reference
├── data/          # Abbreviation data from shared spec
├── parser/        # Lexer, Parser, AST nodes
├── analysis/      # Code analyzer, Metrics
├── utils/         # Validator, Registry, Formatter
└── cli/           # Command-line interface

Testing

# Run all tests
pytest tests/

# Run specific test file
pytest tests/test_core.py

# Run with coverage
pytest tests/ --cov=coon --cov-report=html

# Run conformance tests
pytest tests/test_conformance.py

License

MIT - See LICENSE for details.

Release files for coon 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for coon 0.1.1
File Size Uploaded
coon-0.1.1.tar.gz 51.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for coon 0.1.1
File Interpreter ABI Platform
coon-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 112.5 kB

Release files / coon-0.1.1.tar.gz

Download URL coon-0.1.1.tar.gz
Size 51.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ec46889fe502286fe379dca0ae24ae3bfba8fdf7f3f8e15f1078bd5b76e72151
BLAKE2b-256 checksum
How to use checksums
6d4327e2e43bda2d8e807b52547a5eb48c811423371477528b7e2d7cc38d23a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Release files / coon-0.1.1-py3-none-any.whl

Download URL coon-0.1.1-py3-none-any.whl
Size 60.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8689efa628da619055327003b7de78348ed191bc650d8b15c29aa8bbb5bdf509
BLAKE2b-256 checksum
How to use checksums
d7b08a07bb801ec6ca87210d9bb475b3b1a76976152e7d596e982a25eb96b507
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.4

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page