Python plac.call() Examples

The following are 14 code examples of plac.call(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module plac , or try the search function .
Example #1
Source File: cli.py    From MicroTokenizer with MIT License 6 votes vote down vote up
def main(args=None):
    commands = {
        'train': train,
        'download': download,
        'link': link
    }
    if len(sys.argv) == 1:
        print("{title}: {content}, exits: {exits}".format(
            content=', '.join(commands),
            title="Available commands",
            exits=1)
        )

    command = sys.argv.pop(1)
    sys.argv[0] = 'MicroTokenizer %s' % command

    if command in commands:
        plac.call(commands[command], sys.argv[1:])
    else:
        print("{title}: {content}, exits: {exits}".format(
            content="Available: %s" % ', '.join(commands),
            title="Unknown command: %s" % command,
            exits=1)
        ) 
Example #2
Source File: plac_runner.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def main(verbose, interactive, multiline, serve, batch, test, fname='',
         *extra):
    "Runner for plac tools, plac batch files and plac tests"
    baseparser = plac.parser_from(main)
    if not fname:
        baseparser.print_help()
    elif sys.argv[1] == fname:  # script mode
        plactool = plac.import_main(fname)
        plactool.prog = os.path.basename(sys.argv[0]) + ' ' + fname
        out = plac.call(plactool, sys.argv[2:], eager=False)
        if plac.iterable(out):
            for output in out:
                print(output)
        else:
            print(out)
    elif interactive or multiline or serve:
        plactool = plac.import_main(fname, *extra)
        plactool.prog = ''
        i = plac.Interpreter(plactool)
        if interactive:
            i.interact(verbose=verbose)
        elif multiline:
            i.multiline(verbose=verbose)
        elif serve:
            i.start_server(serve)
    elif batch:
        run((fname,) + extra, 'execute', verbose)
    elif test:
        run((fname,) + extra, 'doctest', verbose)
        print('run %s plac test(s)' % (len(extra) + 1))
    else:
        baseparser.print_usage() 
Example #3
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_kwargs():
    def main(opt, arg1, *args, **kw):
        print(opt, arg1)
        return args, kw
    main.__annotations__ = dict(opt=('Option', 'option'))
    argskw = plac.call(main, ['arg1', 'arg2', 'a=1', 'b=2'])
    assert argskw == [('arg2',), {'a': '1', 'b': '2'}], argskw

    argskw = plac.call(main, ['arg1', 'arg2', 'a=1', '-o', '2'])
    assert argskw == [('arg2',), {'a': '1'}], argskw

    expect(SystemExit, plac.call, main, ['arg1', 'arg2', 'a=1', 'opt=2']) 
Example #4
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_kwargs2():
    # see https://github.com/micheles/plac/issues/39
    def main(**kw):
        return kw.items()
    assert plac.call(main, ['a=1']) == [('a', '1')]
    expect(SystemExit, plac.call, main, ['foo'])
    expect(SystemExit, plac.call, main, ['foo', 'a=1']) 
Example #5
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_kwargs3():
    # see https://github.com/micheles/plac/issues/38
    def main(opt='foo', **kw):
        return opt, kw
    main.__annotations__ = dict(opt=('Option', 'option'))
    assert plac.call(main, ['-o', 'abc=']) == ['abc=', {}]
    assert plac.call(main, ['-o', 'abc=', 'z=1']) == ['abc=', {'z': '1'}]
    assert plac.call(main, ['z=1']) == ['foo', {'z': '1'}] 
Example #6
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_cmds():
    assert 'commit' == plac.call(cmds, ['commit'])
    assert ['help', 'foo'] == plac.call(cmds, ['help', 'foo'])
    expect(SystemExit, plac.call, cmds, []) 
Example #7
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_sub_help():
    c = Cmds()
    c.add_help = True
    expect(SystemExit, plac.call, c, ['commit', '-h']) 
Example #8
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_yield():
    def main():
        for i in (1, 2, 3):
            yield i
    assert plac.call(main, []) == [1, 2, 3] 
Example #9
Source File: test_plac.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def check_script(args):
    if failing_scripts.intersection(args):
        assert subprocess.call(args) > 0, (  # expected failure
            'Unexpected success for %s' % ' '.join(args))
    else:
        assert subprocess.call(args) == 0, 'Failed %s' % ' '.join(args) 
Example #10
Source File: shelve_interpreter.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def main(interactive, *subcommands):
    """
    This script works both interactively and non-interactively.
    Use .help to see the internal commands.
    """
    if interactive:
        plac.Interpreter(ishelve.main).interact()
    else:
        for out in plac.call(ishelve.main, subcommands):
            print(out) 
Example #11
Source File: test_ishelve.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test():
    assert plac.call(ishelve.main, ['.clear']) == ['cleared the shelve']
    assert plac.call(ishelve.main, ['a=1']) == ['setting a=1']
    assert plac.call(ishelve.main, ['a']) == ['1']
    assert plac.call(ishelve.main, ['.delete=a']) == ['deleted a']
    assert plac.call(ishelve.main, ['a']) == ['a: not found'] 
Example #12
Source File: main.py    From typed-json-dataclass with MIT License 5 votes vote down vote up
def entrypoint():
  plac.call(main) 
Example #13
Source File: __main__.py    From news-please with Apache License 2.0 5 votes vote down vote up
def main():
    plac.call(cli) 
Example #14
Source File: sum_eval.py    From sumeval with Apache License 2.0 5 votes vote down vote up
def entry_point():
    plac.call(main)