Load toml/yaml/json config files into classes for a typed config (type hinting etc.)
Project description
Configuraptor
Load config files into Python classes for a typed config (for type hinting etc.).
Supported file types are toml/yaml/json, and .env/.ini to a lesser degree
(see Supported Config File Types).
Table of Contents
Installation
pip install configuraptor
Usage
Configuraptor can be used to load your config files into structured Python classes.
# examples/example_from_readme.toml
[config]
name = "Hello World!"
[config.reference]
number = 42
numbers = [41, 43]
string = "42"
Could be loaded into Python classes using the following code:
# examples/example_from_readme.py
from configuraptor import load_into, TypedConfig
######################
# with basic classes #
######################
class SomeRegularClass:
number: int
numbers: list[int]
string: str
class Config:
name: str
reference: SomeRegularClass
if __name__ == '__main__':
my_config = load_into(Config, "example_from_readme.toml") # or .json, .yaml
print(my_config.name)
# Hello World!
print(my_config.reference.numbers)
# [41, 43]
########################
# alternative notation #
########################
class SomeOtherRegularClass:
number: int
numbers: list[int]
string: str
class OtherConfig(TypedConfig):
name: str
reference: SomeRegularClass
if __name__ == '__main__':
my_config = OtherConfig.load("example_from_readme.toml") # or .json, .yaml
print(my_config.name)
# Hello World!
print(my_config.reference.numbers)
# [41, 43]
# TypedConfig has an extra benefit of allowing .update:
my_config.update(numbers=[68, 70])
More examples can be found in examples.
Supported Config File Types
.toml
: supports the most types (strings, numbers, booleans, datetime, lists/arrays, dicts/tables);.json
: supports roughly the same types as toml (except datetime);.yaml
: supports roughly the same types as toml, backwards compatible with JSON;.env
: only supports strings. You can useconvert_types=True
to try to convert to your annotated types;.ini
: only supports strings. You can useconvert_types=True
to try to convert to your annotated types;
For other file types, a custom Loader can be written. See examples/readme.md#Custom File Types
License
configuraptor
is distributed under the terms of the MIT license.
Changelog
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
configuraptor-1.13.1.tar.gz
(536.6 kB
view hashes)
Built Distribution
Close
Hashes for configuraptor-1.13.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9944d3c96757d430d5f9c837c89f72492d15a07662222b33cf2494365196f7f |
|
MD5 | 659ec966e677771fd4aeb484b4866ea4 |
|
BLAKE2b-256 | 089c1ca749db361d68b2725d95ff60dfb96fac55d34d1491e5b1fcd4a75affb4 |