Created
March 28, 2014 08:38
-
-
Save ir4y/9828078 to your computer and use it in GitHub Desktop.
infinity sequence generator for array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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