Iterate over space-separated import chunks in a file.
175227
def iter_import_chunks(self):
    """"""
    chunk = []
    last_line = None
    for leaf in self.python_file.tree.body:
      if isinstance(leaf, (ast.Import, ast.ImportFrom)):
        # we've seen previous imports but this import is not in the same chunk
        if last_line and leaf.lineno != last_line[1]:
          yield chunk
          chunk = [leaf]
        # we've either not seen previous imports or this is part of the same chunk
        elif not last_line or last_line and leaf.lineno == last_line[1]:
          chunk.append(leaf)
        last_line = self.python_file.logical_lines[leaf.lineno]
    if chunk:
      yield chunk
103541
def p_import(self, p):
        ''
        p[0] = AstImport(self.path, p.lineno(1), p.lexpos(1), p[2])
512392
def chunks(cls, iterable, n, fill=None):
    

    return cls(itertools.zip_longest(*[iter(iterable)] * n, fillvalue=fill))
1199140
def chunks(stream, chunk_size, output_type=tuple):
    '''  '''
    assert iterable(stream), 'chunks needs stream to be iterable'
    assert (isinstance(chunk_size, int) and chunk_size > 0) or callable(chunk_size), 'chunks needs chunk_size to be a positive int or callable'
    assert callable(output_type), 'chunks needs output_type to be callable'
    if callable(chunk_size):
        ''' chunk_size is acting as a separator function '''
        for chunk in chunk_on(stream, chunk_size, output_type):
            yield chunk
    else:
        it = iter(stream)
        marker = object()
        iters = [it] * chunk_size
        pipeline = apply_to_last(
           zip_longest(*iters, fillvalue=marker),
           lambda last_chunk: tuple(i for i in last_chunk if i is not marker)
        )
        if output_type is not tuple:
            pipeline = map(output_type, pipeline)
        for chunk in pipeline:
            yield chunk
1246104
def batch(input_iter, batch_size=32):
  """"""
  input_iter = iter(input_iter)
  next_ = list(itertools.islice(input_iter, batch_size))
  while next_:
    yield next_
    next_ = list(itertools.islice(input_iter, batch_size))
1060760
def iter_chunks(l, size):
    
    if isinstance(l, six.integer_types):
        l = six.moves.range(l)

    if is_lazy_iterable(l):
        if size < 1:
            yield list(l)
        else:
            chunk = []
            for elem in l:
                if len(chunk) < size:
                    chunk.append(elem)
                else:
                    yield chunk
                    chunk = [elem]
            else:
                if chunk:
                    yield chunk

    else:
        if size < 1:
            yield l
        else:
            for i in six.moves.range(0, len(l), size):
                yield l[i:i + size]
675678
def iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None, delimiter=None):
        

        pending = None

        for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode):

            if pending is not None:
                chunk = pending + chunk

            if delimiter:
                lines = chunk.split(delimiter)
            else:
                lines = chunk.splitlines()

            if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
                pending = lines.pop()
            else:
                pending = None

            for line in lines:
                yield line

        if pending is not None:
            yield pending
1279320
def chunked(l, n):
    """"""
    return [l[i:i + n] for i in range(0, len(l), n)]
1129966
def chunks_str(str, n, separator="\n", fill_blanks_last=True):
    
    return separator.join(chunks(str, n))
759280
def chunks(l:Collection, n:int)->Iterable:
    ""
    for i in range(0, len(l), n): yield l[i:i+n]
1151003
def _chunks(iterable, n):
    
    iterable = iter(iterable)
    while True:
        # store one line in memory,
        # chain it to an iterator on the rest of the chunk
        yield chain([next(iterable)], islice(iterable, n-1))
1337142
def iter_slices(string, slice_length):
    """"""
    pos = 0
    if slice_length is None or slice_length <= 0:
        slice_length = len(string)
    while pos < len(string):
        yield string[pos:pos + slice_length]
        pos += slice_length
477043
def iter_tex_documents(base_dir="."):
    """"""
    for path, dirlist, filelist in os.walk(base_dir):
        for name in fnmatch.filter(filelist, "*.tex"):
            yield os.path.join(path, name)
403628
def data_chunk(data, chunk, with_overlap=False):
    """"""
    assert isinstance(chunk, tuple)
    if len(chunk) == 2:
        i, j = chunk
    elif len(chunk) == 4:
        if with_overlap:
            i, j = chunk[:2]
        else:
            i, j = chunk[2:]
    else:
        raise ValueError("'chunk' should have 2 or 4 elements, "
                         "not {0:d}".format(len(chunk)))
    return data[i:j, ...]
1259847
def chunks(lst, size):
    """"""
    for i in xrange(0, len(lst), size):
        yield lst[i:i + size]
1212239
def dict_chunks(dictionary, chunk):
    """"""
    return [{k: v for k, v in dictionary.items() if k in i} for i in chunks(list(dictionary.keys()), chunk)]
964895
def _file_iter_range(fp, offset, bytes, maxread=1024*1024):
    ''' '''
    fp.seek(offset)
    while bytes > 0:
        part = fp.read(min(bytes, maxread))
        if not part: break
        bytes -= len(part)
        yield part