问题
我正在实验OmegaConf库,并且在OmegaConf.merge方法上遇到了问题。参考项目结构,有两个配置文件。当使用OmegaConf.merge方法时,它会按照预期将这两个配置组合成一个。然而,这让我感到困惑,因为我很难确定哪些配置属于特定的配置文件。
我之所以希望合并这些配置,是因为我希望有一个全局变量,所有的配置都可以跨模块使用。
由于以前所面临的限制,我不希望使用Hydra库。
项目结构
project_root
├── config
│ ├── example1.yaml
│ └── example2.yaml
└── src
└── main.py代码
config/example1.yaml
file: foods.txt
check: --A--
sample: XXA1config/example2.yaml
items:
- XXA1
- XXA66
users:
- user1
- user2src/main.py
import omegaconf
from omegaconf import OmegaConf
CONFIG = None # Configurations stored here which will be imported across multiple modules.
example1_config = OmegaConf.load('../config/example1.yaml')
example2_config = OmegaConf.load('../config/example2.yaml')
CONFIG = OmegaConf.merge(example1_config, example2_config )
print(OmegaConf.to_yaml(merged_configs))电流输出
file: foods.txt
check: --A--
sample: XXA1
items:
- XXA1
- XXA66
users:
- user1
- user2期望输出
example1:
file: foods.txt
check: --A--
sample: XXA1
example2:
items:
- XXA1
- XXA66
users:
- user1
- user2发布于 2022-09-30 17:33:47
OmegaConf.merge结合了输入的键,就像python的内置dict.update方法一样。
要获得所需的输出,您需要在输入中添加一个嵌套级别:
CONFIG = OmegaConf.merge(
{"example1": example1_config},
{"example2": example2_config},
)https://stackoverflow.com/questions/73909847
复制相似问题