-
Notifications
You must be signed in to change notification settings - Fork 2
/
batched.py
48 lines (34 loc) · 1.37 KB
/
batched.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class batched:
def __init__(self, iterable, n):
raise NotImplementedError()
def test_batched_is_iterator():
"""Verify that `batched` is (or, at least, looks like) an iterator."""
assert hasattr(batched, "__next__")
assert hasattr(batched, "__iter__")
nw = batched([], 5)
assert iter(nw) is nw
def test_batched_is_class():
"""Verify that `batched` was defined as a class."""
assert isinstance(batched, type) # Check that `batched` is defined as a class...
assert batched.__name__ == "batched" # ... with the correct name.
def test_batched_mississippi():
"""Verify that `batched` works."""
assert list(batched("Mississippi", 4)) == [
("M", "i", "s", "s"),
("i", "s", "s", "i"),
("p", "p", "i"),
]
def test_batched_even_division():
"""Verify that `batched` works when `n` divides evenly into the source iterable."""
assert list(batched(range(6), 2)) == [(0, 1), (2, 3), (4, 5)]
def test_batched_too_short():
"""Test `batched` with a short iterable."""
assert list(batched("abc", 4)) == [("a", "b", "c")]
def test_batched_empty():
"""Test `batched` with an empty iterable."""
assert list(batched("", 3)) == []
def test_batched_batches_of_one():
"""Test `batched` with `n=1`."""
assert list(batched("Hello, world!", 1)) == list(
map(lambda x: (x,), "Hello, world!")
)