Skip to content

Instantly share code, notes, and snippets.

@shagunsodhani
Created July 18, 2020 02:10
Show Gist options
  • Save shagunsodhani/cafab41a2ce43d3cb4604c652725aa02 to your computer and use it in GitHub Desktop.
Save shagunsodhani/cafab41a2ce43d3cb4604c652725aa02 to your computer and use it in GitHub Desktop.
hydra_issue_791
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from dataclasses import dataclass
from typing import Any
from omegaconf import MISSING, DictConfig, OmegaConf
import hydra
from hydra.core.config_store import ConfigStore
from hydra.types import ObjectConf
cs = ConfigStore.instance()
def name_print(name):
print(name)
@dataclass
class NameConf:
name: str = ""
cs.store(
group="fn",
name="NR",
node=ObjectConf(target="my_app.name_print", params=NameConf(),),
)
@dataclass
class MySQLConfig:
driver: str = "mysql"
host: str = "localhost"
port: int = 3306
@dataclass
class PostGreSQLConfig:
driver: str = "postgresql"
host: str = "localhost"
port: int = 5432
timeout: int = 10
# Config is extending DictConfig to allow type safe access to the pretty() function below
@dataclass
class Config(DictConfig):
db: Any = MISSING
fn: Any = MISSING
cs.store(name="config", node=Config)
cs.store(group="db", name="mysql", node=MySQLConfig)
cs.store(group="db", name="postgresql", node=PostGreSQLConfig)
@hydra.main(config_name="config")
def my_app(cfg: Config) -> None:
print(dict(cfg.fn))
print(cfg.pretty())
if __name__ == "__main__":
my_app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment