Getting started with toml_dataclass
Toml and Python Dataclass serialization.
Features
- Preserve the in-memory representation of Python dataclasses
Examples
Creating a dataclass with support to toml serialization.
>>> from danoan.toml_dataclass import TomlDataClassIO
>>> from dataclasses import dataclass
>>> @dataclass
... class Plugin(TomlDataClassIO):
... name: str
... version: str
... description: str
>>> jpg_plugin = Plugin("image-jpg", "1.0", "Conversion functions to jpg type.")
>>> with open("jpg-plugin.toml", "w") as fw:
... jpg_plugin.write(fw)
Here it is what it looks like the written toml.
>>> with open("jpg-plugin.toml", "r") as fr:
... print(fr.read())
name = "image-jpg"
version = "1.0"
description = "Conversion functions to jpg type."
Preserve the in-memory representation of Python dataclasses after reading.
>>> from pprint import pprint
>>> @dataclass
... class Configuration(TomlDataClassIO):
... project_name: str
... plugin: Plugin
>>> config = Configuration("image-reader", jpg_plugin)
>>> with open("config.toml", "w") as fw:
... config.write(fw)
>>> with open("config.toml", "r") as fr:
... config_from_file = Configuration.read(fr)
>>> pprint(config_from_file)
Configuration(project_name='image-reader',
plugin=Plugin(name='image-jpg',
version='1.0',
description='Conversion functions to jpg type.'))
>>> pprint(f"Plugin name: {config_from_file.plugin.name}")
'Plugin name: image-jpg'
Notice how it differs from the ouptut of the toml library.
>>> import toml
>>> pprint(toml.load("config.toml"))
{'plugin': {'description': 'Conversion functions to jpg type.',
'name': 'image-jpg',
'version': '1.0'},
'project_name': 'image-reader'}
Preserve in-memory representation with toml tables.
>>> from danoan.toml_dataclass import TomlTableDataClassIO
>>> from typing import List
>>> @dataclass
... class PluginTable(TomlTableDataClassIO):
... list_of_plugins: List[Plugin]
>>> png_plugin = Plugin("image-png", "1.0", "Conversion functions to png type.")
>>> plugin_table = PluginTable([jpg_plugin, png_plugin])
>>> with open("plugin-table.toml", "w") as fw:
... plugin_table.write(fw)
>>> with open("plugin-table.toml", "r") as fr:
... plugin_table_from_file = PluginTable.read(fr)
>>> pprint(plugin_table_from_file)
PluginTable(list_of_plugins=[Plugin(name='image-jpg',
version='1.0',
description='Conversion functions to jpg '
'type.'),
Plugin(name='image-png',
version='1.0',
description='Conversion functions to png '
'type.')])
Notice how it differs from the ouptut of the toml library.
>>> pprint(toml.load("plugin-table.toml"))
{'list_of_plugins': [{'description': 'Conversion functions to jpg type.',
'name': 'image-jpg',
'version': '1.0'},
{'description': 'Conversion functions to png type.',
'name': 'image-png',
'version': '1.0'}]}
Here it is what looks like the written toml.
>>> with open("plugin-table.toml", "r") as f:
... print(f.read())
[[list_of_plugins]]
name = "image-jpg"
version = "1.0"
description = "Conversion functions to jpg type."
<BLANKLINE>
[[list_of_plugins]]
name = "image-png"
version = "1.0"
description = "Conversion functions to png type."
<BLANKLINE>
<BLANKLINE>
Metadata
Release files for toml-dataclass 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| toml-dataclass-0.1.0.tar.gz | 5.6 kB | Without provenance | Details for toml-dataclass-0.1.0.tar.gz |
Release files / toml-dataclass-0.1.0.tar.gz
| Download URL | toml-dataclass-0.1.0.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64687a14e168c8b3100ed61589e0e1bd7d7f3bae0f7793053cd14e2876b0488a
|
|
BLAKE2b-256 checksum How to use checksums |
c0d977574c24d3aef0727a483c5dc3b5e66e8991f7c5e22b073ed4004658e0ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.8
|
Security for toml-dataclass 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.