Created
March 6, 2021 10:29
-
-
Save omry/09a2b971c83abce3970e2648f3785977 to your computer and use it in GitHub Desktop.
Hydra C++ example
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
app: ${hydra:runtime.cwd}/main | |
db: | |
driver: mysql | |
user: omry | |
password: secret |
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
#include <iostream> | |
#include <iomanip> | |
#include <nlohmann/json.hpp> | |
#include <fstream> | |
using json = nlohmann::json; | |
int main() | |
{ | |
std::ifstream file("config.json"); | |
json j = json::parse(file); | |
// pretty print with indent of 4 spaces | |
std::cout << "Config printed in C++\n" << std::setw(4) << j << '\n'; | |
} | |
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
from omegaconf import DictConfig, OmegaConf | |
import hydra | |
import json | |
import subprocess | |
@hydra.main(config_name="config") | |
def my_app(cfg: DictConfig) -> None: | |
container = OmegaConf.to_container(cfg, resolve=True) | |
with open('config.json', 'w') as f: | |
json.dump(container, f) | |
subprocess.check_call([cfg.app]) | |
if __name__ == "__main__": | |
my_app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment