简体   繁体   中英

extend list from default config in Hydra

I have a list that is defined in my defaults
configuration file base_list :

list:
  - 1
  - 2

I know I can override the list values in the config file:

defaults:
  - base_list
list:
  - 3
  - 4

which results

list:
- 3
- 4

However, I look for a way to extend the list, and the desired output is:

list:
- 1
- 2
- 3
- 4

Any idea how to do this?

This is not supported directly. However, you can achieve the desired behavior using the built-in OmegaConf resolvers oc.dict.* .

Those resolvers allow you to access the keys or values of a config node as a list:

cfg = OmegaConf.create(
    {
        "workers": {
            "node3": "10.0.0.2",
            "node7": "10.0.0.9",
        },
        "nodes": "${oc.dict.keys: workers}",
        "ips": "${oc.dict.values: workers}",
    }
)
# Keys are copied from the DictConfig:
show(cfg.nodes)
# -> type: ListConfig, value: ['node3', 'node7']
# Values are dynamically fetched through interpolations:
show(cfg.ips)
# -> type: ListConfig, value: ['${workers.node3}', '${workers.node7}']
assert cfg.ips == ["10.0.0.2", "10.0.0.9"]

With this, you can compose a dictionary and have a node that access the values or keys as if they are a list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM