Skip to main content

Support for Pydantic settings configuration file loading

Project description

Pydantic Config

Support for Pydantic settings configuration file loading

Installation

pip install pydantic-config

Usage

# config.toml
app_name = "Python Application"
description = "Test application description"
from src.pydantic_config import SettingsModel


class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = None
    description: str = None
    log_level: str = 'INFO'

    class Config:
        config_file = 'config.toml'


settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Test application description' log_level='INFO'

Using multiple config files

Multiple config files can be loaded by passing a list of file names. Files will be loaded in the order they are listed. Meaning later files in the list will take priority over earlier files.

# config.toml
app_name = "Python Application"
description = "Test application description"
// config.json
{
  "description": "Description from JSON file",
  "log_level": "WARNING"
}
from src.pydantic_config import SettingsModel


class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = 'App Name'
    description: str = None
    log_level: str = 'INFO'

    class Config:
        config_file = ['config.toml', 'config.json']  # The config.json file will take priority over config.toml


settings = Settings()
print(settings)
# app_id='1' app_name='Python Application' description='Description from JSON file' log_level='WARNING'

Merging

If your configurations have existing list or dict variables the contents will be merged by default. To disable this behavior and override the contents instead you can set the config_merge option to False in the settings Config class.

# config.toml
[foo]
item1 = "value1"
# config2.toml
[foo]
item2 = "value2"
from src.pydantic_config import SettingsModel


class Settings(SettingsModel):
    foo: dict = {}

    class Config:
        config_file = ['config.toml', 'config2.toml']
        config_merge: bool = True


settings = Settings()
print(settings)
# foo={'item1': 'value1', 'item2': 'value2'}

# If config_merge=False then config2.toml would ovverride the values from config.toml
# foo={'item2': 'value2'}

Duplicate items in merged lists

By default, only unique list items will be merged. To disable this behavior and keep all items of a list regardless of duplication set the config_merge_unique option to False.

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

pydantic-config-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pydantic_config-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic-config-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic-config-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for pydantic-config-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b318d88239a35449240491c581411582fbfb21cfe05ed8a35d70944ef59d96fa
MD5 5b8d47e7d6892a7ffc7b5ef51a3476b5
BLAKE2b-256 347545b1e1ca5c3f8559c8f495c23fd5eb3df2ceb529de7c7c6a66d241f10e40

See more details on using hashes here.

File details

Details for the file pydantic_config-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_config-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2d18fbc5e49086494ce9e4e9cf99370cd216f2a1d22ac6a992ebe5ce8221e875
MD5 18d259cfc59352e0633d8d5babe6c223
BLAKE2b-256 c0500215ea17471ecea27ed2df2041d8219ef31d932023048b25f1a651c9c6be

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page