A flexible configuration library that provides hierarchical configuration management with YAML support, variable interpolation, and type validation
—
Methods for creating OmegaConf configurations from various sources including dictionaries, YAML files, dataclasses, command-line arguments, and dot notation lists.
Creates OmegaConf configurations from various Python objects including dictionaries, lists, dataclasses, and other structured objects.
def create(obj=None, parent=None, flags=None):
"""
Create a config from a variety of inputs.
Parameters:
- obj: Input object (dict, list, dataclass, object, or None for empty config)
- parent: Parent node for nested configs
- flags: Dict of configuration flags (struct, readonly, etc.)
Returns:
DictConfig or ListConfig based on input type
"""def structured(obj, parent=None, flags=None):
"""
Create config from structured object (alias for create).
Parameters:
- obj: Structured object (dataclass, attrs class, or instance)
- parent: Parent node for nested configs
- flags: Dict of configuration flags
Returns:
DictConfig with type validation based on structured schema
"""Loads configuration from YAML files with support for complex nested structures and OmegaConf-specific features.
def load(file_):
"""
Load configuration from YAML file or file-like object.
Parameters:
- file_: File path (str/Path) or file-like object
Returns:
DictConfig containing the loaded configuration
Raises:
- FileNotFoundError: If file doesn't exist
- yaml.YAMLError: If YAML parsing fails
"""def save(config, f, resolve=False):
"""
Save configuration object to a file.
Parameters:
- config: Configuration object (DictConfig, ListConfig, dataclass, or attrs)
- f: File path (str/Path) or file-like object
- resolve: Whether to resolve interpolations before saving (default: False)
Notes:
- Saves in YAML format
- Automatically converts dataclass/attrs objects to configs before saving
"""Usage examples:
# Load from file path
config = OmegaConf.load("config.yaml")
# Save to file path
OmegaConf.save(config, "output.yaml")
# Save with resolved interpolations
OmegaConf.save(config, "resolved_config.yaml", resolve=True)
# Load from file object
with open("config.yaml") as f:
config = OmegaConf.load(f)
# Save to file object
with open("output.yaml", "w") as f:
OmegaConf.save(config, f)Creates configuration from command-line arguments using dot notation syntax.
def from_cli(args_list=None):
"""
Create config from command line arguments.
Parameters:
- args_list: List of arguments (defaults to sys.argv[1:])
Returns:
DictConfig with parsed command line overrides
Examples:
- ["key=value"] -> {"key": "value"}
- ["nested.key=123"] -> {"nested": {"key": 123}}
- ["list=[1,2,3]"] -> {"list": [1, 2, 3]}
"""Usage example:
# From sys.argv automatically
config = OmegaConf.from_cli()
# From explicit argument list
config = OmegaConf.from_cli(["database.host=localhost", "debug=true"])Creates configuration from lists of dot notation strings.
def from_dotlist(dotlist):
"""
Create config from list of dot notation assignments.
Parameters:
- dotlist: List of "key=value" strings using dot notation
Returns:
DictConfig with parsed assignments
Examples:
- ["a=1", "b.c=2"] -> {"a": 1, "b": {"c": 2}}
- ["list=[1,2,3]"] -> {"list": [1, 2, 3]}
"""Usage example:
dotlist = [
"server.host=localhost",
"server.port=8080",
"features=[auth,logging]"
]
config = OmegaConf.from_dotlist(dotlist)
# Result: {"server": {"host": "localhost", "port": 8080}, "features": ["auth", "logging"]}Configuration flags control behavior during creation and usage:
# Create read-only config
config = OmegaConf.create({"key": "value"}, flags={"readonly": True})
# Create struct config (no new keys allowed)
config = OmegaConf.create({"key": "value"}, flags={"struct": True})# Create hierarchical configs with parent relationships
parent_config = OmegaConf.create({"parent_key": "parent_value"})
child_config = OmegaConf.create({"child_key": "child_value"}, parent=parent_config)# Create empty configurations
empty_dict = OmegaConf.create() # Empty DictConfig
empty_list = OmegaConf.create([]) # Empty ListConfigInstall with Tessl CLI
npx tessl i tessl/pypi-omegaconf