File size: 1,188 Bytes
ff88fc5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from omegaconf import DictConfig
import hydra
class DBConnection:
def connect(self) -> None:
pass
class MySQLConnection(DBConnection):
def __init__(self, host: str, user: str, password: str) -> None:
self.host = host
self.user = user
self.password = password
def connect(self) -> None:
print(
f"MySQL connecting to {self.host} with user={self.user} and password={self.password}"
)
class PostgreSQLConnection(DBConnection):
def __init__(self, host: str, user: str, password: str, database: str) -> None:
self.host = host
self.user = user
self.password = password
self.database = database
def connect(self) -> None:
print(
f"PostgreSQL connecting to {self.host} with user={self.user} "
f"and password={self.password} and database={self.database}"
)
@hydra.main(config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
connection = hydra.utils.instantiate(cfg.db)
connection.connect()
if __name__ == "__main__":
my_app()
|