This file contains hidden or 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 copy | |
class ReducingDict(dict): | |
def __init__(self, function, initial = None): | |
self._function = function | |
self._initial = initial | |
super(ReducingDict, self).__init__() | |
def __setitem__(self, key, new_value): |
This file contains hidden or 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
class ReducingDict(dict): | |
def __init__(self, function, initial = None): | |
self._function = function | |
self._initial = initial | |
super(ReducingDict, self).__init__() | |
def __setitem__(self, key, new_value): | |
if key in self: |
This file contains hidden or 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 itertools | |
def iter_interleave(*args): | |
iterators = [(x for x in it) for it in args] | |
has_values = [True for _ in xrange(len(iterators))] | |
while max(has_values): | |
for it, has_value, i in itertools.izip(iterators, has_values, xrange(len(iterators))): | |
if has_value: | |
try: | |
value = it.next() |