• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2_NOT_SET = object()
3
4
5def peek_and_iter(items):
6    if not items:
7        return None, None
8    items = iter(items)
9    try:
10        peeked = next(items)
11    except StopIteration:
12        return None, None
13    def chain():
14        yield peeked
15        yield from items
16    return chain(), peeked
17
18
19def iter_many(items, onempty=None):
20    if not items:
21        if onempty is None:
22            return
23        if not callable(onempty):
24            raise onEmpty
25        items = onempty(items)
26        yield from iter_many(items, onempty=None)
27        return
28    items = iter(items)
29    try:
30        first = next(items)
31    except StopIteration:
32        if onempty is None:
33            return
34        if not callable(onempty):
35            raise onEmpty
36        items = onempty(items)
37        yield from iter_many(items, onempty=None)
38    else:
39        try:
40            second = next(items)
41        except StopIteration:
42            yield first, False
43            return
44        else:
45            yield first, True
46            yield second, True
47        for item in items:
48            yield item, True
49