我正试图自动转换
from omegaconf import OmegaConf
s = """
nodes:
node1:
group: standard
status: online
node2:
group: small
status: online
node3:
group: standard
status: offline
"""到节点列表中,其中"node1/2/3“是节点的名称:
from dataclasses import dataclass
from typing import List
@dataclass
class Node:
name: str
group: str
status: str
@dataclass
class Config:
nodes: List[Node]使用
conf = OmegaConf.create(s)
schema = OmegaConf.structured(Config)
merged_conf = OmegaConf.merge(schema, conf)是否有这方面的机制?如果我从盒子里拿出来试试看
omegaconf.errors.ConfigTypeError: Cannot merge DictConfig with ListConfig发布于 2022-04-27 16:22:33
错误告诉您,您正试图将字典与不支持的列表合并。您的架构说节点是一个List[Node],但是您的YAML字符串包含一个映射(词典的YAML术语)。
要么更改您的架构以指示节点是一个Dict:
@dataclass
class Config:
nodes: Dict[str, Node]或将YAML字符串更改为包含列表:
s = """
nodes:
- group: standard
status: online
- group: small
status: online
- group: standard
status: offline
"""https://stackoverflow.com/questions/72030476
复制相似问题