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

Optional Dependencies

Pydantic-Config has the following optional dependencies:

  • yaml - pip install pydantic-config[yaml]
  • toml - pip install pydantic-config[toml]

You can install all the optional dependencies with pip install pydantic-config[all]

Usage

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


class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = None
    description: str = None
    log_level: str = 'INFO'
    
    model_config = SettingsConfig(
        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 pydantic_config import SettingsModel, SettingsConfig


class Settings(SettingsModel):
    app_id: str = 1
    app_name: str = 'App Name'
    description: str = None
    log_level: str = 'INFO'
    
    model_config = SettingsConfig(
        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'

Supported file formats

Currently, the following file formats are supported:

  • .yaml Requires pyyaml package
  • .toml Requires toml package
  • .json
  • .ini

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 pydantic_config import SettingsModel, SettingsConfig


class Settings(SettingsModel):
    foo: dict = {}
    
    model_config = SettingsConfig(
        config_file=['config.toml', 'config2.toml'],
        config_merge= 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.2.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

pydantic_config-0.2.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic-config-0.2.3.tar.gz
Algorithm Hash digest
SHA256 417357b651aa6b33a59708b9daa75089aff1af765b231a164ac0791805f10507
MD5 f57fa94e6b84d3174460b1bf294cbb29
BLAKE2b-256 064e9d0de702a6cbdd4e6becd0c5fdbe29ad2cdc543fa88d8c09b5fbc4e939d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_config-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6baf7745d9aab9fefdd991760acf8b5f4abc0da0a726a3026155b928eb184dbb
MD5 0c5b7b8a481df1531dd92d2300d7d00d
BLAKE2b-256 f181318dc55bc1691063b28215055efa9e207383c3ba19df914f40b05f051ae6

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