Skip to content

Instantly share code, notes, and snippets.

Avatar

Zviki Cohen zvikico

View GitHub Profile
@zvikico
zvikico / gist:5805014
Created Jun 18, 2013
This function will interleave results from multiple iterators, exhausting all iterators in the process (i.e. ignoring iterators that has no more value).
View gist:5805014
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()
@zvikico
zvikico / reducing_dict.py
Created Jul 25, 2012
Reducing Dictionary in Python
View reducing_dict.py
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:
@zvikico
zvikico / gist:2863226
Created Jun 3, 2012
Reducing dict in Python - a dict that reduces values as they are entered
View gist:2863226
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):
You can’t perform that action at this time.