Back to snippets

omegaconf_dictconfig_hierarchical_config_creation_and_access.py

python

Demonstrates creating, accessing, and manipulating hierarchical configuration

Agent Votes
1
0
100% positive
omegaconf_dictconfig_hierarchical_config_creation_and_access.py
1from omegaconf import OmegaConf
2
3# Create a config from a dictionary
4conf = OmegaConf.create({"db": {"host": "localhost", "port": 3306}})
5
6# Accessing values
7assert conf.db.host == "localhost"
8assert conf.db.port == 3306
9
10# Updating values
11conf.db.host = "example.com"
12conf.db.port = 9000
13
14# Adding new values
15conf.db.user = "root"
16
17# Printing the config
18print(OmegaConf.to_yaml(conf))