Skip to content

Instantly share code, notes, and snippets.

@kowey
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowey/330900aac2fcfa7a0e16 to your computer and use it in GitHub Desktop.
Save kowey/330900aac2fcfa7a0e16 to your computer and use it in GitHub Desktop.
def add_fold_choice_args(psr):
"ability to select a subset of the data according to a fold"
fold_grp = psr.add_argument_group('fold selection')
fold_grp.add_argument("--fold-file", metavar="FILE",
type=argparse.FileType('r'),
help="read folds from this json file")
fold_grp.add_argument("--fold", metavar="INT",
type=int,
help="fold to select")
def validate_fold_choice_args(wrapped):
"""
Given a function that accepts an argparsed object, check
the fold arguments before carrying on.
The idea here is that --fold and --fold-file are meant to
be used together (xnor)
This is meant to be used as a decorator, eg.::
@validate_fold_choice_args
def main(args):
blah
"""
@wraps(wrapped)
def inner(args):
"die if fold args are incomplete"
if args.fold_file and not args.fold:
# I'd prefer to use something like ArgumentParser.error
# here, but I can't think of a convenient way of passing
# the parser object in or otherwise obtaining it
sys.exit("arg error: --fold is required when "
"--fold-file is present")
elif args.fold and not args.fold_file:
sys.exit("arg error: --fold-file is required when "
"--fold is present")
wrapped(args)
return inner
def config_argparser(psr):
"add subcommand arguments to subparser"
add_common_args(psr)
add_decoder_args(psr)
add_fold_choice_args(psr)
psr.add_argument("--attachment-model", "-A", default=None,
help="provide saved model for prediction of "
"attachment (only with -T option)")
psr.add_argument("--relation-model", "-R", default=None,
help="provide saved model for prediction of "
"relations (only with -T option)")
psr.add_argument("--output", "-o",
default=None,
required=True,
metavar="DIR",
help="save predicted structures here")
psr.set_defaults(func=main)
@validate_fold_choice_args
def main(args):
"subcommand main"
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment