Created
July 18, 2020 02:20
-
-
Save shagunsodhani/f5bad29584e7cddee47526257237970e to your computer and use it in GitHub Desktop.
hydra_isse_791_2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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