Skip to content

Commit a5d409b

Browse files
committed
refactor: replace custom batches function with native itertools.batched
- Remove custom batches() implementation from coderdojochi/util.py - Use native itertools.batched() for email recipient batching - Modernize code to leverage Python 3.12+ standard library features Addresses requirements 3.1 and 6.4 from python-modernization spec
1 parent dff21e0 commit a5d409b

File tree

3 files changed

+84
-110
lines changed

3 files changed

+84
-110
lines changed

.kiro/specs/python-modernization/tasks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
- Update `Dockerfile` base image from `python:3.11.9` to `python:3.13`
88
- _Requirements: 1.1, 4.1, 4.2, 4.3_
99

10-
- [ ] 2. Regenerate dependency lock file for Python 3.13
10+
- [x] 2. Regenerate dependency lock file for Python 3.13
1111

1212
- Run `docker compose run --rm app uv lock --upgrade` to regenerate uv.lock for Python 3.13
1313
- Test that all dependencies resolve correctly with `docker compose run --rm app uv sync --frozen`
1414
- Verify no compatibility issues with existing dependencies
1515
- _Requirements: 5.1, 5.2_
1616

17-
- [ ] 3. Replace custom batches function with native itertools.batched
17+
- [x] 3. Replace custom batches function with native itertools.batched
1818

1919
- Remove the custom `batches()` function from `coderdojochi/util.py`
2020
- Replace all usage of `batches()` with `itertools.batched()` in the email function

coderdojochi/util.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from itertools import batched
23

34
from anymail.message import AnymailMessage
45
from django.conf import settings
@@ -66,7 +67,7 @@ def email(
6667
"group_id": unsub_group_id,
6768
}
6869

69-
for recipients_batch in batches(recipients, batch_size):
70+
for recipients_batch in batched(recipients, batch_size):
7071
msg = AnymailMessage(
7172
subject=subject,
7273
body=body,
@@ -107,20 +108,4 @@ def email(
107108
user.save()
108109

109110

110-
def batches(items, batch_size):
111-
"""
112-
Split a list into smaller batches of a specified size.
113111

114-
Args:
115-
items (list): The list of items to be split into batches
116-
batch_size (int): The maximum number of items per batch
117-
118-
Yields:
119-
list: A batch containing up to batch_size items from the original list
120-
121-
Example:
122-
>>> list(batches([1, 2, 3, 4, 5], 2))
123-
[[1, 2], [3, 4], [5]]
124-
"""
125-
for start_index in range(0, len(items), batch_size):
126-
yield items[start_index : start_index + batch_size]

0 commit comments

Comments
 (0)