A small but useful package to load, validate and use typed data structures, including configuration files.
You get:
- An easy way to define a typed hierarchical data structure.
- Hassle free definition nested structures.
- Loading from a variety of formats (json, yaml and everything supported by Serialize),
- Error checking including: missing values, unexpected value, wrong type, wrong value.
- Easy to integrate in another app error reporting.
pip install datastruct
>>> from typing import List
>>> from datastruct import DataStruct
>>> class EmailServer(DataStruct):
...
... host: str
... port: int
... username: str
... password: str
>>>
>>> class Config(DataStruct):
...
... download_path: str
... email_servers: List[EmailServer]
... wait_time: float
>>>
>>> cfg = Config.from_filename('settings.yaml')
When an invalid value is found, an exception will be raised.
If you want to accumulate all errors for inspection:
>>> cfg = Config.from_filename('settings.yaml', raise_on_error=False)
>>> print(cfg.get_errors())
You can then use the DataStruct object in your code:
>>> print(cfg.email_servers[0].host)
You can easily specify default values that
>>> class EmailServer(DataStruct):
...
... host: str
... port: int = 25
... username: str
... password: str
See AUTHORS for a list of the maintainers.
To review an ordered list of notable changes for each version of a project, see CHANGES