{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "from hydra.experimental import initialize, compose" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "This is what our config structure looks like:\n", "\n", "$ tree hydra/test_utils/configs/cloud_infra_example/\n", "hydra/test_utils/configs/cloud_infra_example/\n", "├── application\n", "│   ├── bananas.yaml\n", "│   └── donkey.yaml\n", "├── cloud_provider\n", "│   ├── aws.yaml\n", "│   └── local.yaml\n", "├── config.yaml\n", "├── db\n", "│   ├── mysql.yaml\n", "│   └── sqlite.yaml\n", "├── environment\n", "│   ├── production.yaml\n", "│   └── testing.yaml\n", "└── __init__.py\n", "\n", "Note the __init__.py at the end, it can be empty but you need it to help Python fine the config files." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "initialize(config_path=\"../../hydra/test_utils/configs/cloud_infra_example\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n", "\n" ] } ], "source": [ "# Compose empty config (not super useful)\n", "cfg=compose()\n", "print(cfg.pretty())" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "db:\n", " driver: sqlite\n", " user: ???\n", " pass: ???\n", " file: test.db\n", "\n" ] } ], "source": [ "# Compose a config from scratch\n", "# Missing actual user and password as those are environment specific\n", "cfg=compose(overrides=[\"+db=sqlite\"])\n", "print(cfg.pretty())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "db:\n", " driver: mysql\n", " user: mysql\n", " pass: r4Zn*jQ9JB1Rz2kfz\n", "donkey:\n", " name: kong\n", " rank: king\n", "\n" ] } ], "source": [ "# compose application specific config (in this case the applicaiton is \"donkey\")\n", "cfg=compose(overrides= [\"+db=mysql\", \"+environment=production\", \"+application=donkey\"])\n", "print(cfg.pretty())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "db:\n", " driver: sqlite\n", " user: test\n", " pass: test\n", " file: test.db\n", "cloud:\n", " name: local\n", " host: localhost\n", " port: 9876\n", "\n" ] } ], "source": [ "# compose from config.yaml, this composes a bunch of defaults in:\n", "cfg=compose(config_name=\"config.yaml\")\n", "print(cfg.pretty())" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "db:\n", " driver: sqlite\n", " user: mysql\n", " pass: r4Zn*jQ9JB1Rz2kfz\n", " file: test.db\n", "cloud:\n", " name: aws\n", " api_key: ${env:AWS_API_KEY}\n", " ami_id: MY_AMI_ID\n", "\n" ] } ], "source": [ "# compose from config.yaml and override from the default testing\n", "# environment to production and cloud provider to aws.\n", "# Also override the ami id\n", "cfg=compose(\n", " config_name=\"config.yaml\", \n", " overrides=[\n", " \"cloud_provider=aws\",\n", " \"environment=production\",\n", " \"cloud.ami_id=MY_AMI_ID\",\n", " ]\n", ")\n", "print(cfg.pretty())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 2 }