def windows(iterable, length=<NUM_LIT:2>, overlap=<NUM_LIT:0>, padding=True):
it = iter(iterable)<EOL>results = list(itertools.islice(it, length))<EOL>while len(results) == length:<EOL><INDENT>yield results<EOL>results = results[length-overlap:]<EOL>results.extend(itertools.islice(it, length-overlap))<EOL><DEDENT>if padding and results:<EOL><INDENT>results.extend(itertools.repeat(None, length-len(results)))<EOL>yield results<EOL><DEDENT>
Code snippet from Python Cookbook, 2nd Edition by David Ascher,
    Alex Martelli and Anna Ravenscroft; O'Reilly 2005

    Problem: You have an iterable s and need to make another iterable whose
    items are sublists (i.e., sliding windows), each of the same given length,
    over s' items, with successive windows overlapping by a specified amount.