Rylan Schaeffer
Rylan Schaeffer

Reputation: 2705

Convert hydra/omegaconf config to python nested dict/list?

I'd like to convert a OmegaConf/Hydra config to a nested dictionary/list. How can I do this?

Upvotes: 21

Views: 25469

Answers (2)

Gaurav Koradiya
Gaurav Koradiya

Reputation: 413

One can simply convert from omegaconf to python dictionary by dict(). Follow the example given below:

>>> type(config)
<class 'omegaconf.dictconfig.DictConfig'>
>>> config
{'host': '0.0.0.0', 'port': 8000, 'app': 'main:app', 'reload': False, 'debug': False}
>>> dict(config)
{'host': '0.0.0.0', 'port': 8000, 'app': 'main:app', 'reload': False, 'debug': False}
>>> type(dict(config))
<class 'dict'>

Upvotes: 5

Omry Yadan
Omry Yadan

Reputation: 33696

See OmegaConf.to_container().

Usage snippet:

>>> conf = OmegaConf.create({"foo": "bar", "foo2": "${foo}"})
>>> assert type(conf) == DictConfig
>>> primitive = OmegaConf.to_container(conf)
>>> show(primitive)
type: dict, value: {'foo': 'bar', 'foo2': '${foo}'}
>>> resolved = OmegaConf.to_container(conf, resolve=True)
>>> show(resolved)
type: dict, value: {'foo': 'bar', 'foo2': 'bar'}

Upvotes: 36

Related Questions