batch: use builtin next() and catch StopIteration (#38781)
Catching StopIteration is for 3.7 compatibility: https://www.python.org/dev/peps/pep-0479/
This commit is contained in:
parent
f44ffec80f
commit
c9ccc87626
|
@ -355,7 +355,10 @@ def batch(iterable, size):
|
||||||
batchiter = islice(sourceiter, size)
|
batchiter = islice(sourceiter, size)
|
||||||
# call next() at least one time to advance, if the caller does not
|
# call next() at least one time to advance, if the caller does not
|
||||||
# consume the returned iterators, sourceiter will never be exhausted.
|
# consume the returned iterators, sourceiter will never be exhausted.
|
||||||
yield chain([batchiter.next()], batchiter)
|
try:
|
||||||
|
yield chain([next(batchiter)], batchiter)
|
||||||
|
except StopIteration:
|
||||||
|
return
|
||||||
|
|
||||||
# legacy import, other modules keep importing to_json from passerelle.utils
|
# legacy import, other modules keep importing to_json from passerelle.utils
|
||||||
from .jsonresponse import to_json
|
from .jsonresponse import to_json
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
from passerelle.utils import batch
|
||||||
|
|
||||||
|
|
||||||
|
def test_batch():
|
||||||
|
data = range(10)
|
||||||
|
for i, d in enumerate(batch(data, 1)):
|
||||||
|
assert list(d) == [i]
|
Loading…
Reference in New Issue