A flexible configuration library that provides hierarchical configuration management with YAML support, variable interpolation, and type validation
—
Methods for updating, merging, selecting, and modifying configurations including dot notation access, deep merging strategies, and configuration resolution.
Combines multiple configurations with different merging strategies and conflict resolution approaches.
def merge(*configs):
"""
Merge multiple configs with copying for safety.
Parameters:
- *configs: Variable number of configs to merge (DictConfig, ListConfig, or dicts/lists)
Returns:
Merged configuration (DictConfig or ListConfig)
Notes:
- Later configs override earlier ones
- Creates copies to avoid modifying originals
- Supports deep merging of nested structures
"""
def unsafe_merge(*configs):
"""
Fast merge without copying for performance.
Parameters:
- *configs: Variable number of configs to merge
Returns:
Merged configuration
Warning:
- May modify input configurations
- Use only when input configs won't be reused
"""Selects values from nested configurations using dot notation paths with robust error handling.
def select(cfg, key, default=None, throw_on_resolution_failure=True, throw_on_missing=False):
"""
Select value using dot notation key.
Parameters:
- cfg: Configuration to select from
- key: Dot notation key (e.g., "database.host" or "servers[0].name")
- default: Value to return if key not found
- throw_on_resolution_failure: Whether to raise on interpolation errors
- throw_on_missing: Whether to raise on missing keys
Returns:
Selected value or default
Examples:
- select(cfg, "database.host") -> cfg.database.host
- select(cfg, "servers[0]") -> cfg.servers[0]
- select(cfg, "missing.key", "default") -> "default"
"""Updates configuration values using dot notation with support for nested creation and type validation.
def update(cfg, key, value, merge=True, force_add=False):
"""
Update configuration value using dot notation.
Parameters:
- cfg: Configuration to update
- key: Dot notation key for update location
- value: New value to set
- merge: Whether to merge if value is a config/dict
- force_add: Whether to add new keys even in struct mode
Examples:
- update(cfg, "database.host", "new-host")
- update(cfg, "new.nested.key", {"a": 1, "b": 2})
"""Resolves all variable interpolations in configurations with cycle detection and error handling.
def resolve(cfg):
"""
Resolve all interpolations in configuration.
Parameters:
- cfg: Configuration to resolve
Returns:
None (modifies configuration in-place)
Notes:
- Resolves ${key} interpolations
- Handles nested interpolations
- Detects and prevents circular references
- Uses registered resolvers for custom interpolations
"""Identifies keys with missing mandatory values throughout the configuration hierarchy.
def missing_keys(cfg):
"""
Get set of keys with missing mandatory values.
Parameters:
- cfg: Configuration to analyze
Returns:
Set of dot notation paths to missing keys
Examples:
- {"key": "???"} -> {"key"}
- {"nested": {"key": "???"}} -> {"nested.key"}
"""Creates modified copies of configurations with selective key inclusion and state preservation.
def masked_copy(conf, keys):
"""
Create copy with only specified keys included.
Parameters:
- conf: Configuration to copy
- keys: List of keys to include in copy
Returns:
New configuration with only specified keys
"""from omegaconf import OmegaConf
# Basic merging
base = OmegaConf.create({"a": 1, "b": {"x": 10}})
override = OmegaConf.create({"b": {"y": 20}, "c": 3})
merged = OmegaConf.merge(base, override)
# Result: {"a": 1, "b": {"x": 10, "y": 20}, "c": 3}
# Multiple config merging
config = OmegaConf.merge(base_config, user_config, cli_overrides)
# Performance-focused merging (unsafe)
result = OmegaConf.unsafe_merge(cfg1, cfg2, cfg3)from omegaconf import OmegaConf
config = OmegaConf.create({
"database": {
"connections": [
{"host": "primary", "port": 5432},
{"host": "replica", "port": 5432}
]
},
"features": {"auth": True, "logging": "???"}
})
# Select values
host = OmegaConf.select(config, "database.connections[0].host") # "primary"
missing = OmegaConf.select(config, "missing.key", default="fallback") # "fallback"
# Update values
OmegaConf.update(config, "database.connections[1].port", 5433)
OmegaConf.update(config, "new.nested.setting", {"enabled": True})
# Check for missing values
missing_keys = OmegaConf.missing_keys(config) # {"features.logging"}from omegaconf import OmegaConf
# Config with interpolations
config = OmegaConf.create({
"server": {
"host": "localhost",
"port": 8080,
"url": "http://${server.host}:${server.port}",
"api_url": "${server.url}/api/v1"
},
"client": {
"endpoint": "${server.api_url}/users"
}
})
# Before resolution
print(config.server.url) # "http://${server.host}:${server.port}"
# Resolve all interpolations
OmegaConf.resolve(config)
# After resolution
print(config.server.url) # "http://localhost:8080"
print(config.client.endpoint) # "http://localhost:8080/api/v1/users"from omegaconf import OmegaConf
config = OmegaConf.create({
"database": {"host": "localhost", "port": 5432},
"cache": {"enabled": True, "ttl": 300},
"logging": {"level": "INFO", "file": "/var/log/app.log"}
})
# Create masked copy with subset of keys
db_config = OmegaConf.masked_copy(config, ["database"])
# Result: {"database": {"host": "localhost", "port": 5432}}
# Bulk updates with merging
updates = {"database": {"pool_size": 10}, "cache": {"ttl": 600}}
for key, value in updates.items():
OmegaConf.update(config, key, value, merge=True)
# Check configuration completeness
if OmegaConf.missing_keys(config):
print("Configuration has missing mandatory values")Install with Tessl CLI
npx tessl i tessl/pypi-omegaconf