Reputation: 2705
I'd like to convert a OmegaConf/Hydra config to a nested dictionary/list. How can I do this?
Upvotes: 21
Views: 25469
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
Reputation: 33696
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