Python argparse.ArgumentParser() Examples

The following are code examples for showing how to use argparse.ArgumentParser(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: BERT-Classification-Tutorial   Author: Socialbird-AILab   File: download_glue.py    Apache License 2.0 7 votes vote down vote up
def main(arguments):
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', help='directory to save data to', type=str, default='glue_data')
    parser.add_argument('--tasks', help='tasks to download data for as a comma separated string',
                        type=str, default='all')
    parser.add_argument('--path_to_mrpc',
                        help='path to directory containing extracted MRPC data, msr_paraphrase_train.txt and msr_paraphrase_text.txt',
                        type=str, default='')
    args = parser.parse_args(arguments)

    if not os.path.isdir(args.data_dir):
        os.mkdir(args.data_dir)
    tasks = get_tasks(args.tasks)

    for task in tasks:
        if task == 'MRPC':
            format_mrpc(args.data_dir, args.path_to_mrpc)
        elif task == 'diagnostic':
            download_diagnostic(args.data_dir)
        else:
            download_and_extract(task, args.data_dir) 
Example 2
Project: mutatest   Author: EvanKepner   File: cli.py    MIT License 7 votes vote down vote up
def cli_args(
    args: Sequence[str], ini_config_file: Path = Path("mutatest.ini")
) -> argparse.Namespace:
    """Command line arguments as parsed args.

    If a INI configuration file is set it is used to set additional default arguments, but
    the CLI arguments override any INI file settings.

    Args:
        args: the argument sequence from the command line
        ini_config_file: the ini config file for the default settings

    Returns:
        Parsed args from ArgumentParser
    """
    parser = cli_parser()

    if ini_config_file.exists():
        ini_config = read_ini_config(ini_config_file)
        ini_cli_args = parse_ini_config_with_cli(parser, ini_config, args)
        return parser.parse_args(ini_cli_args)

    return parser.parse_args(args) 
Example 3
Project: incubator-spot   Author: apache   File: worker.py    Apache License 2.0 6 votes vote down vote up
def main():

    # input parameters
    parser = argparse.ArgumentParser(description="Worker Ingest Framework")
    parser.add_argument('-t', '--type', dest='type', required=True,
                        help='Type of data that will be ingested (Pipeline Configuration)',
                        metavar='')
    parser.add_argument('-i', '--id', dest='id', required=True,
                        help='Worker Id, this is needed to sync Kafka and Ingest framework (Partition Number)',
                        metavar='')
    parser.add_argument('-top', '--topic', dest='topic', required=True,
                        help='Topic to read from.', metavar="")
    parser.add_argument('-p', '--processingParallelism', dest='processes',
                        required=False, help='Processing Parallelism', metavar="")
    args = parser.parse_args()

    # start worker based on the type.
    start_worker(args.type, args.topic, args.id, args.processes) 
Example 4
Project: incubator-spot   Author: apache   File: master_collector.py    Apache License 2.0 6 votes vote down vote up
def main():

    # input Parameters
    parser = argparse.ArgumentParser(description="Master Collector Ingest Daemon")
    parser.add_argument('-t', '--type', dest='type', required=True,
                        help='Type of data that will be ingested (Pipeline Configuration)',
                        metavar='')
    parser.add_argument('-w', '--workers', dest='workers_num',
                        required=True, help='Number of workers for the ingest process',
                        metavar='')
    parser.add_argument('-id', '--ingestId', dest='ingest_id',
                        required=False, help='Ingest ID', metavar='')
    args = parser.parse_args()

    # start collector based on data source type.
    start_collector(args.type, args.workers_num, args.ingest_id) 
Example 5
Project: webnull   Author: macrael   File: webnull.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def arg_parser():
    parser = argparse.ArgumentParser(description='A tool for putting websites into a black hole.')
    commands = parser.add_subparsers(title='commands', metavar='COMMAND')
    commands.required = True
    commands.dest = "commands"

    deny = commands.add_parser('deny', description='Add a site to the black hole. It will become unreachable.', help='Add a site to the black hole. It will become unreachable.')
    deny.add_argument('sitename', help='The website to be blackholed. A URL will be stripped down correctly.')
    deny.set_defaults(func=deny_site)

    allow = commands.add_parser('allow', description='Allow access to a blackholed site for a spell.', help='Allow access to a blackholed site for a spell.')
    time_or_tomorrow = allow.add_mutually_exclusive_group()
    time_or_tomorrow.add_argument('-t', '--time', help='sets the duration to enable a site for. Default is five minutes.', default=5, type=int)
    time_or_tomorrow.add_argument('-m', '--morning', help='allow all sites until tomorrow morning at 6am', action='store_true')
    all_or_one = allow.add_mutually_exclusive_group(required=True)
    all_or_one.add_argument('-a', '--all', action='store_true', help='All blackholed hostnames will be granted access instead of a matching sitename.')
    all_or_one.add_argument('sitename', help='All blackholed hostnames that contain this string will be temporarlly granted access.', nargs='?')
    allow.set_defaults(func=allow_site)

    return parser 
Example 6
Project: godot-mono-builds   Author: godotengine   File: cmd_utils.py    MIT License 6 votes vote down vote up
def build_arg_parser(description, env_vars={}):
    from argparse import ArgumentParser, RawDescriptionHelpFormatter
    from textwrap import dedent

    base_env_vars = {
        'MONO_SOURCE_ROOT': 'Overrides default value for --mono-sources',
    }

    env_vars_text = '\n'.join(['    %s: %s' % (var, desc) for var, desc in env_vars.items()])
    base_env_vars_text = '\n'.join(['    %s: %s' % (var, desc) for var, desc in base_env_vars.items()])

    epilog=dedent('''\
environment variables:
%s
%s
''' % (env_vars_text, base_env_vars_text))

    return ArgumentParser(
        description=description,
        formatter_class=RawDescriptionHelpFormatter,
        epilog=epilog
    ) 
Example 7
Project: projection-methods   Author: akshayka   File: save_random_lp.py    GNU General Public License v3.0 6 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    # --- input/output --- #
    parser.add_argument(
        'output', metavar='O', type=str,
        help='output path specifying location in which to problem.')
    # --- problem parameters --- #
    parser.add_argument(
        'm', metavar='rows', type=int, help='number of rows in data matrix')
    parser.add_argument(
        'n', metavar='cols', type=int, help='number of variables')
    parser.add_argument(
        '-d', '--density', type=float, default=.01, help='density of data '
        'matrix A')
    args = parser.parse_args()

    path = check_path(args.output)
    lp = random_linear_program(m=args.m, n=args.n, density=args.density)
    save_problem(path, lp) 
Example 8
Project: pyblish-win   Author: pyblish   File: _uninstall.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _main(argv=None):
    parser = argparse.ArgumentParser(prog="python -m ensurepip._uninstall")
    parser.add_argument(
        "--version",
        action="version",
        version="pip {}".format(ensurepip.version()),
        help="Show the version of pip this will attempt to uninstall.",
    )
    parser.add_argument(
        "-v", "--verbose",
        action="count",
        default=0,
        dest="verbosity",
        help=("Give more output. Option is additive, and can be used up to 3 "
              "times."),
    )

    args = parser.parse_args(argv)

    ensurepip._uninstall_helper(verbosity=args.verbosity) 
Example 9
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test(self):

        def get_my_type(string):
            return 'my_type{%s}' % string

        parser = argparse.ArgumentParser()
        parser.register('type', 'my_type', get_my_type)
        parser.add_argument('-x', type='my_type')
        parser.add_argument('y', type='my_type')

        self.assertEqual(parser.parse_args('1'.split()),
                         NS(x=None, y='my_type{1}'))
        self.assertEqual(parser.parse_args('-x 1 42'.split()),
                         NS(x='my_type{1}', y='my_type{42}'))


# ============
# Action tests
# ============ 
Example 10
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_misc(self):
        parser = argparse.ArgumentParser()
        action = parser.add_argument('--foo', nargs='?', const=42,
                                     default=84, type=int, choices=[1, 2],
                                     help='FOO', metavar='BAR', dest='baz')
        self.assertEqual(action.nargs, '?')
        self.assertEqual(action.const, 42)
        self.assertEqual(action.default, 84)
        self.assertEqual(action.type, int)
        self.assertEqual(action.choices, [1, 2])
        self.assertEqual(action.help, 'FOO')
        self.assertEqual(action.metavar, 'BAR')
        self.assertEqual(action.dest, 'baz')


# ================================
# Argument conflict handling tests
# ================================ 
Example 11
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 6 votes vote down vote up
def monster_lookup():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e monster compendium for the term queried."""))
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)

    try:
        item = Roll20Monster(text)
    except IOError as e:
        print(e)
        item = None
    if item is None or len(item) == 0:
        return False
    else:
        try:
            print(item)
        except UnicodeEncodeError:
            print(item.as_unicode)
        return True 
Example 12
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 6 votes vote down vote up
def item_lookup():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e Item compendium for the term queried."""))
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)
    
    try:
        item = Roll20Item(text)
    except IOError as e:
        print(e)
        item = None
    if item is None or len(item) == 0:
        return False
    else:
        try:
            print(item)
        except UnicodeEncodeError:
            print(item.as_unicode)
        return True 
Example 13
Project: decli   Author: Woile   File: application.py    MIT License 6 votes vote down vote up
def cli(
    data: dict,
    parser_class: Callable = argparse.ArgumentParser,
    parents: Optional[list] = None,
):
    """Create a cli application.

    This is the entrypoint.
    """
    global config
    config = init_config()
    data = deepcopy(data)

    if data.get("prefix_chars"):
        config.update({"prefix_chars": data.get("prefix_chars")})

    parser = add_parser(data, parser_class, parents)
    return parser 
Example 14
Project: lora-sx1276   Author: raspberrypi-tw   File: LoRaArgumentParser.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def parse_args(self, lora):
        """ Parse the args, perform some sanity checks and configure the LoRa accordingly.
        :param lora: Reference to LoRa object
        :return: args
        """
        args = argparse.ArgumentParser.parse_args(self)
        args.bw = self.bw_lookup.get(args.bw, None)
        args.coding_rate = self.cr_lookup.get(args.coding_rate, None)
        # some sanity checks
        assert(args.bw is not None)
        assert(args.coding_rate is not None)
        assert(args.sf >=6 and args.sf <= 12)
        # set the LoRa object
        lora.set_freq(args.freq)
        lora.set_preamble(args.preamble)
        lora.set_spreading_factor(args.sf)
        lora.set_bw(args.bw)
        lora.set_coding_rate(args.coding_rate)
        lora.set_ocp_trim(args.ocp)
        return args 
Example 15
Project: Mastering-Python-Networking-Second-Edition   Author: PacktPublishing   File: eapi_2_acl.py    MIT License 6 votes vote down vote up
def main():
   parser = argparse.ArgumentParser(description="Edit Arista ACLs using your local editor")
   parser.add_argument("acl", metavar="ACL",
                       help="Name of the access list to modify")
   parser.add_argument("switches", metavar="SWITCH", nargs="+",
                       help="Hostname or IP of the switch to query")
   parser.add_argument("--username", help="Name of the user to connect as",
                       default="admin")
   parser.add_argument("--password", help="The user's password")
   parser.add_argument("--https", help="Use HTTPS instead of HTTP",
                       action="store_const", const="https", default="http")
   args = parser.parse_args()

   aclName = args.acl
   tmpfile = "/tmp/AclEditor-%s" % aclName
   apiEndpoints = getEndpoints(args.switches, args.https,
                               args.username, args.password)
   prepopulateAclFile(tmpfile, aclName, apiEndpoints)
   edits = getEdits(tmpfile)
   applyChanges(aclName, apiEndpoints, edits)
   print
   print "Done!" 
Example 16
Project: BASS   Author: Cisco-Talos   File: cmdline.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Bass")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("samples", metavar = "sample", nargs = "+", help = "Sample path") 

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO
        }[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG

    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 17
Project: BASS   Author: Cisco-Talos   File: whitelist.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Add samples to BASS whitelist")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("--url", type = str, default = "http://localhost:5000", help = "URL of BASS server")
    parser.add_argument("sample", help = "Whitelist sample")

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO}[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG
    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 18
Project: BASS   Author: Cisco-Talos   File: client.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Find common ngrams in binary files")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("--output", type = str, default = None, help = "Output to file instead of stdout")
    parser.add_argument("--url", type = str, default = "http://localhost:5000", help = "URL of BASS server")
    parser.add_argument("samples", metavar = "sample", nargs = "+", help = "Cluster samples")

    args = parser.parse_args()

    try:
        loglevel = {
            0: logging.ERROR,
            1: logging.WARN,
            2: logging.INFO}[args.verbose]
    except KeyError:
        loglevel = logging.DEBUG
    logging.basicConfig(level = loglevel)
    logging.getLogger().setLevel(loglevel)

    return args 
Example 19
Project: BASS   Author: Cisco-Talos   File: export_binexport_pickle.py    GNU General Public License v2.0 6 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "IDA Pro script: Dump bindiff database file")
    subparsers = parser.add_subparsers(help = "subcommand")
    
    parser_pickle = subparsers.add_parser("pickle", help = "Dump pickled database")
    parser_pickle.add_argument("pickle_output", type = str, help = "Output pickle database file")
    parser_pickle.set_defaults(handler = handle_pickle)

    parser_bindiff = subparsers.add_parser("binexport", help = "Dump bindiff database")
    parser_bindiff.add_argument("bindiff_output", type = str, help = "Output BinExport database file")
    parser_bindiff.set_defaults(handler = handle_binexport)

    parser_bindiff_pickle = subparsers.add_parser("binexport_pickle", help = "Dump bindiff database and pickled database")
    parser_bindiff_pickle.add_argument("bindiff_output", type = str, help = "Output BinDiff database file")
    parser_bindiff_pickle.add_argument("pickle_output", type = str, help = "Output pickle database file")
    parser_bindiff_pickle.set_defaults(handler = handle_binexport_pickle)

    args = parser.parse_args(idc.ARGV[1:])

    return args 
Example 20
Project: ff-net-worth-calculator   Author: blocktrron   File: cli.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def run():
    parser = argparse.ArgumentParser(description='Calculate net-worth of a Freifunk community')
    parser.add_argument('--url', help='URL to evaluate')
    parser.add_argument('--output-json', help='Stats as JSON', action='store_true')
    parser.add_argument('--exclude-domain', help='Domains to exclude for calculation', action="append")
    args = parser.parse_args()

    if args.url:
        result = load(args.url, args.exclude_domain)
    else:
        parser.print_help()
        sys.exit(1)

    if not result:
        print("No nodes found", file=sys.stderr)
        sys.exit(2)

    model_information = load_devices_json(get_data_path('devices.json'))
    community_information = gather_information(model_information, result)

    print_information(args.url, community_information, not args.output_json) 
Example 21
Project: RelayBot2.0   Author: nukeop   File: relaybot.py    GNU General Public License v3.0 6 votes vote down vote up
def main():
    """RelayBot 2.0 main entry point.
    Creates a new instance of the bot and runs it.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--logfile", help="rotating log filename")
    args = parser.parse_args()

    bot = Bot(args.logfile)

    logger.info("Starting Relay Bot 2.0")
    logger.info("Version: {}.{}".format(VERSION[0], VERSION[1]))

    bot.initialize()

    bot.run() 
Example 22
Project: Pyssql   Author: bicofino   File: pyssql.py    MIT License 6 votes vote down vote up
def __init__(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--username')
        parser.add_argument('--password')
        parser.add_argument('--address')
        parser.add_argument('--port')
        parser.add_argument('--database')

        subparsers = parser.add_subparsers()

        for name in dir(self):
            if not name.startswith("_"):
                p = subparsers.add_parser(name)
                method = getattr(self, name)
                argnames = inspect.getargspec(method).args[1:]
                for argname in argnames:
                    p.add_argument(argname)
                p.set_defaults(func=method, argnames=argnames)
        self.args = parser.parse_args() 
Example 23
Project: iglovikov_segmentation   Author: ternaus   File: inference.py    MIT License 5 votes vote down vote up
def get_args():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg("-c", "--config_path", type=Path, required=True, help="Path to  config")
    arg("-i", "--input_path", type=Path, required=True, help="Path to the target image folder.")
    arg("-j", "--num_workers", type=int, help="Number of CPU threads.", default=12)
    arg("-o", "--output_path", type=Path, help="Path where to save resulting masks.", required=True)
    arg("-w", "--checkpoint", type=Path, help="Path to checkpoint.", required=True)
    arg("-b", "--batch_size", type=int, help="Batch_size.", default=1)
    arg("-t", "--tta", help="Test time augmentation.", default=None, choices=[None, "d4", "lr"])
    arg("-v", "--visualize", help="If add visualized predictions.", action="store_true")
    return parser.parse_args() 
Example 24
Project: iglovikov_segmentation   Author: ternaus   File: train.py    MIT License 5 votes vote down vote up
def get_args():
    parser = argparse.ArgumentParser()
    arg = parser.add_argument
    arg("-c", "--config_path", type=Path, help="Path to the config.", required=True)
    arg("-j", "--num_workers", type=int, help="Number of CPU threads.", default=12)
    arg("-r", "--checkpoint_path", type=Path, default=None, help="Path to checkout.")
    return parser.parse_args() 
Example 25
Project: fs_image   Author: facebookincubator   File: send_fds_and_run.py    MIT License 5 votes vote down vote up
def parse_opts(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--fd', type=int, action='append', default=[],
        help='FDs will be provided to the wrapped process with sequential '
            'numbers starting from 3, in the order they were listed on '
            'the command-line. Repeat to pass multiple FDs.',
    )
    parser.add_argument(
        '--sudo', action='store_true',
        help='Wrap `recv-fds-and-run` with `sudo`, effectively emulating the '
            'behavior of `sudo -C`. See `--sudo-arg` if you need to '
            'pass arguments to `sudo`. Without this option, this CLI is a '
            'very elaborate way of remapping the listed FDs and closing all '
            'others.',
    )
    parser.add_argument(
        '--sudo-arg', action='append', default=[],
        help='Pass this argument to `sudo` on the command-line.'
    )
    parser.add_argument(
        '--no-set-listen-fds', action='store_false', dest='set_listen_fds',
        help='Do not set LISTEN_FDS and LISTEN_PID on the wrapped process. By '
            'default we set these just in case this wraps `systemd-nspawn` -- '
            'that tells it to forward our FDS to the container. If the extra '
            'environment variables are a problem for you, pass this option.',
    )
    parser.add_argument(
        'cmd', nargs='+', help='The command to wrap and supply with FDs.',
    )
    opts = parser.parse_args(argv)
    assert not opts.sudo_arg or opts.sudo, '--sudo-arg requires --sudo'
    return opts 
Example 26
Project: fs_image   Author: facebookincubator   File: recv_fds_and_run.py    MIT License 5 votes vote down vote up
def parse_opts(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--unix-sock', required=True,
        help='Connect to the unix socket at this path to receive FDs.',
    )
    parser.add_argument(
        '--num-fds', type=int, required=True,
        help='The number of FDs to inject, from 3 through (3 + NUM_FDS - 1).',
    )
    parser.add_argument(
        '--no-set-listen-fds', action='store_false', dest='set_listen_fds',
        help='Do not set LISTEN_FDS and LISTEN_PID on the wrapped process. By '
            'default we set these just in case this wraps `systemd-nspawn` -- '
            'that tells it to forward our FDS to the container. If the extra '
            'environment variables are a problem for you, pass this option.',
    )
    parser.add_argument(
        'cmd', nargs='+', help='The command to wrap and supply with FDs.',
    )
    return parser.parse_args(argv)


# This cannot be tested as a library since it `exec`s and rewrites the file
# descriptor table.  However, `test_send_fds_and_run.py` covers this fully. 
Example 27
Project: fs_image   Author: facebookincubator   File: cli.py    MIT License 5 votes vote down vote up
def main(argv, from_file: BytesIO, to_file: BytesIO):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    Storage.add_argparse_arg(
        parser, '--storage', required=True,
        help='JSON blob for creating a Storage instance.',
    )
    parser.add_argument('--debug', action='store_true', help='Log more?')
    subparsers = parser.add_subparsers(help='Sub-commands have help.')

    parser_get = subparsers.add_parser('get', help='Download blob to stdout')
    parser_get.add_argument('storage_id', help='String of the form KEY:ID')
    parser_get.set_defaults(to_file=to_file)
    parser_get.set_defaults(func=get)

    parser_put = subparsers.add_parser(
        'put', help='Write a blob from stdin, print its ID to stdout',
    )
    parser_put.set_defaults(from_file=from_file)
    parser_put.set_defaults(to_file=to_file)  # For the storage ID
    parser_put.set_defaults(func=put)

    args = parser.parse_args(argv)
    init_logging(debug=args.debug)

    args.func(args) 
Example 28
Project: fs_image   Author: facebookincubator   File: subvolume_garbage_collector.py    MIT License 5 votes vote down vote up
def parse_args(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--refcounts-dir', required=True,
        help='We will create a hardlink to `--new-subvolume-json` in this '
            'directory. For that reason, this needs to be on same device, '
            'and thus cannot be under `--subvolumes-dir`',
    )
    parser.add_argument(
        '--subvolumes-dir', required=True,
        help='A directory on a btrfs volume, where all the subvolume wrapper '
            'directories reside.',
    )
    parser.add_argument(
        '--new-subvolume-wrapper-dir',
        help='Subvolumes live inside wrapper directories, following the '
            'convention <name>:<version>/<name>. This parameter should '
            'consist just of the <name>:<version> part.',
    )
    parser.add_argument(
        '--new-subvolume-json',
        help='We will delete any file at this path, then create an empty one, '
            'and hard-link into `--refcounts-dir` for refcounting purposes. '
            'The image compiler will then write data into this file.',
    )
    return parser.parse_args(argv) 
Example 29
Project: leapp-repository   Author: oamg   File: ntp2chrony.py    Apache License 2.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(description="Convert ntp configuration to chrony.")
    parser.add_argument("-r", "--root", dest="roots", default=["/"], nargs="+",
                        metavar="DIR", help="specify root directory (default /)")
    parser.add_argument("--ntp-conf", action="store", default="/etc/ntp.conf",
                        metavar="FILE", help="specify ntp config (default /etc/ntp.conf)")
    parser.add_argument("--step-tickers", action="store", default="",
                        metavar="FILE", help="specify ntpdate step-tickers config (no default)")
    parser.add_argument("--chrony-conf", action="store", default="/etc/chrony.conf",
                        metavar="FILE", help="specify chrony config (default /etc/chrony.conf)")
    parser.add_argument("--chrony-keys", action="store", default="/etc/chrony.keys",
                        metavar="FILE", help="specify chrony keyfile (default /etc/chrony.keys)")
    parser.add_argument("-b", "--backup", action="store_true", help="backup existing configs before writing")
    parser.add_argument("-L", "--ignored-lines", action="store_true", help="print ignored lines")
    parser.add_argument("-D", "--ignored-directives", action="store_true",
                        help="print names of ignored directives")
    parser.add_argument("-n", "--dry-run", action="store_true", help="don't make any changes")
    parser.add_argument("-v", "--verbose", action="count", default=0, help="increase verbosity")

    args = parser.parse_args()

    logging.basicConfig(format="%(message)s",
                        level=[logging.ERROR, logging.INFO, logging.DEBUG][min(args.verbose, 2)])

    for root in args.roots:
        conf = NtpConfiguration(root, args.ntp_conf, args.step_tickers)

        if args.ignored_lines:
            for line in conf.ignored_lines:
                print(line)

        if args.ignored_directives:
            for directive in conf.ignored_directives:
                print(directive)

        conf.write_chrony_configuration(args.chrony_conf, args.chrony_keys, args.dry_run, args.backup) 
Example 30
Project: mutatest   Author: EvanKepner   File: test_cli.py    MIT License 5 votes vote down vote up
def mock_parser():
    """Mock parser."""
    parser = argparse.ArgumentParser(prog="mock_parser", description=("Mock parser"))
    parser.add_argument("-e", "--exclude", action="append", default=[], help="Append")
    parser.add_argument("-b", "--blacklist", nargs="*", default=[], help="Nargs")
    parser.add_argument("--debug", action="store_true", help="Store True.")

    return parser 
Example 31
Project: incubator-spot   Author: apache   File: bluecoat.py    Apache License 2.0 5 votes vote down vote up
def main():
    """
    Handle commandline arguments and
    start the collector.
    """
    # input Parameters
    parser = argparse.ArgumentParser(description="Bluecoat Parser")
    parser.add_argument('-zk', '--zookeeper', dest='zk', required=True,
                        help='Zookeeper IP and port (i.e. 10.0.0.1:2181)', metavar='')
    parser.add_argument('-t', '--topic', dest='topic', required=True,
                        help='Topic to listen for Spark Streaming', metavar='')
    parser.add_argument('-db', '--database', dest='db', required=True,
                        help='Hive database whete the data will be ingested', metavar='')
    parser.add_argument('-dt', '--db-table', dest='db_table', required=True,
                        help='Hive table whete the data will be ingested', metavar='')
    parser.add_argument('-w', '--num_of_workers', dest='num_of_workers', required=True,
                        help='Num of workers for Parallelism in Data Processing', metavar='')
    parser.add_argument('-bs', '--batch-size', dest='batch_size', required=True,
                        help='Batch Size (Milliseconds)', metavar='')
    args = parser.parse_args()

    # start collector based on data source type.
    bluecoat_parse(args.zk, args.topic, args.db, args.db_table, args.num_of_workers, args.batch_size) 
Example 32
Project: incubator-spot   Author: apache   File: collector.py    Apache License 2.0 5 votes vote down vote up
def _parse_args():
    '''
        Parse command-line options found in 'args' (default: sys.argv[1:]).

    :returns: On success, a namedtuple of Values instances.
    '''
    parser   = ArgumentParser('Distributed Collector Daemon of Apache Spot', epilog='END')
    required = parser.add_argument_group('mandatory arguments')

    # .................................state optional arguments
    parser.add_argument('-c', '--config-file',
        default='ingest_conf.json',
        type=file,
        help='path of configuration file',
        metavar='')

    parser.add_argument('-l', '--log-level',
        default='INFO',
        help='determine the level of the logger',
        metavar='')

    parser.add_argument('--skip-conversion',
        action='store_true',
        default=False,
        help='no transformation will be applied to the data; useful for importing CSV files')

    # .................................state mandatory arguments
    required.add_argument('--topic',
        required=True,
        help='name of topic where the messages will be published')

    required.add_argument('-t', '--type',
        choices=pipelines.__all__,
        required=True,
        help='type of data that will be collected')

    return parser.parse_args() 
Example 33
Project: incubator-spot   Author: apache   File: start_oa.py    Apache License 2.0 5 votes vote down vote up
def main():

    # input parameters.
    parser = argparse.ArgumentParser(description="Master OA Script")
    parser.add_argument('-d','--date',required=True,dest='date',help='Date data that will be processed by OA  (i.e 20161102)',metavar='')
    parser.add_argument('-t','--type',required=True,dest='type',help='Data type that will be processed by OA (i.e dns, proxy, flow)',metavar='')
    parser.add_argument('-l','--limit',required=True,dest='limit',help='Num of suspicious connections that will be processed by OA.',metavar='')
    args= parser.parse_args()

    start_oa(args) 
Example 34
Project: cloudflare-ddns   Author: ailionx   File: cloudflare.py    MIT License 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('email')
    parser.add_argument('api_key')
    parser.add_argument('domain')
    parser.add_argument('--proxied', '-p', dest='proxied', action='store_true', default=False,
                        help='Enable the Cloudflare proxy for the record')
    args = parser.parse_args()
    cf = CloudFlare(**vars(args))
    cf.sync_dns_from_my_ip() 
Example 35
Project: meta-transfer-learning   Author: erfaneshrati   File: args.py    MIT License 5 votes vote down vote up
def argument_parser():
    """
    Get an argument parser for a training script.
    """
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--pretrained', help='evaluate a pre-trained model',
                        action='store_true', default=False)
    parser.add_argument('--seed', help='random seed', default=0, type=int)
    parser.add_argument('--checkpoint', help='checkpoint directory', default='model_checkpoint')
    parser.add_argument('--classes', help='number of classes per inner task', default=5, type=int)
    parser.add_argument('--shots', help='number of examples per class', default=5, type=int)
    parser.add_argument('--train-shots', help='shots in a training batch', default=0, type=int)
    parser.add_argument('--inner-batch', help='inner batch size', default=5, type=int)
    parser.add_argument('--inner-iters', help='inner iterations', default=20, type=int)
    parser.add_argument('--replacement', help='sample with replacement', action='store_true')
    parser.add_argument('--learning-rate', help='Adam step size', default=1e-3, type=float)
    parser.add_argument('--meta-step', help='meta-training step size', default=0.1, type=float)
    parser.add_argument('--meta-step-final', help='meta-training step size by the end',
                        default=0.1, type=float)
    parser.add_argument('--meta-batch', help='meta-training batch size', default=1, type=int)
    parser.add_argument('--meta-iters', help='meta-training iterations', default=400000, type=int)
    parser.add_argument('--eval-batch', help='eval inner batch size', default=5, type=int)
    parser.add_argument('--eval-iters', help='eval inner iterations', default=50, type=int)
    parser.add_argument('--eval-samples', help='evaluation samples', default=100, type=int)
    parser.add_argument('--eval-interval', help='train steps per eval', default=1000, type=int)
    parser.add_argument('--weight-decay', help='weight decay rate', default=1, type=float)
    parser.add_argument('--transductive', help='evaluate all samples at once', action='store_true')
    parser.add_argument('--foml', help='use FOML instead of Reptile', action='store_true')
    parser.add_argument('--foml-tail', help='number of shots for the final mini-batch in FOML',
                        default=None, type=int)
    parser.add_argument('--sgd', help='use vanilla SGD instead of Adam', action='store_true')
    parser.add_argument('--metatransfer', help='use meta transfer learning approach', action='store_true')
    parser.add_argument('--gpu', help='gpu id', default=0, type=int)
    return parser 
Example 36
Project: heroku-log-lights   Author: codingjoe   File: __main__.py    MIT License 5 votes vote down vote up
def get_args():
    """Setup argument parser and return parsed arguments."""
    parser = argparse.ArgumentParser(
        description=__doc__.strip()
    )
    parser.add_argument('-a', '--app', dest='app', metavar='HEROKU_APP', type=str,
                        help='Name of the target Heroku app.')
    parser.add_argument('-t', '--token', dest='token', metavar='AUTH_TOKEN', type=str,
                        default=None, help='Heroku AUTH token.')
    parser.add_argument('-v', dest='verbose', action='store_const',
                        const=logging.DEBUG, default=logging.WARNING,
                        help='verbose mode (default: off)')
    return parser.parse_args() 
Example 37
Project: projection-methods   Author: akshayka   File: save_random_cone_program.py    GNU General Public License v3.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    # --- input/output --- #
    parser.add_argument(
        'output', metavar='O', type=str,
        help='output path specifying location in which to problem.')
    # --- problem parameters --- #
    parser.add_argument(
        '-cd', '--cone_dims', required=True, type=int, nargs='+',
        help='list of cone dimensions')
    parser.add_argument(
        '-c', '--cones', required=True, type=str, nargs='+',
        help='list of cone classes belonging to %s' % str(k_cones.keys()))
    parser.add_argument(
        '-n', '--n', required=True, type=int,
        help='number of variables (i.e., dimension of p)')
    parser.add_argument(
        '-d', '--density', type=float, default=.01,
        help='density of data matrix A')
    args = parser.parse_args()

    path = check_path(args.output)
    dim = 2 * (sum(args.cone_dims) + args.n + 1)
    x = cvxpy.Variable(dim)

    for c in args.cones:
        assert c in k_cones
    cones = [k_cones[c] for c in args.cones]
    cone_program = random_cone_program(x=x, cone_dims=args.cone_dims,
        cones=cones, n=args.n, density=args.density)
    save_problem(path, cone_program) 
Example 38
Project: cat-bbs   Author: aleju   File: create_dataset.py    MIT License 5 votes vote down vote up
def main():
    """Generate datasets."""

    parser = argparse.ArgumentParser(description="Process some integers.")
    parser.add_argument("--dataset_dir", help="")
    parser.add_argument("--out_dir", default="", help="")
    parser.add_argument("--img_size", default=224, type=int, help="")
    args = parser.parse_args()

    assert args.dataset_dir is not None, "Expected 10k cats dataset directory via --dataset_dir"

    # load images and their facial keypoints
    print("Loading filepaths...")
    fps = find_image_filepaths(args.dataset_dir)

    print("Loading images...")
    examples = []
    for i, fp in enumerate(fps):
        if (i+1) % 100 == 0:
            print("Image %d of %d..." % (i+1, len(fps)))
        img = ndimage.imread(fp, mode="RGB")
        kps = load_keypoints(fp, image_height=img.shape[0], image_width=img.shape[1])
        img_square, (added_top, added_right, added_bottom, added_left) = to_aspect_ratio_add(img, 1.0, return_paddings=True)
        kps = [(x+added_left, y+added_top) for (x, y) in kps]
        #rs_factor = args.img_size / img_square.shape[0]
        img_square_rs = misc.imresize(img_square, (args.img_size, args.img_size))
        kps_rs = [(int((x/img_square.shape[1])*args.img_size), int((y/img_square.shape[0])*args.img_size)) for (x, y) in kps]
        examples.append(Example(fp, img_square_rs, kps_rs))

    # save datasets
    print("Saving pickle file...")
    with open(os.path.join(args.out_dir, "cats-dataset.pkl"), "w") as f:
        pickle.dump(examples, f) 
Example 39
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test(self):

        parser = argparse.ArgumentParser()
        parser.register('action', 'my_action', self.MyAction)
        parser.add_argument('badger', action='my_action')

        self.assertEqual(parser.parse_args(['1']), NS(badger='foo[1]'))
        self.assertEqual(parser.parse_args(['42']), NS(badger='foo[42]'))


# ================
# Subparsers tests
# ================ 
Example 40
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflicting_parents(self):
        self.assertRaises(
            argparse.ArgumentError,
            argparse.ArgumentParser,
            parents=[self.w_parent, self.wxyz_parent]) 
Example 41
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflicting_parents_mutex(self):
        self.assertRaises(
            argparse.ArgumentError,
            argparse.ArgumentParser,
            parents=[self.abcd_parent, self.ab_mutex_parent]) 
Example 42
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertTypeError(self, *args, **kwargs):
        parser = argparse.ArgumentParser()
        self.assertRaises(TypeError, parser.add_argument,
                          *args, **kwargs) 
Example 43
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def assertValueError(self, *args, **kwargs):
        parser = argparse.ArgumentParser()
        self.assertRaises(ValueError, parser.add_argument,
                          *args, **kwargs) 
Example 44
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_invalid_action(self):
        self.assertValueError('-x', action='foo')
        self.assertValueError('foo', action='baz')
        self.assertValueError('--foo', action=('store', 'append'))
        parser = argparse.ArgumentParser()
        try:
            parser.add_argument("--foo", action="store-true")
        except ValueError:
            e = sys.exc_info()[1]
            expected = 'unknown action'
            msg = 'expected %r, found %r' % (expected, e)
            self.assertTrue(expected in str(e), msg) 
Example 45
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_multiple_dest(self):
        parser = argparse.ArgumentParser()
        parser.add_argument(dest='foo')
        try:
            parser.add_argument('bar', dest='baz')
        except ValueError:
            e = sys.exc_info()[1]
            expected = 'dest supplied twice for positional argument'
            msg = 'expected %r, found %r' % (expected, e)
            self.assertTrue(expected in str(e), msg) 
Example 46
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_parsers_action_missing_params(self):
        self.assertTypeError('command', action='parsers')
        self.assertTypeError('command', action='parsers', prog='PROG')
        self.assertTypeError('command', action='parsers',
                             parser_class=argparse.ArgumentParser) 
Example 47
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_dest(self):
        parser = argparse.ArgumentParser()
        action = parser.add_argument('--foo')
        self.assertEqual(action.dest, 'foo')
        action = parser.add_argument('-b', '--bar')
        self.assertEqual(action.dest, 'bar')
        action = parser.add_argument('-x', '-y')
        self.assertEqual(action.dest, 'x') 
Example 48
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_bad_type(self):
        self.assertRaises(ValueError, argparse.ArgumentParser,
                          conflict_handler='foo') 
Example 49
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_conflict_error(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-x')
        self.assertRaises(argparse.ArgumentError,
                          parser.add_argument, '-x')
        parser.add_argument('--spam')
        self.assertRaises(argparse.ArgumentError,
                          parser.add_argument, '--spam') 
Example 50
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_resolve_error(self):
        get_parser = argparse.ArgumentParser
        parser = get_parser(prog='PROG', conflict_handler='resolve')

        parser.add_argument('-x', help='OLD X')
        parser.add_argument('-x', help='NEW X')
        self.assertEqual(parser.format_help(), textwrap.dedent('''\
            usage: PROG [-h] [-x X]

            optional arguments:
              -h, --help  show this help message and exit
              -x X        NEW X
            '''))

        parser.add_argument('--spam', metavar='OLD_SPAM')
        parser.add_argument('--spam', metavar='NEW_SPAM')
        self.assertEqual(parser.format_help(), textwrap.dedent('''\
            usage: PROG [-h] [-x X] [--spam NEW_SPAM]

            optional arguments:
              -h, --help       show this help message and exit
              -x X             NEW X
              --spam NEW_SPAM
            '''))


# =============================
# Help and Version option tests
# ============================= 
Example 51
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_type_function_call_only_once(self):
        def spam(string_to_convert):
            self.assertEqual(string_to_convert, 'spam!')
            return 'foo_converted'

        parser = argparse.ArgumentParser()
        parser.add_argument('--foo', type=spam, default='bar')
        args = parser.parse_args('--foo spam!'.split())
        self.assertEqual(NS(foo='foo_converted'), args)

# ==================================================================
# Check semantics regarding the default argument and type conversion
# ================================================================== 
Example 52
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_type_function_call_with_non_string_default(self):
        def spam(int_to_convert):
            self.assertEqual(int_to_convert, 0)
            return 'foo_converted'

        parser = argparse.ArgumentParser()
        parser.add_argument('--foo', type=spam, default=0)
        args = parser.parse_args([])
        # foo should *not* be converted because its default is not a string.
        self.assertEqual(NS(foo=0), args) 
Example 53
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_type_function_call_with_string_default(self):
        def spam(int_to_convert):
            return 'foo_converted'

        parser = argparse.ArgumentParser()
        parser.add_argument('--foo', type=spam, default='0')
        args = parser.parse_args([])
        # foo is converted because its default is a string.
        self.assertEqual(NS(foo='foo_converted'), args) 
Example 54
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_no_double_type_conversion_of_default(self):
        def extend(str_to_convert):
            return str_to_convert + '*'

        parser = argparse.ArgumentParser()
        parser.add_argument('--test', type=extend, default='*')
        args = parser.parse_args([])
        # The test argument will be two stars, one coming from the default
        # value and one coming from the type conversion being called exactly
        # once.
        self.assertEqual(NS(test='**'), args) 
Example 55
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_issue_15906(self):
        # Issue #15906: When action='append', type=str, default=[] are
        # providing, the dest value was the string representation "[]" when it
        # should have been an empty list.
        parser = argparse.ArgumentParser()
        parser.add_argument('--test', dest='test', type=str,
                            default=[], action='append')
        args = parser.parse_args([])
        self.assertEqual(args.test, [])

# ======================
# parse_known_args tests
# ====================== 
Example 56
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_arguments_list(self):
        parser = argparse.ArgumentParser()
        parser.parse_args([]) 
Example 57
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_arguments_tuple_positional(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('x')
        parser.parse_args(('x',)) 
Example 58
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_arguments_list_positional(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('x')
        parser.parse_args(['x']) 
Example 59
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_optionals(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--foo')
        args, extras = parser.parse_known_args('--foo F --bar --baz'.split())
        self.assertEqual(NS(foo='F'), args)
        self.assertEqual(['--bar', '--baz'], extras) 
Example 60
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_mixed(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('-v', nargs='?', const=1, type=int)
        parser.add_argument('--spam', action='store_false')
        parser.add_argument('badger')

        argv = ["B", "C", "--foo", "-v", "3", "4"]
        args, extras = parser.parse_known_args(argv)
        self.assertEqual(NS(v=3, spam=True, badger="B"), args)
        self.assertEqual(["C", "--foo", "4"], extras)

# ==========================
# add_argument metavar tests
# ========================== 
Example 61
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def do_test_exception(self, nargs, metavar):
        parser = argparse.ArgumentParser()
        with self.assertRaises(ValueError) as cm:
            parser.add_argument("--foo", nargs=nargs, metavar=metavar)
        self.assertEqual(cm.exception.args[0], self.EXPECTED_MESSAGE)

    # Unit tests for different values of metavar when nargs=None 
Example 62
Project: pyblish-win   Author: pyblish   File: test_argparse.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_main():
    # silence warnings about version argument - these are expected
    with test_support.check_warnings(
            ('The "version" argument to ArgumentParser is deprecated.',
             DeprecationWarning),
            ('The (format|print)_version method is deprecated',
             DeprecationWarning)):
        test_support.run_unittest(__name__)
    # Remove global references to avoid looking like we have refleaks.
    RFile.seen = {}
    WFile.seen = set() 
Example 63
Project: teamscale-cli   Author: cqse   File: precommit_client.py    Apache License 2.0 5 votes vote down vote up
def _parse_args():
    """Parses the precommit client command line arguments."""
    parser = argparse.ArgumentParser(description='Precommit analysis client for Teamscale.')
    parser.add_argument('path', metavar='path', type=str, nargs=1,
                        help='path to any file in the repository')
    parser.add_argument('--exclude-findings-in-changed-code', dest='exclude_findings_in_changed_code',
                        action='store_const', const=True, default=False,
                        help='Determines whether to exclude findings in changed code (default: False)')
    parser.add_argument('--fetch-existing-findings', dest='fetch_existing_findings', action='store_const',
                        const=True, default=False,
                        help='When this option is set, existing findings in the specified file are fetched in addition '
                             'to precommit findings. (default: False)')
    parser.add_argument('--fetch-existing-findings-in-changes', dest='fetch_existing_findings_in_changes',
                        action='store_const', const=True, default=False,
                        help='When this option is set, existing findings in all changed files are fetched in addition '
                             'to precommit findings. (default: False)')
    parser.add_argument('--fetch-all-findings', dest='fetch_all_findings', action='store_const',
                        const=True, default=False,
                        help='When this option is set, all existing findings in the repo are fetched in addition '
                             'to precommit findings. (default: False)')
    parser.add_argument('--fail-on-red-findings', dest='fail_on_red_findings', action='store_const',
                        const=True, default=False,
                        help='When this option is set, the precommit client will exit with a non-zero return value '
                             'whenever RED findings were among the precommit findings. (default: False)')
    parser.add_argument('--omit-links-to-findings', dest='omit_links_to_findings', action='store_const',
                        const=True, default=False,
                        help='By default, each finding includes a link to the corresponding finding in Teamscale. '
                             'Setting this option omits these links. (default: False)')
    parser.add_argument('--verify', default=True, type=_bool_or_string,
                        help='Path to different certificate file. See requests\' verify parameter in '
                             'http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification.\n'
                             'Other possible values: True, False (default: True)')
    return parser.parse_args() 
Example 64
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 5 votes vote down vote up
def spell_lookup():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e Spell compendium for the term queried."""))
    parser.add_argument('--ds',
                        help='Parse as dungeonsheets class', action='store_true')
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)
    
    try:
        item = DnDSpell(text)
#         item = Roll20Spell(text)
    except IOError as e:
        print(e)
        item = None
    if item is None or len(item) == 0:
        return False
    else:
        try:
            if args.ds:
                print(item.as_dungeonsheets_class())
            else:
                print(item)
        except UnicodeEncodeError:
            print(item.as_unicode)
        return True 
Example 65
Project: DnD4py   Author: bacook17   File: lookup_5e.py    MIT License 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """Searches Roll20.net 5e compendium for the term queried."""))

    parser.add_argument('--monster',
                        help='Search only monster lists', action='store_true')
    parser.add_argument('--spell',
                        help='Search only spell lists', action='store_true')
    parser.add_argument('--item',
                        help='Search only item lists', action='store_true')
    parser.add_argument('query', nargs='+', help='The words to search')

    args = parser.parse_args()
    text = ' '.join(args.query)

    item = None
    if args.monster:
        item = Roll20Monster(text)
    elif args.spell:
        item = Roll20Spell(text)
    elif args.item:
        item = Roll20Item(text)
    else:
        for r in [Roll20Monster, Roll20Spell,
                  Roll20Item, Roll20]:
            try:
                item = r(text)
            except IOError as e:
                continue
            else:
                break
    if item is None or len(item) == 0:
        print('Not Found')
    else:
        try:
            print(item)
        except UnicodeEncodeError:
            print(item.as_unicode) 
Example 66
Project: lora-sx1276   Author: raspberrypi-tw   File: LoRaArgumentParser.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, description):
        argparse.ArgumentParser.__init__(self, description=description)
        self.add_argument('--ocp', '-c', dest='ocp', default=100, action="store", type=float,
                          help="Over current protection in mA (45 .. 240 mA)")
        self.add_argument('--sf', '-s', dest='sf', default=7, action="store", type=int,
                          help="Spreading factor (6...12). Default is 7.")
        self.add_argument('--freq', '-f', dest='freq', default=869., action="store", type=float,
                          help="Frequency")
        self.add_argument('--bw', '-b', dest='bw', default='BW125', action="store", type=str,
                          help="Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25 BW41_7 BW62_5 BW125 BW250 BW500).\nDefault is BW125.")
        self.add_argument('--cr', '-r', dest='coding_rate', default='CR4_5', action="store", type=str,
                          help="Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8).\nDefault is CR4_5.")
        self.add_argument('--preamble', '-p', dest='preamble', default=8, action="store", type=int,
                          help="Preamble length. Default is 8.") 
Example 67
Project: BASS   Author: Cisco-Talos   File: server.py    GNU General Public License v2.0 5 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description = "Function database server")
    parser.add_argument("-v", "--verbose", action = "count", default = 0, help = "Increase verbosity")
    parser.add_argument("--db", type = str, default = os.environ.get("DATABASE", None), help = "Database URL to connect to")
    args = parser.parse_args()

    return args 
Example 68
Project: RF-Monitor   Author: EarToEarOak   File: rf_monitor.py    GNU General Public License v2.0 5 votes vote down vote up
def __arguments():
    parser = argparse.ArgumentParser(prog="rfmonitor.py",
                                     description='RF signal monitor')

    parser.add_argument('-c', '--cli',
                        help='Command line mode', action='store_true')
    groupCli = parser.add_argument_group('Command line mode')

    groupCli.add_argument('-p', '--port',
                          help='GPS serial port')
    groupCli.add_argument('-b', '--baud', type=int,
                          help='GPS serial baud rate')
    groupCli.add_argument('-j', '--json', action='store_true',
                          help='Output JSON updates (suppresses other output)')
    groupCli.add_argument('-w', '--web', type=str,
                          help='Web server push address')

    parser.add_argument('file', nargs='?',
                        help='Load file')

    args = parser.parse_args()

    if args.cli and args.file is None:
        sys.stderr.write('Filename required in command line mode')
        exit(1)

    if args.file is not None and not os.path.exists(args.file):
        sys.stderr.write('File not found')
        exit(1)

    return args 
Example 69
Project: fs_image   Author: facebookincubator   File: demo_sendstreams.py    MIT License 4 votes vote down vote up
def _main():
    p = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )

    class Print(enum.Enum):
        NONE = 'none'
        PRETTY = 'pretty'
        PICKLE = 'pickle'

        def __str__(self):
            return self.value

    p.add_argument(
        '--print', type=Print, choices=Print, default=Print.NONE,
        help='If set, prints the result in the specified format to stdout.',
    )
    p.add_argument(
        '--write-gold-to-dir',
        help='If set, writes the gold test data into the given directory as '
            'gold_demo_sendstreams.{pickle,pretty}. Warning: you will need '
            'to manually update some constants like `uuid_create` in the '
            '"expected" section of the test code.',
    )
    args = p.parse_args()

    # __file__ won't let us find the repo in @mode/opt, but that's OK, since
    # this is only used as a binary for development purposes.
    sendstream_dict = make_demo_sendstreams(__file__)

    # This width makes the `--dump`ed commands fit on one line.
    prettified = pprint.pformat(sendstream_dict, width=200).encode()
    pickled = pickle.dumps(sendstream_dict)

    if args.print == Print.PRETTY:
        sys.stdout.buffer.write(prettified)
    elif args.print == Print.PICKLE:
        sys.stdout.buffer.write(pickled)
    else:
        assert args.print == Print.NONE, args.print

    if args.write_gold_to_dir is not None:
        for filename, data in [
            ('gold_demo_sendstreams.pickle', pickled),
            ('gold_demo_sendstreams.pretty', prettified),  # For humans
        ]:
            path = os.path.join(args.write_gold_to_dir, filename)
            # We want these files to be created by a non-root user
            assert os.path.exists(path), path
            with open(path, 'wb') as f:
                f.write(data) 
Example 70
Project: fs_image   Author: facebookincubator   File: update_package_db.py    MIT License 4 votes vote down vote up
def _parse_args(argv, *, overview_doc, options_doc):
    parser = argparse.ArgumentParser(
        description=__doc__ + overview_doc,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        '--db', type=Path.from_argparse, required=True,
        help='Path to the database to update',
    )
    parser.add_argument(
        '--out-db', type=Path.from_argparse,
        help='Path for the updated database (defaults to `--db`)',
    )
    parser.add_argument(
        '--out-format',
        choices=set(_FORMAT_NAME_TO_WRITER.keys()),
        help='Which database format to write? (defaults to the auto-detected '
            'input format)',
    )
    parser.add_argument(
        '--no-update-existing', action='store_false', dest='update_existing',
        help='Only update package / tag pairs set via --create or --replace',
    )
    for action, doc in [
        (
            'create',
            'Ensures this PACKAGE/TAG pair will be updated in the DB, '
            f'even if `--no-update-existing` was passed. {options_doc} '
            'Asserts that the PACKAGE/TAG pair was NOT already in the DB. ',
        ),
        (
            'replace',
            'Just like `--create`, but asserts that the PACKAGE/TAG pair '
            'already exists in the DB.',
        ),
    ]:
        parser.add_argument(
            '--' + action, action='append', default=[], nargs=3,
            metavar=('PACKAGE', 'TAG', 'OPTIONS'), help=doc,
        )
    parser.add_argument(  # Pass this to `init_logging`
        '--debug', action='store_true', help='Enable verbose logging',
    )
    return parser.parse_args(argv) 
Example 71
Project: fs_image   Author: facebookincubator   File: snapshot_repo.py    MIT License 4 votes vote down vote up
def snapshot_repo(argv):
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
    )
    add_standard_args(parser)
    parser.add_argument(
        '--repo-name', required=True,
        help="Used to distinguish this repo's metadata from others' in the DB.",
    )
    parser.add_argument(
        '--repo-url', required=True,
        help='The base URL of the repo -- the part before repodata/repomd.xml. '
            'Supported protocols include file://, https://, and http://.',
    )
    parser.add_argument(
        '--gpg-url', required=True, action='append',
        help='(May be repeated) Yum will need to import this key to gpgcheck '
            'the repo. To avoid placing blind trust in these keys (e.g. in '
            'case this is an HTTP URL), they are verified against '
            '`--gpg-key-whitelist-dir`',
    )
    args = parser.parse_args(argv)

    init_logging(debug=args.debug)

    with populate_temp_dir_and_rename(
        args.snapshot_dir, overwrite=True,
    ) as td, RepoSnapshot.add_sqlite_to_storage(args.storage, td) as db:
        sizer = RepoSizer()
        # This is outside the retry_fn not to mask transient verification
        # failures.  I don't expect many infra failures.
        snapshot_gpg_keys(
            key_urls=args.gpg_url,
            whitelist_dir=args.gpg_key_whitelist_dir,
            snapshot_dir=td,
        )
        retry_fn(
            lambda: RepoDownloader(
                args.repo_name,
                args.repo_url,
                RepoDBContext(args.db, args.db.SQL_DIALECT),
                args.storage,
            ).download(rpm_shard=args.rpm_shard),
            delays=[0] * args.retries,
            what=f'Downloading {args.repo_name} from {args.repo_url} failed',
        ).visit(sizer).to_sqlite(args.repo_name, db)
        log.info(sizer.get_report(f'This {args.rpm_shard} snapshot weighs')) 
Example 72
Project: fs_image   Author: facebookincubator   File: compiler.py    MIT License 4 votes vote down vote up
def parse_args(args) -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument(
        '--subvolumes-dir', required=True,
        help='A directory on a btrfs volume to store the compiled subvolume '
            'representing the new layer',
    )
    # We separate this from `--subvolumes-dir` in order to help keep our
    # JSON output ignorant of the absolute path of the repo.
    parser.add_argument(
        '--subvolume-rel-path', required=True,
        help='Path underneath --subvolumes-dir where we should create '
            'the subvolume. Note that all path components but the basename '
            'should already exist.',
    )
    parser.add_argument(
        '--build-appliance-json',
        help='Path to the JSON output of target referred by build_appliance',
    )
    parser.add_argument(
        '--preserve-yum-dnf-cache', action='store_true',
        help='RpmActionItem will write to `/var/cache/{dnf,yum}` on the '
            'subvolume when installing RPMs. The caches will contain repodata '
            'from the current repo snapshot, so this is most useful for '
            'constructing build-appliance images.',
    )
    parser.add_argument(
        '--artifacts-may-require-repo', action='store_true',
        help='Buck @mode/dev produces "in-place" build artifacts that are '
            'not truly standalone. It is important to be able to execute '
            'code from images built in this mode to support rapid '
            'development and debugging, even though it is not a "true" '
            'self-contained image. To allow execution of in-place binaries, '
            'fs_image runtimes will automatically mount the repo into any '
            '`--artifacts-may-require-repo` image at runtime (e.g. when '
            'running image unit-tests, when using `-container` or `-boot` '
            'targets, when using the image as a build appliance).',
    )
    parser.add_argument(
        '--child-layer-target', required=True,
        help='The name of the Buck target describing the layer being built',
    )
    parser.add_argument(
        '--child-feature-json', action='append', default=[],
        help='The path of the JSON output of any `image.feature`s that are'
            'directly included by the layer being built',
    )
    parser.add_argument(
        '--child-dependencies',
        nargs=argparse.REMAINDER, metavar=['TARGET', 'PATH'], default=(),
        help='Consumes the remaining arguments on the command-line, with '
            'arguments at positions 1, 3, 5, 7, ... used as Buck target names '
            '(to be matched with the targets in per-feature JSON outputs). '
            'The argument immediately following each target name must be a '
            'path to the output of that target on disk.',
    )
    return parser.parse_args(args) 
Example 73
Project: odorik   Author: nijel   File: main.py    GNU General Public License v3.0 4 votes vote down vote up
def get_parser():
    """Create argument parser."""
    parser = ArgumentParser(
        description='Odorik <{0}> command line utility.'.format(odorik.URL),
        epilog='This utility is developed at <{0}>.'.format(odorik.DEVEL_URL),
    )
    parser.add_argument(
        '--format',
        default='text',
        choices=('text', 'csv', 'json', 'html'),
        help='Output format to use'
    )
    parser.add_argument(
        '--version',
        action='version',
        version='odorik {0}'.format(odorik.__version__)
    )
    parser.add_argument(
        '--config',
        help='Path to configuration file',
    )
    parser.add_argument(
        '--config-section',
        default='odorik',
        help='Configuration section to use'
    )
    parser.add_argument(
        '--user',
        help='API username',
    )
    parser.add_argument(
        '--password',
        help='API password',
    )
    parser.add_argument(
        '--url',
        help='API URL',
    )
    subparser = parser.add_subparsers(dest="cmd")

    for command in COMMANDS:
        COMMANDS[command].add_parser(subparser)

    return parser 
Example 74
Project: SyNEThesia   Author: RunOrVeith   File: run_synethesia.py    MIT License 4 votes vote down vote up
def parse_args():

    parser = argparse.ArgumentParser(description="""SyNEThesia is a deep neural network that
                                                 visualizes music (and other sound).""")

    subparsers = parser.add_subparsers(dest="mode", help="""Choose a mode of operation.""")
    subparsers.required = True

    train_parser = subparsers.add_parser("train", help="Train the model.")
    store_parser = subparsers.add_parser("infer", help="Infer songs or other sounds and store the frames in a folder.")
    stream_parser = subparsers.add_parser("stream", help="""Infer input from your standard microphone live.
                                                            Opens a window in full screen mode.
                                                            Close it by pressing 'q', or minimize it with 'Esc'.""")
    info_parser = subparsers.add_parser("info", help="Display additional information, such as available models.")

    for _parser in [train_parser, store_parser, stream_parser]:
        _parser.add_argument("model_name", type=str,
                            help="""Name of the model to be trained or used for inference.
                                    If it exists in the local checkpoints folder, the model will be loaded,
                                    otherwise it will be newly created.""")

        _parser.add_argument("-b", "--batch-size", default=32, type=int, dest="batch_size",
                             help="Batch size. Default is %(default)s. Ignored for streaming inference.")

        _parser.add_argument("-r", "--n-rows", default=256, type=int, dest="rows",
                             help="""Image rows (height). Should be a power of two.
                                     An error will be thrown if the loaded model was not trained on the same size.
                                     Defaults to %(default)s.""")
        _parser.add_argument("-c", "--n-cols", default=128, type=int, dest="cols",
                             help="""Image columns (width). Should be a power of two.
                                     An error will be thrown if the loaded model was not trained on the same size.
                                     Defaults to %(default)s.""")

    train_parser.add_argument("-l", "--learning-rate", default=0.0001, type=float, dest="learning_rate",
                              help="""Learning rate for training. Will be
                                      exponentially decayed over time.
                                      Defaults to %(default)s.""")



    train_parser.add_argument("data", type=str,
                              help="""Either a file containing paths to .mp3's, or a folder containing .mp3's,
                                      or a single .mp3""")
    store_parser.add_argument("data", type=str,
                              help="""Either a file containing paths to .mp3's, or a folder containing .mp3's,
                                      or a single .mp3""")
    store_parser.add_argument("target_dir", type=str,
                              help="""Target directory for storing the resulting video.
                                      Warning: There may be many frames stored here intermediately,
                                      so make sure you have enough disc space.""")
    stream_parser.add_argument("-d", "--device-index", dest="device_index", type=int, default=None,
                               help="""Device index to be used as audio input.
                                       Default is the system's standard audio input.""")

    arguments = parser.parse_args()
    return arguments 
Example 75
Project: mutatest   Author: EvanKepner   File: cli.py    MIT License 4 votes vote down vote up
def get_parser_actions(parser: argparse.ArgumentParser) -> ParserActionMap:
    """Create a parser action map used when creating the command list mixed from the
    CLI and the ini config file.

    ParserActionMap has both actions and types e.g.,

    .. code-block:: python

        # action-types:

        {argparse._HelpAction: ['help'],
         mutatest.cli.ValidCategoryAction: ['blacklist', 'whitelist'],
         argparse._AppendAction: ['exclude'],
         argparse._StoreAction: ['mode', 'output', 'src', 'testcmds'],
         mutatest.cli.PositiveIntegerAction: ['nlocations', 'rseed', 'exception'],
         argparse._StoreTrueAction: ['debug', 'nocov']}

        # actions:

        {'-h': '--help',
         '-b': '--blacklist',
         '-e': '--exclude',
         '-m': '--mode',
         '-n': '--nlocations',
         '-o': '--output',
         '-r': '--rseed',
         '-s': '--src',
         '-t': '--testcmds',
         '-w': '--whitelist',
         '-x': '--exception',
         '--debug': '--debug',
         '--nocov': '--nocov'}

    Args:
        parser: the argparser

    Returns:
        ParserActionMap: includes actions and action_types
    """
    actions: Dict[str, str] = {}
    action_types: Dict[Any, List[str]] = {}

    for action in parser._actions:
        # build the actions
        # option_strings is either [-r, --rseed] or [--debug] for short-hand options
        actions[action.option_strings[0]] = action.option_strings[-1]

        # build the action_types
        # values align to the keywords that can be used in the INI config
        try:
            action_types[type(action)].append(action.option_strings[-1].strip("--"))

        except KeyError:
            action_types[type(action)] = [action.option_strings[-1].strip("--")]

    return ParserActionMap(actions=actions, action_types=action_types) 
Example 76
Project: mutatest   Author: EvanKepner   File: cli.py    MIT License 4 votes vote down vote up
def parse_ini_config_with_cli(
    parser: argparse.ArgumentParser, ini_config: configparser.SectionProxy, cli_args: Sequence[str]
) -> List[str]:
    """Combine the INI file settings with the CLI args, using the CLI args as the override.

    Args:
        parser: the argparser
        ini_config: the section of the parsed INI file
        cli_args: the original cli args

    Returns:
        Updated args mixing INI and CLI, with CLI used as the override
    """

    action_maps = get_parser_actions(parser)
    final_args_list = [action_maps.actions.get(i, i) for i in cli_args]

    def ws_proc(value: str) -> List[str]:
        """Convenience function for stripping newlines from configparser section values
        and splitting whitespace to a list.
        """
        return value.replace("\n", " ").split()

    for k in ini_config.keys():
        arg_key = f"--{k}"

        if arg_key in action_maps.actions.values():
            if arg_key not in final_args_list:

                if k in action_maps.action_types[mutatest.cli.ValidCategoryAction]:
                    values = ws_proc(ini_config[k])
                    final_args_list.extend([arg_key] + values)

                elif k in action_maps.action_types[argparse._StoreTrueAction]:
                    if ini_config.getboolean(k):
                        final_args_list.append(arg_key)

                elif k in action_maps.action_types[argparse._AppendAction]:
                    values = ws_proc(ini_config[k])
                    final_args_list.extend(
                        [i for j in list(itertools.product([arg_key], values)) for i in j]
                    )

                else:
                    final_args_list.extend([arg_key, ini_config[k]])

    return final_args_list


####################################################################################################
# CLI REPORTING OUTPUTS:w
#################################################################################################### 
Example 77
Project: incubator-spot   Author: apache   File: start_listener.py    Apache License 2.0 4 votes vote down vote up
def parse_args():
    '''
        Parse command-line options found in 'args' (default: sys.argv[1:]).

    :returns: On success, a namedtuple of Values instances.
    '''
    parser   = ArgumentParser('Start Spark Job for Streaming Listener Daemon', epilog='END')
    required = parser.add_argument_group('mandatory arguments')

    # .................................state optional arguments
    parser.add_argument('-c', '--config-file',
        default='ingest_conf.json',
        type=file,
        help='path of configuration file',
        metavar='')

    parser.add_argument('-d', '--deploy-mode',
        default='client',
        help='Whether to launch the driver program locally ("client") or on one of the '
            'worker machines inside the cluster ("cluster")',
        metavar='')

    parser.add_argument('-g', '--group-id',
        help='name of the consumer group to join for dynamic partition assignment',
        metavar='')

    parser.add_argument('-l', '--log-level',
        default='INFO',
        help='determine the level of the logger',
        metavar='')

    parser.add_argument('-m', '--master',
        default='yarn',
        help='spark://host:port, mesos://host:port, yarn, or local',
        metavar='')

    parser.add_argument('-n', '--app-name',
        help='name of the Spark Job to display on the cluster web UI',
        metavar='')

    parser.add_argument('-r', '--redirect-spark-logs',
        help='redirect output of spark to specific file',
        metavar='')

    # .................................state mandatory arguments
    required.add_argument('-p', '--partitions',
        required=True,
        help='number of partitions to consume; each partition is consumed in its own thread')

    required.add_argument('-t', '--type',
        choices=pipelines.__all__,
        required=True,
        help='type of the data that will be ingested')

    required.add_argument('--topic',
        required=True,
        help='topic to listen for new messages')

    return parser.parse_args() 
Example 78
Project: kicker-module   Author: EvanTheB   File: front.py    GNU General Public License v3.0 4 votes vote down vote up
def ladder_command(self, command):
        class LadderArgError(Exception):
            pass

        class LadderArgumentParser(argparse.ArgumentParser):

            def error(self, message):
                raise LadderArgError(self.format_usage())

        parser = LadderArgumentParser(prog=".wrank", add_help=False)
        subcommands = parser.add_subparsers()
        parser.add_argument("-h", action="store_true")

        ladder = subcommands.add_parser('ladder')
        ladder.add_argument('type',
                            type=str,
                            choices=['ELO', 'basic', 'scaled', 'trueskill', "trueskill_ms"],
                            default='trueskill',
                            nargs='?',
                            )
        ladder.add_argument('-v', '--verbose', action='count')
        ladder.add_argument('-b', '--history')
        ladder.add_argument('-f', '--filter', nargs='*')
        ladder.add_argument('options', nargs='*')
        ladder.set_defaults(func=self._show_ladder)

        add = subcommands.add_parser('add')
        add.add_argument('name', type=str)
        add.set_defaults(func=self._add_player)

        game = subcommands.add_parser('game')
        game.add_argument('words', type=str, nargs='*')
        game.set_defaults(func=self._add_game)

        history = subcommands.add_parser('history')
        history.add_argument('players', type=str, nargs='*')
        history.set_defaults(func=self._show_history)

        next_best = subcommands.add_parser('next')
        next_best.add_argument('players', type=str, nargs='*')
        next_best.add_argument('--heuristic', '-z',
                               type=str,
                               default='fast',
                               choices=HeuristicManager().command)
        next_best.set_defaults(func=self._best_matches)

        whowins = subcommands.add_parser('whowins')
        whowins.add_argument('team_a', type=str, nargs=2)
        whowins.add_argument('team_b', type=str, nargs=2)
        whowins.set_defaults(func=self._expected_outcome)

        try:
            args = parser.parse_args(command)
            return args.func(args)
        except LadderArgError as e:
            return str(e).split('\n') 
Example 79
Project: projection-methods   Author: akshayka   File: plot_residuals.py    GNU General Public License v3.0 4 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    # --- input/output --- #
    parser.add_argument(
        'data', metavar='D',
        help=('glob matching pickled results to plot; results should be '
        'generated by experiment.py'))
    parser.add_argument(
        '-o', '--output', type=str, default=None,
        help=('output filename of plot (w/o extension); if None, plot is '
        'shown but not saved.'))
    # --- plot settings --- #
    parser.add_argument(
        '-t', '--title', type=str, default='Residuals for feasibility problem',
        help='plot title')
    args = vars(parser.parse_args())

    if args['output'] is not None:
        output_path = PosixPath(args['output'] + '.png')
        if output_path.is_file():
            raise ValueError('Output file %s already exists!' % str(output_path))

    data_paths = [PosixPath(f) for f in glob(args['data'])]
    data = []
    for p in data_paths:
        if not p.is_file():
            raise ValueError('File %s does not exist.' % str(p))
        with p.open('rb') as f:
            data.append(cPickle.load(f))

    plt.figure() 
    max_its = 0
    for d in data:
        res = d['res']
        res = [sum(r) for r in res]
        if 0 in res:
            res = [r + 1e-20 for r in res]
        it = range(len(res))
        if len(res) > max_its:
            max_its = len(res)
        plt.plot(it, res, label=d['name'])
    plt.semilogy()
    step = int(max_its / 10)
    plt.xticks(range(0, max_its+1, step))
    plt.title(args['title']) 
    plt.ylabel('residual')
    plt.xlabel('iterations')
    plt.legend()

    if args['output'] is not None:
        plt.savefig(str(output_path))
    else:
        datacursor(formatter='{label}'.format)
        plt.show() 
Example 80
Project: DnD4py   Author: bacook17   File: roll4me.py    MIT License 4 votes vote down vote up
def roll():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description=(
            """
            Parses a list of dice rolls and modifiers, and prints
            the total of one realization, the individual rolls that
            produced that total, and the mean/percentile of that roll
            relative to the overall distribution.

            Example Usage:
            roll 1d10 + 3d6 + 8

            
            """))
    parser.add_argument('rolls', nargs='+', help=(
        """The list of rolls and modifiers to simulate."""))
    
    args = parser.parse_args().rolls
    message1 = "= "
    message2 = ""
    total = 0
    means = 0
    distribution = np.zeros(n_trials)
    for arg in args:
        if arg == '+':
            message1 += ' + '
            message2 += ' + '
        else:
            the_sum, rolls, mean, realizations = parse_roll(arg)
            message1 += str(the_sum) + ' '*(len(rolls) - len(str(the_sum)))
            message2 += rolls
            total += the_sum
            means += mean
            distribution += realizations
    print('*************')
    print('Total: {:4d}'.format(total))
    print('*************')
    print(message1)
    print(message2)
    print('Mean: {:.1f}'.format(means))
    print('Percentile: {:.1f}%'.format((total > distribution).mean()*100.))