python,Hydra,从YAML到列表的负载变量
我得到了一个以下代码:
# settings.yaml
my_class:
columns_id: ${oc.env:MY_LIST}
all_columns: {"all_columns_as_str": ${oc.env:MY_LIST}}
我的变量为my_list = a,b,c
,然后我得到:
# hydra_conf.py
from dataclasses import dataclass
@dataclass
class MyClass:
columns_id: list
all_columns: dict[str, str]
然后我使用所有这些都像下面:
# main.py
import hydra
from hydra_conf import MyClass
@hydra.main(config_path="settings", config_name="settings")
def main(cfg: MyClass) -> None:
assert isinstance(cfg.columns_id, list)
assert isinstance(cfg.all_columns, dict)
assert cfg.all_columns['all_columns_as_str'] == 'a,b,c'
我需要如何配置yml.file < /code>或我的
all_columns 作为hydra_conf.py
load columns_id
as list
anddict [str,str]
?
I got a below code:
# settings.yaml
my_class:
columns_id: ${oc.env:MY_LIST}
all_columns: {"all_columns_as_str": ${oc.env:MY_LIST}}
where my variable is MY_LIST=a,b,c
then I got:
# hydra_conf.py
from dataclasses import dataclass
@dataclass
class MyClass:
columns_id: list
all_columns: dict[str, str]
and then I use it all like below:
# main.py
import hydra
from hydra_conf import MyClass
@hydra.main(config_path="settings", config_name="settings")
def main(cfg: MyClass) -> None:
assert isinstance(cfg.columns_id, list)
assert isinstance(cfg.all_columns, dict)
assert cfg.all_columns['all_columns_as_str'] == 'a,b,c'
How I need to configure my yml.file
or my hydra_conf.py
file to load columns_id
as list
and all_columns
as a dict[str, str]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下:
以下是这样做的方法:
my_class_schema
,这意味着您的yaml文件将与myClass
定义的架构合并。请参阅结构化configs “ https://hydra.cc/docs/tutorials/structured_config/schema/” rel =“ nofollow noreferrer”>结构化配置模式)有关此的更多信息。“ [$ {oc.env:my_list}]”
在解决插值后,在中导致
“ [a,b,c]”
。oc.decode
oc.decode resolver resolver解析字符串“ [a,b,c]”
,导致[“ a”,“ b”,“ c”]
。Give this a try:
Here's how this works:
my_class_schema
in the defaults list means that your yaml file will be merged with the schema defined byMyClass
. See the Hydra docs on Structured Configs (and specifically on using a Structured Config Schema) for more info about this."[${oc.env:MY_LIST}]"
results in"[a,b,c]"
after the interpolation is resolved.oc.decode
resolver parses the string"[a,b,c]"
, resulting in["a", "b", "c"]
.