0
0
mirror of https://github.com/python/cpython.git synced 2024-11-28 16:45:42 +01:00
cpython/Tools/c-analyzer/c_common/iterutil.py
Nikita Sobolev 1ca315538f
gh-102033: Fix syntax error in Tools/c-analyzer (GH-102066)
The easiest way to format strings with `{}` meaningful chars is via `%`.
2023-03-22 07:59:32 -07:00

45 lines
1.0 KiB
Python

def peek_and_iter(items):
if not items:
return None, None
items = iter(items)
try:
peeked = next(items)
except StopIteration:
return None, None
def chain():
yield peeked
yield from items
return chain(), peeked
def iter_many(items, onempty=None):
if not items:
if onempty is None:
return
if not callable(onempty):
raise onEmpty
items = onempty(items)
yield from iter_many(items, onempty=None)
return
items = iter(items)
try:
first = next(items)
except StopIteration:
if onempty is None:
return
if not callable(onempty):
raise onEmpty
items = onempty(items)
yield from iter_many(items, onempty=None)
else:
try:
second = next(items)
except StopIteration:
yield first, False
return
else:
yield first, True
yield second, True
for item in items:
yield item, True