Skip to main content

A flexible configuration library

Project description

OmegaConf

OmegaConf is a flexible yaml based configuration library, supporting dot access, deep updates and more.

Loading from a string:

    s = 'hello: world'
    c = OmegaConf.from_string(s)
    assert c == {'hello': 'world'}

Loading from a file

Given the file config.yaml with this content:

env_name: '???' # '???' denotes mandatory variables, see below
num_trials: 1
train_timesteps: 200
render: False

training:
  batch_size: 128

To load config.yaml:

conf = OmegaConf.from_filename('config.yaml')

Access

You can read and write variables using dot and dictionary notations:

assert conf.training.batch_size == 128
assert conf.train_timesteps == 200
assert conf['training'] == 128
assert conf['training']['batch_size'] == 128

Overriding values

You can override configuration values, even with dot notation

conf.env_name = 'NewEnv-v2'
conf.training.batch_size = 256

# Which are (almost) equivalent to
conf.update('env_name', 'NewEnv-v2')
conf.update('training.batch_size', 256)

update() will allow you to automatically create subtree if a node is missing.

Mandatory variables

Accessing variables with the string value ??? will throw an exception, those variables mut be overridden before accessing In the above example, such a variable is env_name

CLI based configuration

To access the CLI arguments (sys.argv), you can get a cli_config: conf = OmegaConf.from_cli() For example, if your CLI arguments are:

python prog.py a=1 b=2 c.a = 3 c.b = 4

Although CLI only allow simple key=value pairs, you can use dot notation to create more complex configurations. The arguments above will contain the config:

a: 1
b: 2
c: {
    a: 3
    b: 4
}

Environment based configuration

Similarly to CLI config, you can have a config that is based on your system environment: conf = OmegaConf.from_env()

Merging configurations

A powerful feature of OmegaConf is the ability to layer configurations in a specific order. Any number of configurations can be merged into a single tree in a specific order: you could do something like:

file1conf = OmegaConf.from_filename('conf1.yaml')
file2conf = OmegaConf.from_filename('conf2.yaml')
envconf = OmegaConf.from_env()
cliconf = OmegaConf.from_cli()
conf = OmegaConf.merge(file1econf, file2econf, envconf, cliconf)

Merged conf would contain the merge of all four configurations, if a value exist in two config, the one mentioned later in the merge will win over.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

omegaconf-1.0.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

omegaconf-1.0.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file omegaconf-1.0.0.tar.gz.

File metadata

  • Download URL: omegaconf-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/3.6.6

File hashes

Hashes for omegaconf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e3af19ca6f9b9ee76d410b456e25018bca2317bd5730bade4263022d69fb3617
MD5 e80e7f7ac5cf3800f133c9c0c55d89b1
BLAKE2b-256 d51eac3e4e5d90d5800ac3e012b96bf5fa3e582f711bcd6659c234948b683a62

See more details on using hashes here.

Provenance

File details

Details for the file omegaconf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: omegaconf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.19.4 CPython/3.6.6

File hashes

Hashes for omegaconf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8769d093877976a51a75753853dfe0bde17d876e1cf869f56fe07d45dfb2b9a
MD5 43004c23a682282c56c8b694612c7800
BLAKE2b-256 33f3989099b3ab0fe1b08320619600c0438a8dfa88dd4456d57de9e5fb0059c7

See more details on using hashes here.

Provenance

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