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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic-config-0.2.1.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.1.tar.gz
Algorithm Hash digest
SHA256 99319e429c9ff4985b85b766bb02c9a2c6e9d6ccad3a2de766d7a7bf2dc50b97
MD5 7d2e8f29b16f5248d519fda7a0eac351
BLAKE2b-256 554016e747511f38c4f6533569f607fd9ce3dd4d07bf32d3bfa373cb19c2dbbe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pydantic_config-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7208dad31b2876d7933916ef368fcac1a017f22caa005855c317448554bb3fc1
MD5 3778c065dee0b89f23226ec61d5756dd
BLAKE2b-256 4ac48ef6fe62d5077fface572b0661db180415084b391220fd0cb2ba12526003

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