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

Uploaded Source

Built Distribution

pydantic_config-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic-config-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 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.0.tar.gz
Algorithm Hash digest
SHA256 6c1d30c01e7e618e4ff959557c97d1e861abcec11361f38d20739d4ef43d668c
MD5 7407dd42438d5124cb3e9406e157d14d
BLAKE2b-256 22ebb96de840db28e6b208f7fd738fdb54a6897cb120e52c808b0e08ede55d67

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_config-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb4137a3dd9714d6ef3cfd48c3076a1fd9e6ed8fae7b965a7282e06997d57963
MD5 eaf4bfa5b208dded50b810f95355989e
BLAKE2b-256 cd02058258a20970e1e361e0f6beac781c2a9770596797edc95565441b794651

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