itertools: Make cycle() work with finite generators

pull/42/head
stijn 2015-08-25 14:36:45 +02:00
rodzic ebb0c60fac
commit 2e3e764f80
1 zmienionych plików z 13 dodań i 2 usunięć

Wyświetl plik

@ -4,8 +4,19 @@ def count(start=0, step=1):
start += step
def cycle(p):
while p:
yield from p
try:
len(p)
while p:
yield from p
except TypeError:
# len() is not defined for this type. Assume it is
# a finite iterable so we must cache the elements.
cache = []
for i in p:
yield i
cache.append(i)
while cache:
yield from cache
def repeat(el, n=None):
if n is None: