Unified way to load a config file to python obj.
Project description
config-core
This is a opinionated way to parse a config.
This is a opinionated way to parse a config. It requires Pydantic BaseModel config models.
Usage
Below are some examples on how to use the config_core.load_config function from this package.
For load_config to work you’ll need a data structure. These can be created in python with Pydantic BaseModel
Basic example
For load_config to work you’ll need a data structure. These can be created in python with Pydantic BaseModel
Simple data stucture example.
from pydantic import AfterValidator, BaseModel, ValidationError
from typing import Set
class NestingConfig(BaseModel):
"""Main config for node."""
unique_int_set: Set = []
example_boolean: bool = False
class ConfigFile(BaseModel):
"""Top config level."""
# Required top-level field: parsing should fail if missing
service_id: int
node_config: NestingConfig
Example config file to be parsed (for this example use “./path_to_config.yaml”)
service_id: 1211
node_config:
unique_int_set:
- 2
- 8
This config file can be parsed with config_core.load_config
from config_core import load_config
config: ConfigFile = load_config("./path_to_config.yaml", {}, ConfigFile)
This will load the config and parse it to an ConfigFile object.
Camel case example
Camel case for inside the configs is also possible. Python can continue to use pascal_case.
serviceId: 1211
nodeConfig:
uniqueIntSet:
- 2
- 8
Key replacement
You can also some keys in the data stucture with the key_replacement argument.
service_id: 1211
type_of_node_running_this_config:
unique_int_set:
- 2
- 8
And then in python
from config_core import load_config
config: ConfigFile = load_config("./path_to_config.yaml", {"type_of_node_running_this_config": "node_config"}, ConfigFile)
Env variables
Env variables inside the config is also possible
Env variables here will be SERVICE_ID
service_id: ${SERVICE_ID}
type_of_node_running_this_config:
unique_int_set:
- 2
- 8
or
service_id: $SERVICE_ID
type_of_node_running_this_config:
unique_int_set:
- 2
- 8
Making Changes & Contributing
This project uses pre-commit, please make sure to install it before making any changes:
pip install pre-commit cd config-core pre-commit install
It is a good idea to update the hooks to the latest version:
pre-commit autoupdate
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file config_core-0.1.0.tar.gz.
File metadata
- Download URL: config_core-0.1.0.tar.gz
- Upload date:
- Size: 19.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5336fe80a76906d63fc234bb6b1de37e6cb5e301c921432d5472234018dd9937
|
|
| MD5 |
41d9724f4d24cbec1ec5f744fb59f1e1
|
|
| BLAKE2b-256 |
ffd1133249a88817c7a58cf7f62c1e3802d5ddc1b44b739b0b472ef0122af6ba
|
File details
Details for the file config_core-0.1.0-py3-none-any.whl.
File metadata
- Download URL: config_core-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a323bc5b77e9889ad94a89354c33921027d69ef5879295c5883f0257ac69747
|
|
| MD5 |
41306dc8e38960ea3e5c98825ac7fc1b
|
|
| BLAKE2b-256 |
32d71eb19ad37b172a1cce7cb7f45aa8639aa185d0e5702fdc9c79d4b7639a8b
|