Skip to content

Commit 94ad385

Browse files
committed
-
1 parent 29f9def commit 94ad385

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

python_toolbox/cute_iter_tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,13 @@ def sentinel(counter=([fill_value] * (len(iterables) - 1)).pop):
190190
yield tuple_
191191
except IndexError:
192192
raise StopIteration
193+
194+
195+
def get_items(iterable, n):
196+
'''
197+
Get the next `n` items from the iterable as a `tuple`.
198+
199+
If there are less than `n` items, no exception will be raised. Whatever
200+
items are there will be returned.
201+
'''
202+
return tuple(shorten(iterable, n))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2009-2012 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
'''Testing module for `cute_iter_tools.get_items`.'''
5+
6+
import itertools
7+
8+
9+
from python_toolbox.cute_iter_tools import get_items
10+
11+
12+
13+
def test():
14+
'''Test the basic workings of `get_items`.'''
15+
16+
iterable = iter(xrange(10))
17+
assert get_items(iterable, 3) == (0, 1, 2)
18+
assert get_items(iterable, 0) == ()
19+
assert get_items(iterable, 2) == (3, 4)
20+
assert get_items(iterable, 4) == (5, 6, 7, 8)
21+
assert get_items(iterable, 3) == (9,)
22+
assert get_items(iterable, 3) == ()
23+
assert get_items(iterable, 4) == ()

0 commit comments

Comments
 (0)