Skip to content

Instantly share code, notes, and snippets.

@suspectpart
Created June 21, 2018 20:04
Show Gist options
  • Save suspectpart/9be4d21a03235e00c3ec45e42f197ed6 to your computer and use it in GitHub Desktop.
Save suspectpart/9be4d21a03235e00c3ec45e42f197ed6 to your computer and use it in GitHub Desktop.
Lookahead
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