Donfig

0.8.1.post1 · active · verified Thu Apr 09

Donfig is a Python library designed to simplify package and script configuration, drawing inspiration from the configuration logic originally found in the Dask library. It allows configuration through programmatic settings, environment variables, and YAML files located in standard paths. The library is actively maintained, with the current version being 0.8.1.post1, and releases occurring periodically to add features and fix bugs.

Warnings

Install

Imports

Quickstart

Initialize a `Config` object with a unique name, which it uses to locate environment variables and YAML files. Access settings using `config.get()` and update them with `config.set()`. The `set()` method can also be used as a context manager for temporary changes. Environment variables take precedence over YAML files, which in turn take precedence over programmatic defaults.

import os
from donfig import Config

# Simulate environment variable for demonstration
os.environ['MYAPP_SETTING_ONE'] = 'env_value'

# Create a configuration object for your application/package
# The name ('myapp' here) is used for environment variable prefixing (e.g., MYAPP_)
# and YAML file searching (e.g., ~/.config/myapp/)
config = Config('myapp', defaults={'setting_one': 'default_value', 'setting_two': 123})

# Access configuration values
value_one = config.get('setting_one')
value_two = config.get('setting_two')

print(f"Setting One (from env): {value_one}")
print(f"Setting Two (from default): {value_two}")

# Update configuration programmatically
config.set(setting_two=456)
print(f"Setting Two (updated): {config.get('setting_two')}")

# Use as a context manager to temporarily change configuration
with config.set(setting_one='context_value'):
    print(f"Setting One (in context): {config.get('setting_one')}")

print(f"Setting One (after context): {config.get('setting_one')}")

# Clean up environment variable (optional, for isolated testing)
del os.environ['MYAPP_SETTING_ONE']

view raw JSON →