pythonfb-hydra

How to handle non-string config with Hydra?


Consider the following example:

from loguru import logger
import sys


logger.add(sys.stdout, level="INFO")

It is pretty simple to create a hydra config for the level parameter:

log:
  level: INFO

On the other hand, I don't know how to create a config parameter for sys.stdout? I tried to do something like:

log:
  level: INFO
  stream: sys.stdout

However, in the script:

@hydra.main(version_base=None, config_path="../config", config_name="main")
set_log(cfg: DictConfig):
  logger.add(cfg.log.stream, level=cfg.log.level)

cfg.log.stream gives the string "sys.stdout". How to solve it?


Solution

  • Maybe not the most elegant solution, but the shortest one is the resort to eval()

    @hydra.main(version_base=None, config_path="../config", config_name="main")
    set_log(cfg: DictConfig):
      logger.add(eval(cfg.log.stream), level=cfg.log.level)