Group a iterable of (stmt, args) by stmt into (stmt, bulk_args).

    bulk_args will be a list of the args grouped by stmt.

    len(bulk_args) will be <= bulk_size
509451
def as_bulk_queries(queries, bulk_size):
    
    stmt_dict = defaultdict(list)
    for stmt, args in queries:
        bulk_args = stmt_dict[stmt]
        bulk_args.append(args)
        if len(bulk_args) == bulk_size:
            yield stmt, bulk_args
            del stmt_dict[stmt]
    for stmt, bulk_args in stmt_dict.items():
        yield stmt, bulk_args
647629
def _iter_grouped(self):
        """"""
        for indices in self._group_indices:
            yield self._obj.isel(**{self._group_dim: indices})
24646
def _get_stmt_by_group(self, stmt_type, stmts_this_type, eh):
        """"""
        # Dict of stmt group key tuples, indexed by their first Agent
        stmt_by_first = collections.defaultdict(lambda: [])
        # Dict of stmt group key tuples, indexed by their second Agent
        stmt_by_second = collections.defaultdict(lambda: [])
        # Dict of statements with None first, with second Agent as keys
        none_first = collections.defaultdict(lambda: [])
        # Dict of statements with None second, with first Agent as keys
        none_second = collections.defaultdict(lambda: [])
        # The dict of all statement groups, with tuples of components
        # or entity_matches_keys as keys
        stmt_by_group = collections.defaultdict(lambda: [])
        # Here we group Statements according to the hierarchy graph
        # components that their agents are part of
        for stmt_tuple in stmts_this_type:
            _, stmt = stmt_tuple
            entities = self._get_entities(stmt, stmt_type, eh)
            # At this point we have an entity list
            # If we're dealing with Complexes, sort the entities and use
            # as dict key
            if stmt_type == Complex:
                # There shouldn't be any statements of the type
                # e.g., Complex([Foo, None, Bar])
                assert None not in entities
                assert len(entities) > 0
                entities.sort()
                key = tuple(entities)
                if stmt_tuple not in stmt_by_group[key]:
                    stmt_by_group[key].append(stmt_tuple)
            elif stmt_type == Conversion:
                assert len(entities) > 0
                key = (entities[0],
                       tuple(sorted(entities[1:len(stmt.obj_from)+1])),
                       tuple(sorted(entities[-len(stmt.obj_to):])))
                if stmt_tuple not in stmt_by_group[key]:
                    stmt_by_group[key].append(stmt_tuple)
            # Now look at all other statement types
            # All other statements will have one or two entities
            elif len(entities) == 1:
                # If only one entity, we only need the one key
                # It should not be None!
                assert None not in entities
                key = tuple(entities)
                if stmt_tuple not in stmt_by_group[key]:
                    stmt_by_group[key].append(stmt_tuple)
            else:
                # Make sure we only have two entities, and they are not both
                # None
                key = tuple(entities)
                assert len(key) == 2
                assert key != (None, None)
                # First agent is None; add in the statements, indexed by
                # 2nd
                if key[0] is None and stmt_tuple not in none_first[key[1]]:
                    none_first[key[1]].append(stmt_tuple)
                # Second agent is None; add in the statements, indexed by
                # 1st
                elif key[1] is None and stmt_tuple not in none_second[key[0]]:
                    none_second[key[0]].append(stmt_tuple)
                # Neither entity is None!
                elif None not in key:
                    if stmt_tuple not in stmt_by_group[key]:
                        stmt_by_group[key].append(stmt_tuple)
                    if key not in stmt_by_first[key[0]]:
                        stmt_by_first[key[0]].append(key)
                    if key not in stmt_by_second[key[1]]:
                        stmt_by_second[key[1]].append(key)

        # When we've gotten here, we should have stmt_by_group entries, and
        # we may or may not have stmt_by_first/second dicts filled out
        # (depending on the statement type).
        if none_first:
            # Get the keys associated with stmts having a None first
            # argument
            for second_arg, stmts in none_first.items():
                # Look for any statements with this second arg
                second_arg_keys = stmt_by_second[second_arg]
                # If there are no more specific statements matching this
                # set of statements with a None first arg, then the
                # statements with the None first arg deserve to be in
                # their own group.
                if not second_arg_keys:
                    stmt_by_group[(None, second_arg)] = stmts
                # On the other hand, if there are statements with a matching
                # second arg component, we need to add the None first
                # statements to all groups with the matching second arg
                for second_arg_key in second_arg_keys:
                    stmt_by_group[second_arg_key] += stmts
        # Now do the corresponding steps for the statements with None as the
        # second argument:
        if none_second:
            for first_arg, stmts in none_second.items():
                # Look for any statements with this first arg
                first_arg_keys = stmt_by_first[first_arg]
                # If there are no more specific statements matching this
                # set of statements with a None second arg, then the
                # statements with the None second arg deserve to be in
                # their own group.
                if not first_arg_keys:
                    stmt_by_group[(first_arg, None)] = stmts
                # On the other hand, if there are statements with a matching
                # first arg component, we need to add the None second
                # statements to all groups with the matching first arg
                for first_arg_key in first_arg_keys:
                    stmt_by_group[first_arg_key] += stmts
        return stmt_by_group
662886
def batched(iterable, size):
    
    length = len(iterable)
    for batch_start in range(0, length, size):
        yield iterable[batch_start:batch_start+size]
1102258
public static <K, V, R extends MutableMultimap<K, V>> MutableMultimap<K, V> groupBy(
            Iterable<V> iterable,
            Function<? super V, ? extends K> function,
            R concurrentMultimap,
            int batchSize,
            ForkJoinPool executor)
    {
        FJIterate.forEach(
                iterable,
                new PassThruProcedureFactory<Procedure<V>>(new MultimapPutProcedure<>(concurrentMultimap, function)),
                Combiners.<Procedure<V>>passThru(),
                batchSize,
                executor);
        return concurrentMultimap;
    }
1040845
def p_arguments(self, p):
        
        if len(p) == 4:
            p[0] = self.asttypes.Arguments(p[2])
        else:
            p[0] = self.asttypes.Arguments([])
        p[0].setpos(p)
385785
List<String> argNames(int size) {
        List<String> argNames = new ArrayList<>();
        for (int i = 0 ; i < size ; i++) {
            argNames.add(StubKind.FACTORY_METHOD_ARG.format(i));
        }
        return argNames;
    }
172193
def group_by(self, *args):
        
        if len(args) == 1:
            self.raw_fields_group = args[0].split(',')
        else:
            self.raw_fields_group = list(args)
        return self
728966
def _convert_args(args):
    '''
    
    '''
    converted = []
    for arg in args:
        if isinstance(arg, dict):
            for key in list(arg.keys()):
                if key == '__kwarg__':
                    continue
                converted.append('{0}={1}'.format(key, arg[key]))
        else:
            converted.append(arg)
    return converted
295126
def group(data, num):
    
    return [data[i:i+num] for i in xrange(0, len(data), num)]
957500
def _list_or_args(self, keys, args):
        
        # returns a single list combining keys and args
        try:
            iter(keys)
            # a string can be iterated, but indicates
            # keys wasn't passed as a list
            if isinstance(keys, basestring):
                keys = [keys]
        except TypeError:
            keys = [keys]
        if args:
            keys.extend(args)
        return keys
327050
def grouper(iterable, n):
    ''''''
    args = [iter(iterable)] * n
    for group in izip_longest(fillvalue=None, *args):
        group = [g for g in group if g != None]
        yield group
216525
def _generic_callable(group_idx, a, size, fill_value, dtype=None,
                      func=lambda g: g, **kwargs):
    """"""
    groups = _array(group_idx, a, size, ())
    ret = np.full(size, fill_value, dtype=dtype or np.float64)

    for i, grp in enumerate(groups):
        if np.ndim(grp) == 1 and len(grp) > 0:
            ret[i] = func(grp)
    return ret
782440
def grouped(self):
        

        collection = OrderedDict(_=Args(no_argv=True))

        _current_group = None

        for arg in self.all:
            if arg.startswith('-'):
                _current_group = arg
                collection.setdefault(arg, Args(no_argv=True))
            else:
                if _current_group:
                    collection[_current_group]._args.append(arg)
                else:
                    collection['_']._args.append(arg)

        return collection
740342
def group(iterable, key):
    
    for _, grouped in groupby(sorted(iterable, key=key), key=key):
        yield list(grouped)
1325075
def _prepare_group2ctxs(group2ctxs, ctx_len):
    
    if group2ctxs is None:
        return [None] * ctx_len
    elif isinstance(group2ctxs, list):
        assert(len(group2ctxs) == ctx_len), "length of group2ctxs\
            should be %d" % ctx_len
        return group2ctxs
    elif isinstance(group2ctxs, dict):
        ret = [{} for i in range(ctx_len)]
        for k, v in group2ctxs.items():
            ctxs = None
            if isinstance(v, ctx.Context):
                ctxs = [v] * ctx_len
            else:
                if len(v) == 1:
                    ctxs = v * ctx_len
                else:
                    assert(len(v) == ctx_len), "length of group2ctxs[%s]\
                        should be %d or 1" % (k, ctx_len)
                    ctxs = v
            for i in range(ctx_len):
                ret[i][k] = ctxs[i]
        return ret
    else:
        assert(False), "group2ctxs should be list of dict of str to context,\
            or dict of str to context or list of context"
        return False
1220865
def full_groupby(iterable, key=None):
    

    return groupby(sorted(iterable, key=key), key=key)