A small but useful package to load, validate and use typed data structures, including configuration files.
Project description
datastruct
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.
Installation
pip install datastruct
Usage
>>> 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)
Other features
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
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
File details
Details for the file datastruct-0.5.tar.gz
.
File metadata
- Download URL: datastruct-0.5.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85153b97cb1579dfbae627ab3294cd873e9501fa614d74eb045db1ce141438ea |
|
MD5 | 4b20936867b1916590fceacd4c92d69e |
|
BLAKE2b-256 | 03c9aff0d56492f52112ccb1e2295c9f5863b35ff1bab35b11fe7eb04f20a410 |