-
Hello! I would like to override an element of a ListConfig using structured config. I tried to make a toy example by adapting the example in the doc: # System imports
from dataclasses import dataclass, field
from typing import Any, List
# Third-party imports
import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import MISSING, ListConfig, OmegaConf
@dataclass
class MySQLConfig:
host: str = "mysql"
# port: int = 3306 <-- original example
ports: ListConfig = field(default_factory=lambda: ListConfig([3306, 3307, 3308]))
@dataclass
class YourSQLConfig(MySQLConfig):
defaults: List[Any] = field(default_factory=lambda: ["mysql"])
host: str = "your_sql"
########################################################################
# Question: what code should go here to override an element of ports here, say, ports[2]=4000?
# From command line, I can do `db.ports.2=4000`.
########################################################################
defaults = [
# Load the config "mysql" from the config group "db"
{"db": "yoursql"}
]
@dataclass
class Config:
# this is unfortunately verbose due to @dataclass limitations
defaults: List[Any] = field(default_factory=lambda: defaults)
# Hydra will populate this field based on the defaults list
db: Any = MISSING
cs = ConfigStore.instance()
cs.store(group="db", name="mysql", node=MySQLConfig)
cs.store(group="db", name="yoursql", node=YourSQLConfig)
cs.store(name="config", node=Config)
@hydra.main(version_base=None, config_name="config")
def my_app(cfg: Config) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
my_app() |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @dnnspark, Option 1: create a new
|
Beta Was this translation helpful? Give feedback.
Hi @dnnspark,
Here are a few options:
Option 1: create a new
field
object:Option 2: pass an override at the command line:
Option 3: Use the
oc.dict.values
trick (detailed here).