Skip to content

Commit 72e8096

Browse files
add get_chunk method and unittest
1 parent 84fbd32 commit 72e8096

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pysenal/utils/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: UTF-8 -*-
2+
from typing import Iterable
3+
4+
5+
def get_chunk(l, n):
6+
"""
7+
get a chunk in iterable object
8+
:param l: iterable object
9+
:param n: chunk size
10+
:return: a chunk in list type
11+
"""
12+
if not isinstance(l, Iterable):
13+
raise TypeError('input value is not iterable')
14+
if hasattr(l, '__getitem__'):
15+
for i in range(0, len(l), n):
16+
yield l[i:i + n]
17+
else:
18+
chunk = []
19+
for i in l:
20+
chunk.append(i)
21+
if len(chunk) == n:
22+
yield chunk
23+
chunk = []
24+
25+
if chunk:
26+
yield chunk

tests/utils/test_utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: UTF-8 -*-
2+
from types import GeneratorType
3+
import pytest
4+
from pysenal.utils.utils import *
5+
6+
7+
def test_get_chunk():
8+
r1 = range(100)
9+
chunk_r1 = get_chunk(r1, 12)
10+
assert type(chunk_r1) == GeneratorType
11+
assert next(chunk_r1) == range(12)
12+
13+
l1 = [1, 3, 5, 7, 9]
14+
chunk_l1 = get_chunk(l1, 5)
15+
assert next(chunk_l1) == l1
16+
with pytest.raises(StopIteration):
17+
next(chunk_l1)
18+
19+
chunk_l1 = get_chunk(l1, 10)
20+
assert next(chunk_l1) == l1
21+
22+
g1 = (i for i in [1, 10, 100, 3, 4, 'a'])
23+
chunk_g1 = get_chunk(g1, 2)
24+
assert type(chunk_g1) == GeneratorType
25+
assert next(chunk_g1) == [1, 10]
26+
assert next(chunk_g1) == [100, 3]
27+
28+
g2 = (i for i in [20, 100, 40, 123])
29+
chunk_g2 = get_chunk(g2, 3)
30+
assert next(chunk_g2) == [20, 100, 40]
31+
assert next(chunk_g2) == [123]
32+
33+
g3 = (i for i in [10, 100, 200])
34+
chunk_g3 = get_chunk(g3, 5)
35+
assert next(chunk_g3, [10, 100, 200])
36+
with pytest.raises(StopIteration):
37+
next(chunk_g3)

0 commit comments

Comments
 (0)