Skip to content

Instantly share code, notes, and snippets.

@pseeth
Last active October 16, 2020 22:51
Show Gist options
  • Save pseeth/32d6a63d8b16cae3efc0871b254a8b85 to your computer and use it in GitHub Desktop.
Save pseeth/32d6a63d8b16cae3efc0871b254a8b85 to your computer and use it in GitHub Desktop.
ArgBind demo
import argbind
from typing import List
@argbind.bind()
def train(epochs : int = 50):
"""Run training.
Parameters
----------
epochs : int, optional
Number of epochs, by default 50
"""
print('train', epochs)
@argbind.bind()
def evaluate(save_folder : str = 'folder'):
"""Run evaluation.
Parameters
----------
save_folder : str, optional
Where to save, by default '.'
"""
print('evaluate', save_folder)
@argbind.bind()
def run(stages : List[str] = ['train', 'evaluate']):
"""Run stages.
Parameters
----------
stages : List[str], optional
Which stages to run, by default 'train'
"""
for stage in stages:
stage_fn = globals()[stage]
stage_fn() # will do train
if __name__ == "__main__":
args = argbind.parse_args()
with argbind.scope(args):
run()
@pseeth
Copy link
Author

pseeth commented Oct 16, 2020

This is a simple demonstration of a multi-stage program using ArgBind. There are two stages, train and evaluate, each of which can be accessed via the bound run function. Here's the help text:

❯ python demo.py -h
usage: demo.py [-h] [--args.save ARGS.SAVE] [--args.load ARGS.LOAD] [--args.debug ARGS.DEBUG] [--train.epochs TRAIN.EPOCHS] [--evaluate.save_folder EVALUATE.SAVE_FOLDER] [--run.stages RUN.STAGES]

optional arguments:
  -h, --help            show this help message and exit
  --args.save ARGS.SAVE
                        Path to save all arguments used to run script to.
  --args.load ARGS.LOAD
                        Path to load arguments from, stored as a .yml file.
  --args.debug ARGS.DEBUG
                        Print arguments as they are passed to each function.

Generated arguments for function train:
  Run training.

  --train.epochs TRAIN.EPOCHS
                        Number of epochs, by default 50

Generated arguments for function evaluate:
  Run evaluation.

  --evaluate.save_folder EVALUATE.SAVE_FOLDER
                        Where to save, by default '.'

Generated arguments for function run:
  Run stages.

  --run.stages RUN.STAGES
                        Which stages to run, by default 'train'

All the help text is generated from the docstring. The arguments are scoped like func.arg. Here's some sample runs:

❯ python demo.py
train 50
evaluate folder
❯ python demo.py --run.stages="train"
train 50
❯ python demo.py --run.stages="evaluate"
evaluate folder
❯ python demo.py --train.epochs=10
train 10
evaluate folder
❯ python demo.py --train.epochs=10 --evaluate.save_folder=different_folder
train 10
evaluate different_folder

@pseeth
Copy link
Author

pseeth commented Oct 16, 2020

❯ python demo.py --train.epochs=10 --evaluate.save_folder=different_folder --args.save demo.yml
train 10
evaluate different_folder
❯ python demo.py --args.load demo.yml
train 10
evaluate different_folder

Contents of demo.yml:

evaluate.save_folder: different_folder

run.stages:
- train
- evaluate

train.epochs: 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment