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
from functools import wraps | |
import inspect | |
def first(fn): | |
'''Wrap a function so True is returned the first time it is called.''' | |
@wraps(fn) | |
def wrapper(self, *args, **kwargs): | |
if self.first: | |
self.first = False |
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
from itertools import tee, izip | |
def inwise(iterable, n=2): | |
'''Return an iterator over iterable where n adjacent items are returned at | |
a time. | |
@param n : int | |
the number of adjacent items to return''' | |
iters = tee(iterable, n=n) |
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
#! /usr/bin/env python | |
from itertools import izip_longest | |
def ichunk(iterable, n): | |
'''Chunk n adjacent elements in the iterable. For example | |
ichunk((1,2,3,4,5,6), 2) -> (1,2),(3,4),(5,6)''' | |
iters = (iter(iterable),) * n | |
return izip_longest(*iters, fillvalue=None) |