Skip to content

TOML conversion

The TOMLDataclass mixin provides automatic conversion to and from TOML. This uses the tomlkit library under the hood.

Usage Example

Define a TOMLDataclass.

from dataclasses import dataclass

from fancy_dataclass.toml import TOMLDataclass


@dataclass
class Database(TOMLDataclass):
    server: str
    ports: list[int]
    connection_max: int = 5000
    enabled: bool = True

Save data to a TOML file.

>>> db_config = Database(server='192.168.1.1', ports=[8001, 8001, 8002])
>>> with open('db_config.toml', 'w') as f:
        db_config.to_toml(f)

View the TOML file db_config.toml:

server = "192.168.1.1"
ports = [8001, 8001, 8002]
connection_max = 5000
enabled = true

Load the data from a TOML file:

>>> with open('db_config.toml') as f:
        db_config = Database.from_toml(f)
>>> print(db_config)
Database(server='192.168.1.1', ports=[8001, 8001, 8002], connection_max=5000, enabled=True)

Details

🚧 Under construction 🚧