Skip to content
Snippets Groups Projects
Commit 52f3af80 authored by Georges Racinet's avatar Georges Racinet
Browse files

util: `chunked` generator

It seems that many streaming Gitaly responses are actually
streaming responses which themselves contains chunks of content.
An example would be `RefService.FindAllBranchNames`.

Couldn't find anything really convincing in `itertools` for that.
parent 689c8a3c
No related branches found
No related tags found
No related merge requests found
# Copyright 2020 Georges Racinet <georges.racinet@octobus.net>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# default as in Gitaly's internal/helper/chunk/chunker.go
DEFAULT_CHUNK_SIZE = 20
def chunked(iterable, size=DEFAULT_CHUNK_SIZE):
"""Return a generator of chunks out of given iterable.
>>> [chunk for chunk in chunked(range(3), size=2)]
[[0, 1], [2]]
>>> [chunk for chunk in chunked(range(4), size=2)]
[[0, 1], [2, 3]]
"""
chunk = []
for i, val in enumerate(iterable):
if i != 0 and i % size == 0:
yield chunk
chunk = []
chunk.append(val)
yield chunk
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment