Skip to main content

Add your description here

Project description

TOMLSerializer 📝

A powerful and flexible Python library for serializing Python objects to TOML format with robust type handling support.

✨ Features

  • 🔄 Serializes Python dataclasses to TOML format and back
  • 🏷️ Preserves type information during serialization
  • 📦 Supports complex data types including:
    • 🔢 Basic types (int, float, str, bool)
    • 📚 Collections (list, dict, set, tuple)
    • 📂 Path objects
    • 🎯 Callable functions
    • ❓ Optional fields
    • 🔀 Union types
    • 🪢 Nested data structures
    • 🕳️ Empty collections
    • 👨‍👦 Inheritance hierarchies

🚀 Installation

pip install tomlserializer  # Package name may vary

🏃 Quick Start

from dataclasses import dataclass
from pathlib import Path
from tomlserializer import TOMLSerializer

@dataclass
class Config:
    name: str
    values: list[int]
    output_path: Path
    enabled: bool

# Create an instance
config = Config(
    name="example",
    values=[1, 2, 3],
    output_path=Path("/tmp/output"),
    enabled=True
)

# Save to TOML
TOMLSerializer.save(config, "config.toml")

# Load from TOML
loaded_data = TOMLSerializer.load_typed_dict("config.toml")

🔧 Advanced Usage

🛠️ Custom Type Registration

You can register custom type handlers for serialization and deserialization:

def custom_type_to_toml(value):
    return str(value)

def custom_type_from_toml(value):
    return CustomType(value)

TOMLSerializer.register_type(CustomType, custom_type_to_toml, custom_type_from_toml)

🌟 Supporting Complex Types

The library handles various complex scenarios:

@dataclass
class ComplexConfig:
    # Optional fields
    optional_value: Optional[int] = None

    # Union types
    flexible_field: Union[str, int]

    # Nested collections
    nested_dict: Dict[str, Dict[str, int]]

    # Function references
    callback: Callable

👨‍👦 Inheritance Support

@dataclass
class BaseConfig:
    base_field: str

@dataclass
class DerivedConfig(BaseConfig):
    derived_field: int

# Both base and derived fields are preserved during serialization

📋 Type Support

The library includes built-in support for:

  • 🔢 Basic types (int, float, str, bool)
  • 📚 Collection types (list, dict, set, tuple)
  • 📂 Pathlib Path objects
  • 🎯 Python functions (via module path resolution)
  • ❌ None values
  • ❓ Optional fields
  • 🔀 Union types

⚠️ Error Handling

The library includes robust error handling for:

  • 🚫 Invalid file paths
  • 🔄 Type conversion errors
  • ❗ Missing required fields
  • 🏗️ Invalid TOML syntax

🤝 Contributing

Contributions are welcome! Please feel free to submit pull requests.

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

tomlserializer-0.2.1.tar.gz (63.2 kB view details)

Uploaded Source

Built Distribution

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

tomlserializer-0.2.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file tomlserializer-0.2.1.tar.gz.

File metadata

  • Download URL: tomlserializer-0.2.1.tar.gz
  • Upload date:
  • Size: 63.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.8

File hashes

Hashes for tomlserializer-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0dd0b36665856a51e40b40c1e2da7f2a7a7e60d16195441da6dad3dc44a12987
MD5 94a9c5469b9d4c69990faf047dfa8511
BLAKE2b-256 b0f233f2c3cfaae09e9ab45db7ebaf6f892f62e37c5fc5d4d9cde8043ff92eca

See more details on using hashes here.

File details

Details for the file tomlserializer-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tomlserializer-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ecaf9eeb1b6872468e05f704cba16054077432a0e758971a604a9fdd56b2ffc
MD5 6eb10fac8f26c6981c69f2dce2da519d
BLAKE2b-256 edcf2ecc6489e04b5aebf76ac6d339291a6ff34462fbda896c7a3206827eaeb0

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