Python itertools.tee() Examples
The following are 30
code examples of itertools.tee().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
itertools
, or try the search function
.
Example #1
Source File: UtilitiesTest.py From coala-quickstart with GNU Affero General Public License v3.0 | 6 votes |
def test_peek(self): def give_gen(): for i in range(1, 5): yield i def give_empty_gen(): for i in range(1, 1): yield i obj = give_gen() for i in range(1, 5): num, new_obj = peek(obj) obj, new_obj = itertools.tee(obj) self.assertEqual(i, num) ret_val = peek(obj) obj = give_empty_gen() ret_val_1 = peek(obj) self.assertEqual(ret_val, None) self.assertEqual(ret_val_1, None)
Example #2
Source File: __init__.py From Computable with MIT License | 6 votes |
def follow_path(self, path, types, call_scope): """ Follows a path like:: self.follow_path(iter(['Foo', 'bar']), [a_type], from_somewhere) to follow a call like ``module.a_type.Foo.bar`` (in ``from_somewhere``). """ results_new = [] iter_paths = itertools.tee(path, len(types)) for i, typ in enumerate(types): fp = self._follow_path(iter_paths[i], typ, call_scope) if fp is not None: results_new += fp else: # This means stop iteration. return types return results_new
Example #3
Source File: the-birthday-bar.py From hackerrank with MIT License | 6 votes |
def sliding_window(n, seq): """ Copied from toolz https://toolz.readthedocs.io/en/latest/_modules/toolz/itertoolz.html#sliding_window A sequence of overlapping subsequences >>> list(sliding_window(2, [1, 2, 3, 4])) [(1, 2), (2, 3), (3, 4)] This function creates a sliding window suitable for transformations like sliding means / smoothing >>> mean = lambda seq: float(sum(seq)) / len(seq) >>> list(map(mean, sliding_window(2, [1, 2, 3, 4]))) [1.5, 2.5, 3.5] """ return zip(*(collections.deque(itertools.islice(it, i), 0) or it for i, it in enumerate(itertools.tee(seq, n))))
Example #4
Source File: prepare_data.py From Hierarchical-Sentiment with MIT License | 6 votes |
def build_dataset(args): print("Building dataset from : {}".format(args.input)) print("-> Building {} random splits".format(args.nb_splits)) nlp = spacy.load('en', create_pipeline=custom_pipeline) gen_a,gen_b = itertools.tee(data_generator(args.input),2) data = [(z["reviewerID"],z["asin"],tok,z["overall"]) for z,tok in zip(tqdm((z for z in gen_a),desc="reading file"),nlp.pipe((x["reviewText"] for x in gen_b), batch_size=1000000, n_threads=8))] print(data[0]) shuffle(data) splits = [randint(0,args.nb_splits-1) for _ in range(0,len(data))] count = Counter(splits) print("Split distribution is the following:") print(count) return {"data":data,"splits":splits,"rows":("user_id","item_id","review","rating")}
Example #5
Source File: schema.py From signac with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __call__(self, jobs_or_statepoints): "Evaluate the schema for the given state points." s = dict() iterators = itertools.tee(jobs_or_statepoints, len(self)) for key, it in zip(self, iterators): values = [] keys = key.split('.') for sp in it: if not isinstance(sp, Mapping): sp = sp.statepoint v = sp[keys[0]] for k in keys[1:]: v = v[k] values.append(v) s[key] = _collect_by_type(values) return ProjectSchema(s)
Example #6
Source File: utils.py From pythonfinder with MIT License | 6 votes |
def unnest(item): # type: (Any) -> Iterable[Any] target = None # type: Optional[Iterable] if isinstance(item, Iterable) and not isinstance(item, six.string_types): item, target = itertools.tee(item, 2) else: target = item if getattr(target, "__iter__", None): for el in target: if isinstance(el, Iterable) and not isinstance(el, six.string_types): el, el_copy = itertools.tee(el, 2) for sub in unnest(el_copy): yield sub else: yield el else: yield target
Example #7
Source File: timeseries.py From traces with MIT License | 6 votes |
def iterintervals(self, n=2): """Iterate over groups of `n` consecutive measurement points in the time series. """ # tee the original iterator into n identical iterators streams = itertools.tee(iter(self), n) # advance the "cursor" on each iterator by an increasing # offset, e.g. if n=3: # # [a, b, c, d, e, f, ..., w, x, y, z] # first cursor --> * # second cursor --> * # third cursor --> * for stream_index, stream in enumerate(streams): for _ in range(stream_index): next(stream) # now, zip the offset streams back together to yield tuples, # in the n=3 example it would yield: # (a, b, c), (b, c, d), ..., (w, x, y), (x, y, z) for intervals in zip(*streams): yield intervals
Example #8
Source File: paginate.py From faces with GNU General Public License v2.0 | 5 votes |
def result_key_iters(self): teed_results = tee(self, len(self.result_keys)) return [ResultKeyIterator(i, result_key) for i, result_key in zip(teed_results, self.result_keys)]
Example #9
Source File: evap_aral.py From hydrology with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2,s3), ...""" a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b) #function to create stage volume output
Example #10
Source File: evaluation.py From few with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = it.tee(iterable) next(b, None) return zip(a, b)
Example #11
Source File: model_convert_utils.py From KL-Loss with Apache License 2.0 | 5 votes |
def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." from itertools import tee a, b = tee(iterable) next(b, None) return zip(a, b)
Example #12
Source File: misc.py From Comparative-Annotation-Toolkit with Apache License 2.0 | 5 votes |
def pairwise_adjacent(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = itertools.tee(iterable) next(b, None) return zip(a, b)
Example #13
Source File: ch_616_daily_wb.py From hydrology with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2,s3), ...""" a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b)
Example #14
Source File: heapq.py From oss-ftp with MIT License | 5 votes |
def nlargest(n, iterable, key=None): """Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n] """ # Short-cut for n==1 is to use max() when len(iterable)>0 if n == 1: it = iter(iterable) head = list(islice(it, 1)) if not head: return [] if key is None: return [max(chain(head, it))] return [max(chain(head, it), key=key)] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key, reverse=True)[:n] # When key is none, use simpler decoration if key is None: it = izip(iterable, count(0,-1)) # decorate result = _nlargest(n, it) return map(itemgetter(0), result) # undecorate # General case, slowest method in1, in2 = tee(iterable) it = izip(imap(key, in1), count(0,-1), in2) # decorate result = _nlargest(n, it) return map(itemgetter(2), result) # undecorate
Example #15
Source File: profile_creator.py From hydrology with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2,s3), ...""" a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b)
Example #16
Source File: area_function.py From hydrology with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2,s3), ...""" a, b = itertools.tee(iterable) next(b, None) return itertools.izip(a, b) #Enter the check dam height
Example #17
Source File: stage_volume_tutorial.py From hydrology with GNU General Public License v3.0 | 5 votes |
def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2,s3), ..." a, b = itertools.tee(iterable) next(b,None) return itertools.izip(a, b) #Enter the check dam height
Example #18
Source File: generators_pythonic_3.py From Clean-code-in-Python with MIT License | 5 votes |
def process_purchases(purchases): min_, max_, avg = tee(purchases, 3) return min(min_), max(max_), median(avg)
Example #19
Source File: paginate.py From faces with GNU General Public License v2.0 | 5 votes |
def result_key_iters(self): teed_results = tee(self, len(self.result_keys)) return [ResultKeyIterator(i, result_key) for i, result_key in zip(teed_results, self.result_keys)]
Example #20
Source File: rc2.py From pysat with MIT License | 5 votes |
def get_core(self): """ Extract unsatisfiable core. The result of the procedure is stored in variable ``self.core``. If necessary, core trimming and also heuristic core reduction is applied depending on the command-line options. A *minimum weight* of the core is computed and stored in ``self.minw``. Finally, the core is divided into two parts: 1. clause selectors (``self.core_sels``), 2. sum assumptions (``self.core_sums``). """ # extracting the core self.core = self.oracle.get_core() if self.core: # try to reduce the core by trimming self.trim_core() # and by heuristic minimization self.minimize_core() # the core may be empty after core minimization if not self.core: return # core weight self.minw = min(map(lambda l: self.wght[l], self.core)) # dividing the core into two parts iter1, iter2 = itertools.tee(self.core) self.core_sels = list(l for l in iter1 if l in self.sels_set) self.core_sums = list(l for l in iter2 if l not in self.sels_set)
Example #21
Source File: scipy2015_cbnx_demo_code.py From Causal-Bayesian-NetworkX with BSD 3-Clause "New" or "Revised" License | 5 votes |
def new_conditional_graph_set(graph_set,condition_list): """ This returns a copy of the old graph_set and a new graph generator which has the conditions in condition_list applied to it. Warning: This function will devour the iterator that you include as the graph_set input, you need to redeclare the variable as one of the return values of the function. Thus a correct use would be: a,b = new_conditional_graph_set(a,c) The following would not be a correct use: x,y = new_conditional_graph_set(a,c) Variables: graph_set is a graph-set generator condition_list is a list of first order functions returning boolean values when passed a graph. """ try: condition_list[0] except TypeError: raise TypeError(""" Subsampling from a graph requires passing in a list of conditions encoded as first-class functions that accept networkX graphs as an input and return boolean values.""") graph_set_newer, graph_set_test = tee(graph_set,2) def gen(): for G in graph_set_test: G_test = G.copy() if all([c(G_test) for c in condition_list]): yield G_test return graph_set_newer, gen()
Example #22
Source File: utilities.py From tensorprob with MIT License | 5 votes |
def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = itertools.tee(iterable) next(b, None) return zip(a, b)
Example #23
Source File: test_control_flow.py From equip with Apache License 2.0 | 5 votes |
def get_pairs(iterable): a, b = tee(iterable) next(b, None) return izip(a, b)
Example #24
Source File: flow.py From equip with Apache License 2.0 | 5 votes |
def get_pairs(iterable): a, b = tee(iterable) next(b, None) return izip(a, b)
Example #25
Source File: integration.py From btcpy with GNU Lesser General Public License v3.0 | 5 votes |
def pairwise(iterable): from itertools import tee a, b = tee(iterable) next(b, None) return list(zip(a, b))
Example #26
Source File: tools.py From nevergrad with MIT License | 5 votes |
def pairwise(iterable: tp.Iterable[tp.Any]) -> tp.Iterator[tp.Tuple[tp.Any, tp.Any]]: """Returns an iterator over sliding pairs of the input iterator s -> (s0,s1), (s1,s2), (s2, s3), ... Note ---- Nothing will be returned if length of iterator is strictly less than 2. """ # From itertools documentation a, b = itertools.tee(iterable) next(b, None) return zip(a, b)
Example #27
Source File: heapq.py From ironpython2 with Apache License 2.0 | 5 votes |
def nlargest(n, iterable, key=None): """Find the n largest elements in a dataset. Equivalent to: sorted(iterable, key=key, reverse=True)[:n] """ # Short-cut for n==1 is to use max() when len(iterable)>0 if n == 1: it = iter(iterable) head = list(islice(it, 1)) if not head: return [] if key is None: return [max(chain(head, it))] return [max(chain(head, it), key=key)] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key, reverse=True)[:n] # When key is none, use simpler decoration if key is None: it = izip(iterable, count(0,-1)) # decorate result = _nlargest(n, it) return map(itemgetter(0), result) # undecorate # General case, slowest method in1, in2 = tee(iterable) it = izip(imap(key, in1), count(0,-1), in2) # decorate result = _nlargest(n, it) return map(itemgetter(2), result) # undecorate
Example #28
Source File: heapq.py From ironpython2 with Apache License 2.0 | 5 votes |
def nsmallest(n, iterable, key=None): """Find the n smallest elements in a dataset. Equivalent to: sorted(iterable, key=key)[:n] """ # Short-cut for n==1 is to use min() when len(iterable)>0 if n == 1: it = iter(iterable) head = list(islice(it, 1)) if not head: return [] if key is None: return [min(chain(head, it))] return [min(chain(head, it), key=key)] # When n>=size, it's faster to use sorted() try: size = len(iterable) except (TypeError, AttributeError): pass else: if n >= size: return sorted(iterable, key=key)[:n] # When key is none, use simpler decoration if key is None: it = izip(iterable, count()) # decorate result = _nsmallest(n, it) return map(itemgetter(0), result) # undecorate # General case, slowest method in1, in2 = tee(iterable) it = izip(imap(key, in1), count(), in2) # decorate result = _nsmallest(n, it) return map(itemgetter(2), result) # undecorate
Example #29
Source File: sqlalchemy.py From nplusone with MIT License | 5 votes |
def query_iter(self): ret, clone = itertools.tee(original_query_iter(self)) signal = ( signals.ignore_load if is_single(self._offset, self._limit) else signals.load ) signal.send( signals.get_worker(), args=(self, ), ret=list(clone), parser=parse_load, ) return ret
Example #30
Source File: generators_pythonic_3.py From Clean-Code-in-Python with MIT License | 5 votes |
def process_purchases(purchases): min_, max_, avg = tee(purchases, 3) return min(min_), max(max_), median(avg)