Skip to content

Commit 3fe2625

Browse files
authored
Merge pull request #322 from renato145/fix_chunked
chunked() getting stuck
2 parents 2d46ecc + e8b0c48 commit 3fe2625

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

fastcore/basics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def gen(func, seq, cond=true):
210210
def chunked(it, chunk_sz=None, drop_last=False, n_chunks=None):
211211
"Return batches from iterator `it` of size `chunk_sz` (or return `n_chunks` total)"
212212
assert bool(chunk_sz) ^ bool(n_chunks)
213-
if n_chunks: chunk_sz = math.ceil(len(it)/n_chunks)
213+
if n_chunks: chunk_sz = max(math.ceil(len(it)/n_chunks), 1)
214214
if not isinstance(it, Iterator): it = iter(it)
215215
while True:
216216
res = list(itertools.islice(it, chunk_sz))

nbs/01_basics.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@
11651165
"def chunked(it, chunk_sz=None, drop_last=False, n_chunks=None):\n",
11661166
" \"Return batches from iterator `it` of size `chunk_sz` (or return `n_chunks` total)\"\n",
11671167
" assert bool(chunk_sz) ^ bool(n_chunks)\n",
1168-
" if n_chunks: chunk_sz = math.ceil(len(it)/n_chunks)\n",
1168+
" if n_chunks: chunk_sz = max(math.ceil(len(it)/n_chunks), 1)\n",
11691169
" if not isinstance(it, Iterator): it = iter(it)\n",
11701170
" while True:\n",
11711171
" res = list(itertools.islice(it, chunk_sz))\n",
@@ -1197,7 +1197,10 @@
11971197
"\n",
11981198
"t = np.arange(10)\n",
11991199
"test_eq(chunked(t,3), [[0,1,2], [3,4,5], [6,7,8], [9]])\n",
1200-
"test_eq(chunked(t,3,True), [[0,1,2], [3,4,5], [6,7,8], ])"
1200+
"test_eq(chunked(t,3,True), [[0,1,2], [3,4,5], [6,7,8], ])\n",
1201+
"\n",
1202+
"test_eq(chunked([], 3), [])\n",
1203+
"test_eq(chunked([], n_chunks=3), [])"
12011204
]
12021205
},
12031206
{

0 commit comments

Comments
 (0)