Programmatic Usage

An empty configuration can be constructed using Config:

from strataconf import Config

config = Config(default_config_package="my_package/configs")

If default_config_package is passed in, it will be used to resolve relative package paths. See Package Paths for details.

Settings can be retrieved from the configuration system using get(path) by passing in a dot-separated path to the property, while has(path) checks whether a property exists before retrieving it:

if config.has("server.port"):
    port = config.get("server.port")

Settings can be added into the defaults, config or overrides layer using native Python dict or OmegaConf DictConfig objects:

values = {
  "server": {
    "port": 9000
  }
}

config.add_defaults(values)
config.add_config(values)
config.add_overrides(values)

Alternatively, settings can be loaded directly from YAML files into these layers:

config.load_defaults("configs/defaults.yaml")
config.load_config("configs/config.yaml")
config.load_overrides("configs/overrides.yaml")

Two special parsers return DictConfig objects, whose return values can then be added to any layer:

# Extract the field defaults of a settings class
setting_config = ConfigFieldParser.parse(MySettings, key="setting")
config.add_defaults(setting_config)

# Parse command-line arguments defined in `args.yaml` in the current working directory
args_config = ConfigArgsParser.parse(config=config, spec="args.yaml", key="cli")
config.add_overrides(args_config)

Tracking which configuration properties are actually used can be enabled with track_usage() and retrieved with get_usage().

To help with debugging, the configuration system provides two methods which return the configuration as a YAML representation:

Finally, stats on how many properties are stored in each layer can be retrieved via stats(), and a clone of the config can be created using clone().