datadings.tools.argparse module

A collection of useful helper functions to create argument parsers. Using pre-defined arguments ensures that arguments are consistent across different tools in datadings.

All helper functions in this module follow the convention that a function called argument_indir adds the indir argument to the given parser, including additional configuration and help text.

Note

Any of the default arguments given by the argument_* functions can be overwritten. For example, argument_shuffle defines choices = ['yes', 'no']. If those are too formal for your liking, you can also use argument_shuffle(parser, choices=['yeah', 'naw']. But please don’t.

class datadings.tools.argparse.MinMaxAction(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]

Bases: Action

Action to clamp value between given min and max values. Create subclass to set min_value and max_value:

class Action(MinMaxAction):
    min_value = 1
    max_value = 7

parser.add_argument('onetoseven', action=Action)
max_value = None
min_value = None
class datadings.tools.argparse.YesNoAction(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)[source]

Bases: Action

Action like store_true that checks if value == yes.

datadings.tools.argparse.argument_calculate_weights(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '--calculate-weights',
    action='store_true',
    help='Calculate median-frequency class weights.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_indir(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    'indir',
    type=<class 'str'>,
    default='.',
    metavar='INDIR',
    help='Directory that contains dataset source files.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_infile(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    'infile',
    type=<class 'str'>,
    default=None,
    help='Input file.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_no_confirm(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '-y', '--no-confirm',
    dest='no_confirm',
    action='store_true',
    help='Don’t require user interaction.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_outdir(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '-o', '--outdir',
    type=<class 'str'>,
    default=None,
    metavar='PATH',
    help='Output directory. Defaults to indir.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_outfile(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '-o', '--outfile',
    type=<class 'str'>,
    default=None,
    metavar='PATH',
    help='Output file.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_outfile_positional(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    'outfile',
    type=<class 'str'>,
    help='Output file.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_outfiles(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '-o', '--outfiles',
    type=<class 'str'>,
    default=None,
    metavar='PATH',
    help='Output files.',
    nargs='+',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_shuffle(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '--shuffle',
    default='yes',
    choices=['yes', 'no'],
    action=<class 'datadings.tools.argparse.YesNoAction'>,
    help='Write samples in random order. (default: yes)',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_skip_verification(*args, **kwargs)

Add the following argument to the given ArgumentParser:

parser.add_argument(
    '-s', '--skip-verification',
    action='store_true',
    help='Skip verification of source files.',
)
Parameters:
  • parser – Call add_argument on this ArgumentParser.

  • args – Additional positional arguments for add_argument.

  • kwargs – Additional keyword arguments for add_argument. Can override keyword arguments specified above.

datadings.tools.argparse.argument_threads(parser, default=1, max_threads=0)[source]

Add threads argument to parser.

Parameters:
  • parser – Argument is added here.

  • default – Default number of threads.

  • max_threads – Maximum number of threads. If >0, use given number. If 0 use cpu_count(). if <0, use -max_threads*cpu_count()

datadings.tools.argparse.make_parser(description, indir=True, outdir=True, no_confirm=True, skip_verification=True, shuffle=True, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, **kwargs) ArgumentParser[source]

Create an ArgumentParser with a set of common arguments.

Parameters:
  • description – Description text displayed before arguments. Usually __doc__ is fine.

  • indir – If True, add indir argument.

  • outdir – If True, add outdir argument.

  • no_confirm – If True, add no_confirm argument.

  • skip_verification – If True, add skip_verification argument.

  • shuffle – If True, add shuffle argument.

  • formatter_class – Description formatter, defaults to raw.

  • kwargs – kwargs given to ArgumentParser.

Returns:

ArgumentParser.

datadings.tools.argparse.make_parser_simple(description, indir=False, outdir=False, no_confirm=False, skip_verification=False, shuffle=False, formatter_class=<class 'argparse.RawDescriptionHelpFormatter'>, **kwargs) ArgumentParser[source]

Same as make_parser(), but add no arguments by default.