Skip to content

Instantly share code, notes, and snippets.

@jclosure
Last active April 25, 2023 21:53
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 jclosure/fd9291a82900078f225cd08cfedb5ebb to your computer and use it in GitHub Desktop.
Save jclosure/fd9291a82900078f225cd08cfedb5ebb to your computer and use it in GitHub Desktop.
Omegaconf: Unobtrusive way to mix env vars over plan data yaml config
import os
from omegaconf import OmegaConf
os.environ.pop("PASSWORD", None)
os.environ.pop("PORT", None)
os.environ["PASSWORD"] = "foo"
os.environ["PORT"] = '666'
file_cfg = OmegaConf.create("inference_worker: {user: Ohad, password: secret, port: 123}")
env_cfg = OmegaConf.create("inference_worker: {port: '${oc.decode:${oc.env:PORT, null}}'}")
# populate from env (resolve)
OmegaConf.resolve(env_cfg)
# mixin cfg=file<-env<-cli
cfg = OmegaConf.merge(file_cfg, env_cfg, OmegaConf.from_cli())
print(OmegaConf.to_yaml(cfg.inference_worker))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment