Skip to content

Instantly share code, notes, and snippets.

@g-simmons
Created May 10, 2023 22:10
Show Gist options
  • Save g-simmons/0f18388ac58297bec11657f40fa885af to your computer and use it in GitHub Desktop.
Save g-simmons/0f18388ac58297bec11657f40fa885af to your computer and use it in GitHub Desktop.
Make a dvc-compatible parameter grid from command line arguments
import yaml
from itertools import product, starmap
from collections import namedtuple
import click
judgement_styles = [1, 2, 3, 4, 5]
groups = ["liberal", "conservative"]
def named_product(**items):
Product = namedtuple("Product", items.keys())
return [x._asdict() for x in starmap(Product, product(*items.values()))]
def yamlgrid(**items):
return yaml.dump(named_product(**items), default_flow_style=False)
@click.command(
name="yamlgrid",
context_settings=dict(
ignore_unknown_options=True,
allow_extra_args=True,
),
)
@click.pass_context
def cli(ctx):
args = [arg.replace("--", "").split("=") for arg in ctx.args]
args = {arg[0]: [x.strip() for x in arg[1].split(",")] for arg in args}
print(yamlgrid(**args))
if __name__ == "__main__":
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment