Skip to content

Commit 5b74c1e

Browse files
committed
fix slice_dictionary implementation
1 parent fbd3dd1 commit 5b74c1e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

aws_lambda_powertools/shared/functions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ def powertools_debug_is_set() -> bool:
140140

141141

142142
def slice_dictionary(data: dict, chunk_size: int) -> Generator[dict, None, None]:
143-
for _ in range(0, len(data), chunk_size):
144-
yield {dict_key: data[dict_key] for dict_key in itertools.islice(data, chunk_size)}
143+
for i in range(0, len(data), chunk_size):
144+
yield {key: data[key] for key in itertools.islice(data, i, i + chunk_size)}
145+
145146

146147

147148
def extract_event_from_common_models(data: Any) -> dict | Any:

tests/unit/test_shared_functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from aws_lambda_powertools.shared import constants
1212
from aws_lambda_powertools.shared.functions import (
1313
abs_lambda_path,
14+
slice_dictionary,
1415
extract_event_from_common_models,
1516
powertools_debug_is_set,
1617
powertools_dev_is_set,
@@ -202,3 +203,14 @@ def test_sanitize_xray_segment_name_with_no_special_characters():
202203
# THEN the sanitized name remains the same as the original name
203204
expected_name = valid_name
204205
assert sanitized_name == expected_name
206+
207+
208+
@pytest.mark.parametrize("chunk_size, expected", [
209+
(1, [{"k0": 0}, {"k1": 1}, {"k2": 2}, {"k3": 3}]),
210+
(2, [{"k0": 0, "k1": 1}, {"k2": 2, "k3": 3}]),
211+
(3, [{"k0": 0, "k1": 1, "k2": 2}, {"k3": 3}]),
212+
])
213+
def test_slice_dictionary(chunk_size, expected):
214+
data = {f"k{i}": i for i in range(4)}
215+
chunks = list(slice_dictionary(data, chunk_size=chunk_size))
216+
assert chunks == expected

0 commit comments

Comments
 (0)