TOML conversion
The TOMLDataclass
mixin provides automatic conversion to and from TOML. This uses the tomlkit
library under the hood.
to_dict
/from_dict
convert to and from Python dicts.to_toml
/from_toml
convert to and from TOML file-like objects.save
/load
convert to and from a TOML file-like object or path.to_toml_string
/from_toml_string
convert to and from TOML strings.
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
:
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 🚧