Core API
The Core module contains fundamental interfaces, types, and registry.
Core module for TOON Converter.
This module contains the fundamental interfaces, types, and registry following SOLID principles and clean architecture.
- class toonverter.core.ComparisonReport(analyses, best_format, worst_format, recommendations=<factory>)[source]
Bases:
objectComparative analysis of multiple formats.
- Parameters:
- analyses
Token analyses for each format
- best_format
Format with lowest token count
- worst_format
Format with highest token count
- recommendations
Optimization recommendations
- __init__(analyses, best_format, worst_format, recommendations=<factory>)
- property max_savings_percentage: float
Calculate maximum possible token savings.
- Returns:
Percentage savings from worst to best format
-
analyses:
list[TokenAnalysis]
- exception toonverter.core.ConversionError[source]
Bases:
ToonConverterErrorRaised when data conversion fails.
- class toonverter.core.ConversionResult(success, source_format, target_format, source_tokens=None, target_tokens=None, savings_percentage=None, data=None, error=None, metadata=<factory>)[source]
Bases:
objectResult of a format conversion operation.
- Parameters:
- success
Whether conversion succeeded
- source_format
Original format
- target_format
Target format
- source_tokens
Token count of source data
- target_tokens
Token count of target data
- savings_percentage
Percentage of tokens saved
- data
Converted data (if successful)
- error
Error message (if failed)
- metadata
Additional conversion metadata
- __init__(success, source_format, target_format, source_tokens=None, target_tokens=None, savings_percentage=None, data=None, error=None, metadata=<factory>)
- class toonverter.core.DecodeOptions(strict=True, type_inference=True, delimiter=',')[source]
Bases:
objectConfiguration options for decoding TOON format.
- strict
Raise errors on malformed input
- type_inference
Automatically infer data types
- delimiter
Expected field delimiter
- __init__(strict=True, type_inference=True, delimiter=',')
- exception toonverter.core.DecodingError[source]
Bases:
ToonConverterErrorRaised when decoding from TOON format fails.
- class toonverter.core.DefaultFormatRegistry[source]
Bases:
FormatRegistryDefault implementation of format adapter registry.
This class implements the Singleton pattern to ensure a single global registry instance. Thread-safe for concurrent access.
- Return type:
- static __new__(cls)[source]
Create or return the singleton instance.
- Return type:
- Returns:
Singleton registry instance
- clear()[source]
Clear all registered adapters.
Warning: This is primarily for testing. Use with caution.
- Return type:
- get(format_name)[source]
Retrieve format adapter by name.
- Parameters:
format_name (
str) – Format identifier (case-insensitive)- Return type:
- Returns:
FormatAdapter instance
- Raises:
FormatNotSupportedError – If format not found
- register(format_name, adapter)[source]
Register a format adapter.
- Parameters:
format_name (
str) – Format identifier (lowercase)adapter (
FormatAdapter) – FormatAdapter instance
- Raises:
ValueError – If format already registered or invalid
- Return type:
- unregister(format_name)[source]
Unregister a format adapter.
- Parameters:
format_name (
str) – Format identifier (case-insensitive)- Raises:
FormatNotSupportedError – If format not found
- Return type:
- class toonverter.core.EncodeOptions(indent=2, delimiter=',', length_marker=None, compact=False, sort_keys=False, ensure_ascii=False, max_line_length=None, token_budget=None, optimization_policy=None)[source]
Bases:
objectConfiguration options for encoding data to TOON format.
This class uses the Builder pattern to provide preset configurations and flexible customization.
- Parameters:
- indent
Number of spaces for indentation (default: 2)
- delimiter
Field delimiter for tabular data
- length_marker
Optional length prefix for strings
- compact
Use compact representation without whitespace
- sort_keys
Sort dictionary keys alphabetically
- ensure_ascii
Escape non-ASCII characters
- max_line_length
Maximum line length before wrapping
- token_budget
Maximum token count for output (active optimization)
- optimization_policy
Rules for intelligent degradation
- __init__(indent=2, delimiter=',', length_marker=None, compact=False, sort_keys=False, ensure_ascii=False, max_line_length=None, token_budget=None, optimization_policy=None)
- classmethod create_compact()[source]
Create preset for compact encoding.
- Return type:
- Returns:
EncodeOptions configured for minimal token usage
- classmethod readable()[source]
Create preset for human-readable encoding.
- Return type:
- Returns:
EncodeOptions configured for readability
- exception toonverter.core.EncodingError[source]
Bases:
ToonConverterErrorRaised when encoding to TOON format fails.
- exception toonverter.core.FileOperationError[source]
Bases:
ToonConverterErrorRaised when file read/write operations fail.
- class toonverter.core.FormatAdapter[source]
Bases:
ABCAbstract base class for format adapters.
Format adapters implement the Strategy pattern for different data format conversions.
- abstractmethod decode(data_str, options=None)[source]
Decode data from this format.
- Parameters:
data_str (
str) – String data in this formatoptions (
DecodeOptions|None) – Decoding configuration options
- Return type:
- Returns:
Decoded data (dict, list, or primitive types)
- Raises:
DecodingError – If decoding fails
- decode_stream(stream, **kwargs)[source]
Decode data from a stream of strings.
- Parameters:
- Returns:
An iterator yielding decoded objects
- Return type:
Iterator[Any]
- Raises:
NotImplementedError – If the adapter does not support streaming
- abstractmethod encode(data, options=None)[source]
Encode data to this format.
- Parameters:
data (
Any) – Data to encode (dict, list, or primitive types)options (
EncodeOptions|None) – Encoding configuration options
- Return type:
- Returns:
String representation in this format
- Raises:
EncodingError – If encoding fails
- encode_stream(data, **kwargs)[source]
Encode data to the format as a stream of strings.
- Parameters:
- Returns:
An iterator yielding chunks of the encoded data
- Return type:
Iterator[str]
- Raises:
NotImplementedError – If the adapter does not support streaming
- abstract property format_name: str
Return the format name (e.g., ‘json’, ‘yaml’, ‘toon’).
- Returns:
Format identifier string
- exception toonverter.core.FormatNotSupportedError[source]
Bases:
ToonConverterErrorRaised when a format is not supported.
- class toonverter.core.FormatRegistry[source]
Bases:
ABCAbstract base class for format adapter registry.
Implements the Factory pattern for creating format adapters.
- abstractmethod get(format_name)[source]
Retrieve format adapter by name.
- Parameters:
format_name (
str) – Format identifier- Return type:
- Returns:
FormatAdapter instance
- Raises:
FormatNotSupportedError – If format not found
- abstractmethod register(format_name, adapter)[source]
Register a format adapter.
- Parameters:
format_name (
str) – Format identifieradapter (
FormatAdapter) – FormatAdapter instance
- Raises:
ValueError – If format already registered
- Return type:
- abstractmethod unregister(format_name)[source]
Unregister a format adapter.
- Parameters:
format_name (
str) – Format identifier- Raises:
FormatNotSupportedError – If format not found
- Return type:
- class toonverter.core.Plugin[source]
Bases:
ABCAbstract base class for plugins.
Plugins extend TOON Converter functionality without modifying the core codebase.
- abstractmethod register(registry)[source]
Register plugin components with the registry.
- Parameters:
registry (
FormatRegistry) – Format registry instance- Raises:
PluginError – If registration fails
- Return type:
- exception toonverter.core.PluginError[source]
Bases:
ToonConverterErrorRaised when plugin loading or registration fails.
- class toonverter.core.TokenAnalysis(format, token_count, model='cl100k_base', encoding='utf-8', metadata=<factory>)[source]
Bases:
objectAnalysis of token usage for different formats.
- format
Data format analyzed
- token_count
Number of tokens
- model
Tokenizer model used
- encoding
Specific encoding method
- metadata
Additional analysis metadata
- __init__(format, token_count, model='cl100k_base', encoding='utf-8', metadata=<factory>)
- exception toonverter.core.TokenCountError[source]
Bases:
ToonConverterErrorRaised when token counting fails.
- class toonverter.core.TokenCounter[source]
Bases:
ABCAbstract base class for token counting implementations.
- abstractmethod analyze(text, format_name)[source]
Analyze token usage for text in given format.
- Parameters:
- Return type:
- Returns:
TokenAnalysis with detailed statistics
- Raises:
TokenCountError – If analysis fails
- abstractmethod count_tokens(text)[source]
Count tokens in text.
- Parameters:
text (
str) – Text to analyze- Return type:
- Returns:
Number of tokens
- Raises:
TokenCountError – If counting fails
- exception toonverter.core.ToonConverterError[source]
Bases:
ExceptionBase exception for all TOON Converter errors.
- exception toonverter.core.ValidationError[source]
Bases:
ToonConverterErrorRaised when input validation fails.