在类中使用 Hydra 配置

发布于 2025-01-18 01:34:48 字数 1296 浏览 3 评论 0原文

我正在尝试在项目中使用Hydra工具,并想将Decorator用于类功能,

import hydra
from hydra.core.config_store import ConfigStore

from src.config import RecordingConfig

cs = ConfigStore.instance()
cs.store(name="recording_config", node=RecordingConfig)

class HydraClassTest:
    @hydra.main(config_path="../src/conf/", config_name="conf")
    def __init__(self, conf: RecordingConfig):
        print(conf)

def main():
    HydraClassTest()

if __name__ == "__main__":
    main()

但是我遇到了这一错误

TypeError: __init__() missing 1 required positional argument: 'conf'

,我是否应该将配置从外部传递到课堂? (例如,通过在主函数上使用装饰器并将配置作为参数传递给初始化器,这起作用)
还是以错误的方式使用装饰器? 如果是打算的,是否有一些设计原因为什么人们不想这样做?

我已经检查了是否正确地使用了配置,该配置是否正确地使用了装饰器。

import hydra
from hydra.core.config_store import ConfigStore

from src.config import RecordingConfig

cs = ConfigStore.instance()
cs.store(name="recording_config", node=RecordingConfig)

class HydraClassTest:
    def __init__(self, conf: RecordingConfig):
        print(conf)

@hydra.main(config_path="../src/conf/", config_name="conf")
def main(conf: RecordingConfig):
    HydraClassTest(conf)

if __name__ == "__main__":
    main()

这给了我预期的结果。

I am trying to use the hydra tool in my project and would like to use the decorator for class functions

import hydra
from hydra.core.config_store import ConfigStore

from src.config import RecordingConfig

cs = ConfigStore.instance()
cs.store(name="recording_config", node=RecordingConfig)

class HydraClassTest:
    @hydra.main(config_path="../src/conf/", config_name="conf")
    def __init__(self, conf: RecordingConfig):
        print(conf)

def main():
    HydraClassTest()

if __name__ == "__main__":
    main()

But I get the error

TypeError: __init__() missing 1 required positional argument: 'conf'

Is this intended and should I pass the configuration from the outside to the class? (For example by using the decorator on the main function and passing the configuration as a parameter to the initializer, this works)
Or am using the decorator in a wrong way?
If it is intended, is there some design reason why one would not want to do it that way?

I have checked whether I used the decorator correctly by passing the configuration through the main function, that worked.

import hydra
from hydra.core.config_store import ConfigStore

from src.config import RecordingConfig

cs = ConfigStore.instance()
cs.store(name="recording_config", node=RecordingConfig)

class HydraClassTest:
    def __init__(self, conf: RecordingConfig):
        print(conf)

@hydra.main(config_path="../src/conf/", config_name="conf")
def main(conf: RecordingConfig):
    HydraClassTest(conf)

if __name__ == "__main__":
    main()

This gives me the expected result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-01-25 01:34:48

@Hydra.main() 不适合此用例。它被设计为在应用程序中使用一次,并且有许多副作用(更改工作目录、配置日志记录等)。

请改用 Compose API

@hydra.main() is not appropriate for this use case. It's designed to be used once in an application and it has many side effects (changing working directory, configuring logging etc).

Use the Compose API instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文