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.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic-config-0.2.2.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.2.tar.gz
Algorithm Hash digest
SHA256 35789e74a4e570e78d6771558f9cc69810856dbaf696793ecbd97b1902775305
MD5 bdacc83fdad5d548c395614a6554871d
BLAKE2b-256 4b9c02370c4a655dc9c833c26b429ca93ea8c1554791440533da663b16aead5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_config-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 41144d4b3d507d0ae437db9501eceb8d332c15eaaf9d50634df7baceef5ccd3a
MD5 393287c62fd1368dab746169fbdf8d7d
BLAKE2b-256 338f4b0652bf1edd4fb33771765c756ff1c0093d5c7dc1d3911f4a1c428216a9

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