Skip to content

Instantly share code, notes, and snippets.

@shagunsodhani
Created July 18, 2020 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shagunsodhani/f5bad29584e7cddee47526257237970e to your computer and use it in GitHub Desktop.
Save shagunsodhani/f5bad29584e7cddee47526257237970e to your computer and use it in GitHub Desktop.
hydra_isse_791_2
# 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:
hydra.utils.instantiate(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