Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created March 28, 2014 08:38
Show Gist options
  • Select an option

  • Save ir4y/9828078 to your computer and use it in GitHub Desktop.

Select an option

Save ir4y/9828078 to your computer and use it in GitHub Desktop.
infinity sequence generator for array
class Generator(object):
def __init__(self, array):
self.array = array
self.index = 0
def __getitem__(self, item):
if isinstance(item, slice):
start = item.start if item.start else 0
self.index = (self.index + start) % len(self.array)
return [self[0] for i in range(item.stop - start)]
else:
self.index = (self.index + item) % len(self.array)
item = self.array[self.index]
self.index = (self.index + 1) % len(self.array)
return item
array = [1,2,3,4,5]
g=Generator(array)
print(g[:10]) #return [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment