A tool to easily load (many) JSON/YAML files as pydantic settings.
Project description
Why Should I Use This?
This project is very helpful for projects that have large configuration files, nested configuration files, or if you just don’t like writing large .env files. It is also worth noting that due to the backwards compatability between YAML and JSON that this will also parse JSON configuration.
For instance, the following settings
This can also be helpful when writing out (variables for ) helm charts, pipelines of various sorts, and other YAML assets. In such a context, it may be necessary to write an ENV file in line with your continuous integration or deployment variables. However, this can be rather cumbersome due to escape sequences:
# Example pipeline with env settings
# The configuration built is compatable with ``./examples/__init__.py``
...
pipelines:
default:
- step:
name: Create settings for subsequent steps
caches: pip
script:
- |
export MYFISTSETTING="1"
export MYDATABASESETTINGS="{
\"host\" : \"localhost\",
\"port\" : \"27017\",
\"username\" : \"some\",
\"password\" : \"dude\"
}"
- echo "MYFISTSETTING=$MYFISTSETTING" > .env
- echo "MYDATABASESETTINGS=$MYDATABASESETTINGS" >> .env
artifacts:
- .env
The script section of the above bitbucket pipeline may be replaced with something less horible to edit:
...
script:
- |
ENVYAMLCONTENT="{
myFistSetting: 1
myDatabaseSettings:
host: localhost
port: 27017
username: some
password: dude
}"
- echo $ENVYAMLCONTENT > .env
...
this may not make the strongest case due to the brevity of the settings themselves. But when the settings are many layers deep, it is clear that writing YAML is preferable.
Examples, Usage, and Installation
Install using pip:
pip install yaml-settings-pydantic
then import into your current project settings and modify your configuration:
from yaml_settings_pydantic import create_settings_yaml
from pydantic import BaseModel
from pydantic.env_settings import BaseSettings, SettingsSourceCallable
class SomeNestedSettings(BaseModel) :
...
class MySettings(BaseSettings):
class Config :
env_settings_yaml = create_settings_yaml(
"./path/to_my.yaml"
)
@classmethod
def customise_sources(
cls,
init_settings: SettingsSourceCallable,
env_settings: SettingsSourceCallable,
file_secret_settings: SettingsSourceCallable,
):
return (
init_settings,
env_settings,
file_secret_settings,
cls.env_yaml_settings,
)
mySetting: str
Finally it is useful to note that create_settings_yaml can accept multiple files as input (all such inputs must deserialize to dict) and reload them on every call of env_settings_yaml or just on the origonal call using the reload parameter:
...
env_settings_yaml = create_settings_yaml(
"./path/to/yaml_1.yaml",
"./path/to_my.yaml",
reload = True
)
...
In this instance the values from ./path/to_my.yaml will take precedence over the YAML provided earlier. That is, the later in the input list the path appears, the more its variables are prefered.
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
Built Distribution
Hashes for yaml-settings-pydantic-0.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f8d828646bebcbc5ad0543f9655851e38124693b89599b17e0048e84a7bb46c |
|
MD5 | c8c60ffef0fca35414f75821b094c62e |
|
BLAKE2b-256 | f7a322663db6163516a4be546382b3edf96df9ac351f14bd677eddc0a12a1e52 |
Hashes for yaml_settings_pydantic-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c47c756108b0721daf3eadab705e67c89e2d744f741c9f2b2d38171d23dab8d3 |
|
MD5 | edc49fffb43eff3b009df9cc6c76a663 |
|
BLAKE2b-256 | 8666407ddb1db60b9b60ee59b6e58462f5652f3a54521e0bc427b9059977a370 |