def test_bipartition():
    odd, even = Stream(range(10)).bipartition(lambda x: x % 2 == 0)
    assert list(odd) == [1, 3, 5, 7, 9]
    assert list(even) == [0, 2, 4, 6, 8]
def test_dropwhile():
    values = list(Stream(range(5)).append(range(8, 2, -1)))
    assert list(values) == [0, 1, 2, 3, 4, 8, 7, 6, 5, 4, 3]
    assert list(
        Stream(values).dropwhile(lambda x: x < 4)) == [4, 8, 7, 6, 5, 4, 3]
def test_filter():
    assert list(Stream(
        range(10)).filter(lambda x: x % 2 == 0)) == [0, 2, 4, 6, 8]
def test_distinct():
    values = [1, 5, 6, 5, 3, 8, 1, 3, 9, 0]
    assert list(Stream(values).distinct()) == [1, 5, 6, 3, 8, 9, 0]
    assert list(Stream(values).distinct(skip=set([5, 3, 9]))) == [1, 6, 8, 0]
def test_getitem():
    assert list(Stream(range(10))[3:8]) == [3, 4, 5, 6, 7]
def test_flatmap():
    values = ['abc', 'def']
    assert ''.join(Stream(values).flatmap(lambda x: x)) == 'abcdef'
def test_map():
    values = [5, 2, 1]
    assert list(Stream(values).map(lambda x: x * 2)) == [10, 4, 2]
Exemple #8
0
def main(argv=None, prog=None):
    """
  Create standalone distributions of Python applications.
  """

    parser = get_argument_parser(prog)
    args, unknown = parser.parse_known_args(argv)

    if args.verbose == 0:
        level = logging.WARN
    else:
        level = logging.INFO
    logging.basicConfig(format='%(levelname)s: %(message)s', level=level)

    hook_options = {}
    for x in unknown:
        if not x.startswith('--hook-'):
            parser.error('unknown option: {}'.format(x))
        if '=' in x:
            key, value = x[7:].partition('=')[::2]
        else:
            key = x[7:]
            value = 'true'
        hook_options[key.lower()] = value

    if args.pex:
        if args.dist:
            parser.error('conflicting arguments: --pex and --dist')
        if not args.collect:
            args.collect = True
            args.exclude_stdlib = True

    if args.dist and args.exclude_stdlib:
        logging.warning(
            'using --exclude-stdlib with --dist may leave you with '
            'will leave you with a non-functional distribution.')

    builder = DistributionBuilder(
        collect=args.collect,
        collect_to=args.collect_to,
        dist=args.dist,
        pex_out=args.pex,
        pex_entrypoint=args.pex_entrypoint,
        pex_console_script=args.pex_console_script,
        pex_root=args.pex_root,
        pex_shebang=args.pex_shebang,
        entries=args.entry,
        resources=args.resource,
        bundle_dir=args.bundle_dir,
        excludes=split_multiargs(args.exclude),
        default_excludes=not args.no_default_excludes,
        exclude_stdlib=args.exclude_stdlib,
        includes=split_multiargs(args.args),
        whitelist=split_multiargs(args.whitelist),
        default_includes=not args.no_default_includes,
        compile_modules=args.compile_modules,
        zip_modules=args.zip_modules,
        zip_file=args.zip_file,
        srcs=not args.no_srcs,
        copy_always=args.copy_always,
        module_path=split_multiargs(args.module_path),
        default_module_path=not args.no_default_module_path,
        hooks_path=split_multiargs(args.hooks_path),
        default_hooks_path=not args.no_default_hooks_path,
        hook_options=hook_options)

    if args.show_module_path:
        dump_list(builder.finder.path, args)
        return 0

    if args.show_hooks_path:
        dump_list(builder.hook.path, args)
        return 0

    if args.package_members:
        result = {}
        flat = []
        for name in args.args:
            module = builder.finder.find_module(name)
            if not args.json:
                print('{} ({})'.format(module.name, module.type))
            contents = result.setdefault(module.name, [])
            for submodule in Stream(
                [[module],
                 builder.finder.iter_package_modules(module)]).concat():
                data = {
                    'name': submodule.name,
                    'type': submodule.type,
                    'filename': submodule.filename
                }
                contents.append(data)
                flat.append(data)
                if not args.json and submodule != module:
                    print('  {} ({})'.format(submodule.name, submodule.type))
        if args.json:
            json.dump(flat if args.flat else result,
                      sys.stdout,
                      indent=2,
                      sort_keys=True)
        return 0

    if args.deps:

        def callback(module, depth):
            print('  ' * depth + '{} ({})'.format(module.name, module.type))

        if args.json or args.dotviz or args.json_graph:
            callback = None
        for name in args.args:
            builder.graph.collect_modules(name, callback=callback)
        if args.json_graph:
            nodes = set()
            graph = {'nodes': [], 'edges': []}

            def mknode(name):
                if name not in nodes:
                    graph['nodes'].append({'id': name})
                    nodes.add(name)

            def mkedge(a, b):
                mknode(a)
                mknode(b)
                graph['edges'].append({'source': a, 'target': b})

            for mod in builder.graph:
                mknode(mod.name)
                for name in mod.imported_from:
                    mkedge(mod.name, name)
            json.dump({'graph': graph}, sys.stdout, indent=2, sort_keys=True)
        """
    # TODO: Dotviz output
    result = []
    current = []
    flat = []
    if args.flat:
      show = lambda mod: print('{} ({})'.format(mod.name, mod.type))
    else:
      show = lambda mod: print('  ' * len(mod.imported_from) + '{} ({})'.format(mod.name, mod.type))
    for name in args.args:
      module = finder.find_module(name)
      for module in finder.iter_modules(module, recursive=True):
        if not args.json: show(module)
        data = {'name': module.name, 'type': module.type, 'filename': module.filename}
        if args.flat:
          flat.append(data)
        else:
          assert len(module.imported_from) <= len(current)+1
          data['imports'] = []
          current = current[:len(module.imported_from)]
          if current:
            current[-1]['imports'].append(data)
          else:
            result.append(data)
          current.append(data)
    if args.json:
      json.dump(flat if args.flat else result, sys.stdout, indent=2, sort_keys=True)
    """
        return 0

    if args.nativedeps:
        # TODO: Dotviz output

        result = {}
        if args.search:
            result[args.search] = []
        for filename in args.args:
            deps = nativedeps.Collection(exclude_system_deps=True)
            deps.add(filename,
                     dependencies_only=True,
                     recursive=args.recursive)
            if args.search:
                for dep in deps:
                    if dep.name.lower() == args.search.lower():
                        if not args.json:
                            print(filename)
                        result[args.search].append(filename)
                        break
            else:
                result[filename] = []
                if not args.json:
                    print(filename)
                for dep in deps:
                    dep_filename = nativedeps.resolve_dependency(dep)
                    if not args.json:
                        print('  {}'.format(dep_filename or dep.name))
                    result[filename].append({
                        'name': dep.name,
                        'filename': dep_filename
                    })
        if args.json:
            json.dump(result, sys.stdout, indent=2, sort_keys=True)
        return 0

    show_usage = not builder.build()
    if show_usage:
        parser.print_usage()
        return 0
def test_collect_immediate_return():
    values = [1, 2, 3]
    assert Stream(values).collect() is values
    assert Stream(iter(values)).collect() == values
    assert Stream(values).collect(lambda x: list(x)) is not values
    assert Stream(values).collect(lambda x: list(x)) == values
def test_sort():
    values = [3, 2, 7]
    assert list(Stream(values).sort()) == [2, 3, 7]
    assert Stream(values).sort().collect() == [2, 3, 7]
def test_call():
    funcs = [(lambda x: x * 2), (lambda x: x + 2), (lambda x: x // 2)]
    assert list(Stream(funcs).call(3)) == [6, 5, 1]
def test_length():
    values = [4, 2, 7]
    assert Stream(values).flatmap(lambda x: ' ' * x).count() == sum(values)
def test_slice():
    assert list(Stream(range(10)).slice(3, 8)) == [3, 4, 5, 6, 7]
    assert list(Stream(range(10))[3:8]) == [3, 4, 5, 6, 7]
def test_stream_module_members():
    assert Stream(
        [1, 2,
         3]).flatmap(lambda x: [x, x + 1]).collect() == [1, 2, 2, 3, 3, 4]
def test_dropnone():
    assert Stream([None, 42, None, 99]).dropnone().collect() == [42, 99]
def test_takewhile():
    values = list(Stream(range(5)).append(range(8, 2, -1)))
    assert list(values) == [0, 1, 2, 3, 4, 8, 7, 6, 5, 4, 3]
    assert list(Stream(values).takewhile(lambda x: x < 8)) == [0, 1, 2, 3, 4]
    assert list(Stream(values).takewhile(lambda x: x > 0)) == []
def test_first():
    assert Stream([42, 99]).first() == 42
    assert Stream().first() is None
Exemple #18
0
def split_multiargs(value):
    if not value:
        return []
    return Stream(x.split(',') for x in value).concat().collect()
Exemple #19
0
 def stream(self) -> 'Stream[T]':
     from nr.stream import Stream
     if self._value is None:
         return Stream()
     return Stream([self._value])