Skip to content

Instantly share code, notes, and snippets.

@knzm
Created August 1, 2013 16:38
Show Gist options
  • Save knzm/6133057 to your computer and use it in GitHub Desktop.
Save knzm/6133057 to your computer and use it in GitHub Desktop.
def _chunker(it):
while True:
yield (it.next(), it.next())
def chunker(seq):
return map(list, _chunker(iter(seq)))
import unittest
class ChunkerTest(unittest.TestCase):
def test_balance(self):
result = chunker([1, 2, 3, 4, 5, 6])
expected = [[1, 2], [3, 4], [5, 6]]
self.assertEqual(result, expected)
def test_unbalance(self):
result = chunker([1, 2, 3, 4, 5])
expected = [[1, 2], [3, 4]]
self.assertEqual(result, expected)
def test_empty(self):
result = chunker([])
expected = []
self.assertEqual(result, expected)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment