Skip to content

Commit 8efd663

Browse files
committed
feat: add batch_items utility for chunking lists
1 parent 4f1eea1 commit 8efd663

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/lazy_ecs/core/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import threading
99
from collections.abc import Iterator
1010
from contextlib import contextmanager, suppress
11-
from typing import TYPE_CHECKING, Literal
11+
from typing import TYPE_CHECKING, Any, Literal
1212

1313
from rich.console import Console
1414
from rich.spinner import Spinner
@@ -80,6 +80,12 @@ def show_spinner() -> Iterator[None]:
8080
yield
8181

8282

83+
def batch_items(items: list[Any], batch_size: int) -> Iterator[list[Any]]:
84+
"""Split a list into batches of specified size."""
85+
for i in range(0, len(items), batch_size):
86+
yield items[i : i + batch_size]
87+
88+
8389
def paginate_aws_list(
8490
client: ECSClient,
8591
operation_name: Literal[

tests/test_utils.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""Tests for utility functions."""
2+
3+
from lazy_ecs.core.utils import batch_items
4+
5+
6+
def test_batch_items_basic():
7+
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8+
batches = list(batch_items(items, 3))
9+
10+
assert len(batches) == 4
11+
assert batches[0] == [1, 2, 3]
12+
assert batches[1] == [4, 5, 6]
13+
assert batches[2] == [7, 8, 9]
14+
assert batches[3] == [10]
15+
16+
17+
def test_batch_items_exact_fit():
18+
items = [1, 2, 3, 4, 5, 6]
19+
batches = list(batch_items(items, 3))
20+
21+
assert len(batches) == 2
22+
assert batches[0] == [1, 2, 3]
23+
assert batches[1] == [4, 5, 6]
24+
25+
26+
def test_batch_items_single_batch():
27+
items = [1, 2, 3]
28+
batches = list(batch_items(items, 10))
29+
30+
assert len(batches) == 1
31+
assert batches[0] == [1, 2, 3]
32+
33+
34+
def test_batch_items_empty_list():
35+
items = []
36+
batches = list(batch_items(items, 5))
37+
38+
assert len(batches) == 0
39+
40+
41+
def test_batch_items_size_one():
42+
items = [1, 2, 3, 4, 5]
43+
batches = list(batch_items(items, 1))
44+
45+
assert len(batches) == 5
46+
assert batches[0] == [1]
47+
assert batches[1] == [2]
48+
assert batches[2] == [3]
49+
assert batches[3] == [4]
50+
assert batches[4] == [5]
51+
52+
53+
def test_batch_items_strings():
54+
items = ["a", "b", "c", "d", "e", "f", "g"]
55+
batches = list(batch_items(items, 3))
56+
57+
assert len(batches) == 3
58+
assert batches[0] == ["a", "b", "c"]
59+
assert batches[1] == ["d", "e", "f"]
60+
assert batches[2] == ["g"]

0 commit comments

Comments
 (0)