lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
386 stars 47 forks source link

Load python file as config file. #180

Closed lkhphuc closed 1 year ago

lkhphuc commented 1 year ago

Is your feature request related to a problem? Please describe. Config file currently support as json and yaml. I wonder whether it is possible to use a python file containing the dataclasses as the config file directly.

Describe the solution you'd like For example in main.py:

@dataclass
class TrainConfig:
    """Training configuration."""

    workers: int = 8  # The number of workers for training
    exp_name: str = "default_exp"  # The experiment name

def main(args=None) -> None:
    cfg = simple_parsing.parse(config_class=TrainConfig, args=args)
    print(f"Training {cfg.exp_name} with {cfg.workers} workers...")

In a cfg/baseline.py:

@dataclass
class BaselineConfig(TrainConfig):
    """Training configuration."""

    workers: int = 1  # The number of workers for training
    exp_name: str = "baseline"  # The experiment name

Now the command line accepts: python main.py --config_path=cfg/baseline.py

lebrice commented 1 year ago

Hi @lkhphuc , thanks for posting!

Hmmmm. I'm slightly confused. Could you explain why this should be allowed? Does it enable something that json/yaml files don't?

This might also be a use-case for subgroups. Have you tried those before?

lebrice commented 1 year ago

I'll go ahead and close this for now, but feel free to ping me (or reopen this if you can) if you think this deserves some attention.