Python itertools.tee() Examples

The following are code examples for showing how to use itertools.tee(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: DataFlow   Author: LordFeratum   File: utils.py    MIT License 6 votes vote down vote up
def async_dropwhile(fnx, data):
    data, data_copy = tee(data)
    idx = 0
    for datum in data_copy:
        if not await fnx(datum):
            break
        idx += 1

    res = []
    for _idx, datum in enumerate(data):
        if _idx <= idx:
            continue

        res.append(datum)

    return res 
Example 2
Project: WhooshSearch   Author: rokartnaz   File: filters.py    BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __call__(self, tokens):
        from itertools import tee

        count = len(self.filters)
        # Tee the token iterator and wrap each teed iterator with the
        # corresponding filter
        gens = [filter(t.copy() for t in gen) for filter, gen
                in zip(self.filters, tee(tokens, count))]
        # Keep a count of the number of running iterators
        running = count
        while running:
            for i, gen in enumerate(gens):
                if gen is not None:
                    try:
                        yield next(gen)
                    except StopIteration:
                        gens[i] = None
                        running -= 1 
Example 3
Project: coala-quickstart   Author: coala   File: UtilitiesTest.py    GNU Affero General Public License v3.0 6 votes vote down vote up
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 4
Project: Hierarchical-Sentiment   Author: cedias   File: prepare_data.py    MIT License 6 votes vote down vote up
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
Project: traces   Author: datascopeanalytics   File: timeseries.py    MIT License 6 votes vote down vote up
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 = 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 6
Project: pyblish-win   Author: pyblish   File: heapq.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
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 7
Project: pyblish-win   Author: pyblish   File: heapq.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
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 8
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: formparser.py    Apache License 2.0 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 9
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: formparser.py    Apache License 2.0 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 10
Project: pinax-documents   Author: pinax   File: models.py    MIT License 5 votes vote down vote up
def shared_parent(self):
        """
        Returns the folder object that is the shared parent (the root of
        a shared folder hierarchy) or None if there is no shared parent.
        """
        root = self
        a, b = itertools.tee(reversed(self.breadcrumbs()))
        next(b, None)
        for folder, parent in izip_longest(a, b):
            if folder.shared:
                root = folder
            if parent is None or not parent.shared:
                break
        return root 
Example 11
Project: flasky   Author: RoseOu   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 12
Project: AshsSDK   Author: thehappydinoa   File: paginate.py    MIT License 5 votes vote down vote up
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 13
Project: CIMtools   Author: stsouko   File: base.py    GNU General Public License v3.0 5 votes vote down vote up
def reaction_support(_class):
    class ReactionSupport(_class):
        def transform(self, x):
            if not all(isinstance(s, ReactionContainer) for s in x):
                raise TypeError('invalid dtype, only ReactionContainers acceptable')

            shifts = {}
            mols = []
            for i in ('reactants', 'products'):
                sh = shifts[i] = [len(mols)]
                for s in x:
                    si = s[i]
                    sh.append(len(si) + sh[-1])
                    mols.extend(si)

            transformed = super().transform(mols)
            if len(transformed) != len(mols):
                raise ValueError('unexpected transformed molecules amount')

            return iter2array(ReactionContainer(r, p, meta=s.meta) for s, r, p in
                              zip(x, (transformed[y: z] for y, z in self.__pairwise(shifts['reactants'])),
                                     (transformed[y: z] for y, z in self.__pairwise(shifts['products']))))

        @staticmethod
        def __pairwise(iterable):
            """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
            a, b = tee(iterable)
            next(b, None)
            return zip(a, b)

    return ReactionSupport 
Example 14
Project: where   Author: kartverket   File: _parser_chain.py    MIT License 5 votes vote down vote up
def read_data(self):
        """Read data from a data file and parse the contents
        """
        # Get chain of parsers
        parsers_chain = iter(self.setup_parser())
        parser = next(parsers_chain)  # Pointing to first parser
        cache = dict(line_num=0)

        with files.open_path(self.file_path, mode="rt", encoding=self.file_encoding) as fid:
            # Get iterators for current and next line
            line_iter, next_line_iter = itertools.tee(fid)
            next(next_line_iter, None)

            # Iterate over all file lines including last line by using zip_longest
            for line, next_line in itertools.zip_longest(line_iter, next_line_iter):
                cache["line_num"] += 1
                self.parse_line(line.rstrip(), cache, parser)

                # Skip to next parser
                if next_line is None or parser.end_marker(line.rstrip(), cache["line_num"], next_line):
                    if parser.end_callback is not None:
                        parser.end_callback(cache)
                    cache = dict(line_num=0)
                    try:
                        parser = next(parsers_chain)
                    except StopIteration:
                        break 
Example 15
Project: where   Author: kartverket   File: parser.py    MIT License 5 votes vote down vote up
def parse_file(self, fid):
        """Read a data file and parse the contents
        """
        # Get "list" of parsers
        parsers_iter = iter(self.setup_parsers())

        parser = next(parsers_iter)  # Pointing to first parser
        cache = dict(line_num=0)

        # Get iterators for current and next line
        line_iter, next_line_iter = itertools.tee(fid)
        next(next_line_iter, None)

        # Iterate over all file lines including last line by using zip_longest
        for line, next_line in itertools.zip_longest(line_iter, next_line_iter):
            cache["line_num"] += 1
            self.parse_line(line.rstrip(), cache, parser)

            # Skip to next parser
            if next_line is None or parser["end_marker"](line.rstrip(), cache["line_num"], next_line):
                if "end_callback" in parser:
                    parser["end_callback"](cache)
                cache = dict(line_num=0)
                try:
                    parser = next(parsers_iter)
                except StopIteration:
                    break 
Example 16
Project: jawfish   Author: war-and-code   File: heapq.py    MIT License 5 votes vote down vote up
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 = zip(iterable, count())                         # decorate
        result = _nsmallest(n, it)
        return [r[0] for r in result]                       # undecorate

    # General case, slowest method
    in1, in2 = tee(iterable)
    it = zip(map(key, in1), count(), in2)                   # decorate
    result = _nsmallest(n, it)
    return [r[2] for r in result]                           # undecorate 
Example 17
Project: jawfish   Author: war-and-code   File: heapq.py    MIT License 5 votes vote down vote up
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 = zip(iterable, count(0,-1))                     # decorate
        result = _nlargest(n, it)
        return [r[0] for r in result]                       # undecorate

    # General case, slowest method
    in1, in2 = tee(iterable)
    it = zip(map(key, in1), count(0,-1), in2)               # decorate
    result = _nlargest(n, it)
    return [r[2] for r in result]                           # undecorate 
Example 18
Project: edit-bone-constraint   Author: HolisticCoders   File: misc.py    GNU General Public License v3.0 5 votes vote down vote up
def pairwise(iterable, cyclic=False):
    "s -> (s0, s1), (s1, s2), (s2, s3), ..."
    a, b = tee(iterable)
    first = next(b, None)
    if cyclic is True:
        return zip(a, chain(b, (first,)))
    return zip(a, b) 
Example 19
Project: lineflow   Author: tofunlp   File: core.py    MIT License 5 votes vote down vote up
def __iter__(self) -> Iterator[Any]:
        if self._ready:
            yield from self._dataset
        else:
            iterable, self._iterable = tee(self._iterable)
            yield from iterable 
Example 20
Project: clacker   Author: wez   File: utils.py    GNU General Public License v2.0 5 votes vote down vote up
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b) 
Example 21
Project: jaraco.itertools   Author: jaraco   File: itertools.py    MIT License 5 votes vote down vote up
def reset(self):
        """
        Resets the iterator to the start.

        Any remaining values in the current iteration are discarded.
        """
        self.__iterator, self.__saved = itertools.tee(self.__saved) 
Example 22
Project: jaraco.itertools   Author: jaraco   File: itertools.py    MIT License 5 votes vote down vote up
def peek(iterable):
    """
    Get the next value from an iterable, but also return an iterable
    that will subsequently return that value and the rest of the
    original iterable.

    >>> l = iter([1,2,3])
    >>> val, l = peek(l)
    >>> val
    1
    >>> list(l)
    [1, 2, 3]
    """
    peeker, original = itertools.tee(iterable)
    return next(peeker), original 
Example 23
Project: jaraco.itertools   Author: jaraco   File: itertools.py    MIT License 5 votes vote down vote up
def nwise(iter, n):
    """
    Like pairwise, except returns n-tuples of adjacent items.
    s -> (s0,s1,...,sn), (s1,s2,...,s(n+1)), ...
    """
    iterset = [iter]
    while len(iterset) < n:
        iterset[-1:] = itertools.tee(iterset[-1])
        next(iterset[-1], None)
    return zip(*iterset) 
Example 24
Project: jaraco.itertools   Author: jaraco   File: itertools.py    MIT License 5 votes vote down vote up
def window(iter, pre_size=1, post_size=1):
    """
    Given an iterable, return a new iterable which yields triples of
    (pre, item, post), where pre and post are the items preceeding and
    following the item (or None if no such item is appropriate). pre
    and post will always be pre_size and post_size in length.

    >>> example = window(range(10), pre_size=2)
    >>> pre, item, post = next(example)
    >>> pre
    (None, None)
    >>> post
    (1,)
    >>> next(example)
    ((None, 0), 1, (2,))
    >>> list(example)[-1]
    ((7, 8), 9, (None,))
    """
    pre_iter, iter = itertools.tee(iter)
    pre_iter = itertools.chain((None,) * pre_size, pre_iter)
    pre_iter = nwise(pre_iter, pre_size)
    post_iter, iter = itertools.tee(iter)
    post_iter = itertools.chain(post_iter, (None,) * post_size)
    post_iter = nwise(post_iter, post_size)
    next(post_iter, None)
    return zip(pre_iter, iter, post_iter) 
Example 25
Project: jaraco.itertools   Author: jaraco   File: itertools.py    MIT License 5 votes vote down vote up
def balanced_rows(n, iterable, fillvalue=None):
    """
    Like grouper, but balance the rows to minimize fill per row.
    balanced_rows(3, 'ABCDEFG', 'x') --> ABC DEx FGx"
    """
    iterable, iterable_copy = itertools.tee(iterable)
    count = len(tuple(iterable_copy))
    for allocation in partition_items(count, n):
        row = itertools.islice(iterable, allocation)
        if allocation < n:
            row = itertools.chain(row, [fillvalue])
        yield tuple(row) 
Example 26
Project: python-podman   Author: containers   File: test_images.py    Apache License 2.0 5 votes vote down vote up
def test_search(self):
        actual = self.pclient.images.search('alpine', 25)
        names, length = itertools.tee(actual)

        for img in names:
            self.assertIn('alpine', img.name)
        self.assertTrue(0 < len(list(length)) <= 25) 
Example 27
Project: chattR   Author: patrickstocklin   File: __init__.py    GNU General Public License v2.0 5 votes vote down vote up
def _get_choices(self):
        if isinstance(self._choices, collections.Iterator):
            choices, self._choices = tee(self._choices)
            return choices
        else:
            return self._choices 
Example 28
Project: transformpy   Author: airbnb   File: base.py    MIT License 5 votes vote down vote up
def tee(self, tee, *args, **kwargs):
        if not isinstance(tee, TeePipe):
            tee = TeePipe(tee, *args, **kwargs)
        return self.__ins_add(self._pipeline, tee, TransformType.TEE, args, kwargs) 
Example 29
Project: transformpy   Author: airbnb   File: base.py    MIT License 5 votes vote down vote up
def __check_pipeline(self):
        '''
        Ensure that pipeline runnable and behaves as expected. Currently checks
        that:
            - Clustering operations are followed by a nested transform or an
              aggregation. If not, an explicity generator -> list mapping operation
              is added to avoid subsequent operations being confused by the
              generator of generators that clustering operations yield.
            - If the last transformation is a clustering operation, that the
              same explicity mapping is done to ensure sensible output from the
              transform pipe.
        '''

        def pairwise(iterable):
            "s -> (s0,s1), (s1,s2), (s2, s3), ..."
            a, b = tee(iterable)
            next(b, None)
            return zip(a, b)

        pipeline = self._pipeline[:1]

        for a, b in pairwise(self._pipeline):
            if a.type == TransformType.CLUSTER and b.type not in (TransformType.NESTED, TransformType.AGGREGATE):
                logging.warning("A non-nested non-aggregation transform has been used after a clustering pipe. Adding a potentially memory inefficient pipe segment to convert cluster generator to list.")
                pipeline.append(self.__ins(lambda x: list(x), TransformType.MAP, [], {}))
            pipeline.append(b)

        if pipeline[-1].type == TransformType.CLUSTER:
            pipeline.append(self.__ins(lambda x: list(x), TransformType.MAP, [], {}))

        return pipeline 
Example 30
Project: link-prediction_with_deep-learning   Author: cambridgeltl   File: wvlib.py    MIT License 5 votes vote down vote up
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

# transated from http://www.hackersdelight.org/hdcodetxt/snoob.c.txt 
Example 31
Project: tomlkit   Author: sdispater   File: source.py    MIT License 5 votes vote down vote up
def __enter__(self):  # type: () -> None
        # Entering this context manager - save the state
        if PY2:
            # Python 2.7 does not allow to directly copy
            # an iterator, so we have to make tees of the original
            # chars iterator.
            self._source._chars, self._chars = itertools.tee(self._source._chars)
        else:
            self._chars = copy(self._source._chars)
        self._idx = self._source._idx
        self._current = self._source._current
        self._marker = self._source._marker

        return self 
Example 32
Project: Flask_Blog   Author: sugarguo   File: formparser.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 33
Project: NiujiaoDebugger   Author: MrSrc   File: datetimetester.py    GNU General Public License v3.0 5 votes vote down vote up
def pairs(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b) 
Example 34
Project: Health-Checker   Author: KriAga   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 35
Project: Health-Checker   Author: KriAga   File: util.py    MIT License 5 votes vote down vote up
def pairwise(iterable):
    """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

#////////////////////////////////////////////////////////////
#{ Feature extractor functions
#//////////////////////////////////////////////////////////// 
Example 36
Project: platzi-hello-gae   Author: xertica-cloud   File: formparser.py    GNU General Public License v2.0 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 37
Project: panwid   Author: tonycpsu   File: common.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = itertools.tee(iterable)
    return itertools.filterfalse(pred, t1), filter(pred, t2) 
Example 38
Project: recruit   Author: Frank-qlu   File: formparser.py    Apache License 2.0 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2
        )
        form = (p[1] for p in formstream if p[0] == "form")
        files = (p[1] for p in filestream if p[0] == "file")
        return self.cls(form), self.cls(files) 
Example 39
Project: cryptopal   Author: lanjelot   File: cryptopal.py    GNU General Public License v3.0 5 votes vote down vote up
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b) 
Example 40
Project: pyglottolog   Author: glottolog   File: test_hhtypes.py    Apache License 2.0 5 votes vote down vote up
def pairwise(iterable):
    a, b = itertools.tee(iterable)
    next(b, None)
    return zip(a, b) 
Example 41
Project: tesismometro   Author: joapaspe   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 42
Project: pdvega   Author: altair-viz   File: utils.py    MIT License 5 votes vote down vote up
def prev_this_next(it, sentinel=None):
    """Utility to return (prev, this, next) tuples from an iterator"""
    i1, i2, i3 = tee(it, 3)
    next(i3, None)
    return zip(chain([sentinel], i1), i2, chain(i3, [sentinel])) 
Example 43
Project: autoslot   Author: cjrh   File: autoslot.py    Apache License 2.0 5 votes vote down vote up
def assignments_to_self(method) -> set:
    """Given a method, collect all the attribute names for assignments
    to "self"."""
    # Get the name of the var used to refer to the instance. Usually,
    # this will be "self". It's the first parameter to the
    # __init__(self, ...)  method call. If there are no parameters,
    # just pretend it was "self".
    instance_var = next(iter(method.__code__.co_varnames), 'self')
    # We need to scan all the bytecode instructions to see all the times
    # an attribute of "self" got assigned-to. First get the list of
    # instructions.
    instructions = dis.Bytecode(method)
    # Assignments to attributes of "self" are identified by a first
    # LOAD_FAST (with a value of "self") immediately followed by a
    # STORE_ATTR (with a value of the attribute name). So we will need
    # to look at a sequence of pairs through the bytecode. The easiest
    # way to do this is with two iterators.
    i0, i1 = tee(instructions)
    # March the second one ahead by one step.
    next(i1, None)
    names = set()
    # a and b are a pair of bytecode instructions; b follows a.
    for a, b in zip(i0, i1):
        accessing_self = (a.argval == instance_var and a.opname == 'LOAD_FAST')
        storing_attribute = (b.opname == 'STORE_ATTR')
        if accessing_self and storing_attribute:
            names.add(b.argval)
    return names 
Example 44
Project: django-echarts   Author: kinegratii   File: fetch.py    MIT License 5 votes vote down vote up
def ifetch_multiple(iterable, *keys, defaults=None, getter=None):
    defaults = defaults or {}
    if len(keys) > 1:
        iters = tee(iterable, len(keys))
    else:
        iters = (iterable,)
    iters = [ifetch_single(it, key, default=defaults.get(key, EMPTY), getter=getter) for it, key in zip(iters, keys)]
    return iters 
Example 45
Project: aws-cfn-plex   Author: lordmuffin   File: paginate.py    MIT License 5 votes vote down vote up
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 46
Project: musaic   Author: al165   File: DataGeneratorsLead.py    GNU General Public License v3.0 5 votes vote down vote up
def generate_data(self, rhythm_context_size=1, melody_context_size=1, 
                      random_stream=None, with_metaData=True):
        
        if not random_stream:
            random_stream = self.random_stream()

        r1, r2 = tee(random_stream, 2)

        rhythm_iter = self.rhythm_gen.generate_data(rhythm_context_size,
                                                    rand_stream=r1,
                                                    with_rhythms=True,
                                                    with_metaData=with_metaData)
        melody_iter = self.melody_gen.generate_data(melody_context_size,
                                                    rand_stream=r2,
                                                    with_metaData=False)

        if with_metaData:
            for (cur_rhythm, cur_melody) in zip(rhythm_iter, melody_iter):
                (*rhythm_x, rhythms, meta, rhythm_lead), rhythm_y = cur_rhythm
                (melody_x, melody_lead), melody_y = cur_melody
                yield ([*rhythm_x, rhythms, melody_x, meta, rhythm_lead, melody_lead],
                       [rhythm_y, melody_y])
        else:
            for (cur_rhythm, cur_melody) in zip(rhythm_iter, melody_iter):
                (*rhythm_x, rhythms, rhythm_lead), rhythm_y = cur_rhythm
                (melody_x, melody_lead), melody_y = cur_melody
            yield ([*rhythm_x, rhythms, melody_x, rhythm_lead, melody_lead],
                   [rhythm_y, melody_y]) 
Example 47
Project: musaic   Author: al165   File: DataGeneratorsLeadMeta.py    GNU General Public License v3.0 5 votes vote down vote up
def generate_data(self, rhythm_context_size=1, melody_context_size=1, 
                      random_stream=None, with_metaData=True):
        
        if not random_stream:
            random_stream = self.random_stream()

        r1, r2 = tee(random_stream, 2)

        rhythm_iter = self.rhythm_gen.generate_data(rhythm_context_size,
                                                    rand_stream=r1,
                                                    with_rhythms=True,
                                                    with_metaData=with_metaData)
        melody_iter = self.melody_gen.generate_data(melody_context_size,
                                                    rand_stream=r2,
                                                    with_metaData=False)

        if with_metaData:
            for (cur_rhythm, cur_melody) in zip(rhythm_iter, melody_iter):
                (*rhythm_x, rhythms, meta, prev_meta, rhythm_lead), rhythm_y = cur_rhythm
                (melody_x, melody_lead), melody_y = cur_melody
                melody_lead = melody_lead.reshape((-1, 1, 48))
                yield ([*rhythm_x, rhythms, melody_x, meta, prev_meta, rhythm_lead, melody_lead],
                       [rhythm_y, melody_y, meta])
        else:
            for (cur_rhythm, cur_melody) in zip(rhythm_iter, melody_iter):
                (*rhythm_x, rhythms, rhythm_lead), rhythm_y = cur_rhythm
                (melody_x, melody_lead), melody_y = cur_melody
            yield ([*rhythm_x, rhythms, melody_x, rhythm_lead, melody_lead],
                   [rhythm_y, melody_y]) 
Example 48
Project: jbox   Author: jpush   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 49
Project: neo4j-social-network   Author: bestvibes   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files) 
Example 50
Project: neo4j-social-network   Author: bestvibes   File: formparser.py    MIT License 5 votes vote down vote up
def parse(self, file, boundary, content_length):
        formstream, filestream = tee(
            self.parse_parts(file, boundary, content_length), 2)
        form = (p[1] for p in formstream if p[0] == 'form')
        files = (p[1] for p in filestream if p[0] == 'file')
        return self.cls(form), self.cls(files)