Created
June 21, 2018 20:04
-
-
Save suspectpart/9be4d21a03235e00c3ec45e42f197ed6 to your computer and use it in GitHub Desktop.
Lookahead
This file contains 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
import unittest | |
class Lookahead(object): | |
def __init__(self, things, count): | |
self.things = tuple(things) | |
self.count = count | |
self.windows = len(things) - count + 1 | |
def __iter__(self): | |
return (self.things[i:i+self.count] for i in range(self.windows)) | |
class LookahedTests(unittest.TestCase): | |
def test_iterator(self): | |
lookahead = Lookahead([1,2,"a",4,5,6,7,8,9], 4) | |
iterator = iter(lookahead) | |
self.assertEqual((1,2,"a",4), next(iterator)) | |
self.assertEqual((2,"a",4,5), next(iterator)) | |
self.assertEqual(("a",4,5,6), next(iterator)) | |
self.assertEqual((4,5,6,7), next(iterator)) | |
self.assertEqual((5,6,7,8), next(iterator)) | |
self.assertEqual((6,7,8,9), next(iterator)) | |
with self.assertRaises(StopIteration): | |
next(iterator) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment