Python more_itertools.peekable() Examples

The following are 30 code examples of more_itertools.peekable(). 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 more_itertools , or try the search function .
Example #1
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend_indexing(self):
        """Tests interaction between prepending and indexing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)

        self.assertEqual(p[0], 30)
        self.assertEqual(next(p), 30)
        self.assertEqual(p[2], 0)
        self.assertEqual(next(p), 40)
        self.assertEqual(p[0], 50)
        self.assertEqual(p[9], 8)
        self.assertEqual(next(p), 50)
        self.assertEqual(p[8], 8)
        self.assertEqual(p[-2], 18)
        self.assertEqual(p[-9], 11)
        self.assertRaises(IndexError, lambda: p[-21]) 
Example #2
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_prepend_slicing(self):
        """Tests interaction between prepending and slicing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)
        pseq = [30, 40, 50] + seq  # pseq for prepended_seq

        # adapt the specific tests from test_slicing
        self.assertEqual(p[0], 30)
        self.assertEqual(p[1:8], pseq[1:8])
        self.assertEqual(p[1:], pseq[1:])
        self.assertEqual(p[:5], pseq[:5])
        self.assertEqual(p[:], pseq[:])
        self.assertEqual(p[:100], pseq[:100])
        self.assertEqual(p[::2], pseq[::2])
        self.assertEqual(p[::-1], pseq[::-1]) 
Example #3
Source File: timeline.py    From byro with Apache License 2.0 6 votes vote down vote up
def add_dummy_entries(entries):
    entries = peekable(entries)
    prev_entry = next(entries)
    yield prev_entry
    output_month, output_year = prev_entry["date"].month, prev_entry["date"].year

    while entries:
        entry = next(entries)
        while output_month != entry["date"].month or output_year != entry["date"].year:
            output_month = output_month - 1
            if output_month < 1:
                output_month = 12
                output_year = output_year - 1
            if output_month != entry["date"].month or output_year != entry["date"].year:
                yield {
                    "type": "dummy",
                    "subtype": "dummy",
                    "instance": None,
                    "date": datetime(year=output_year, month=output_month, day=1),
                }
        yield entry 
Example #4
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_prepend(self):
        """Tests intersperesed ``prepend()`` and ``next()`` calls"""
        it = mi.peekable(range(2))
        actual = []

        # Test prepend() before next()
        it.prepend(10)
        actual += [next(it), next(it)]

        # Test prepend() between next()s
        it.prepend(11)
        actual += [next(it), next(it)]

        # Test prepend() after source iterable is consumed
        it.prepend(12)
        actual += [next(it)]

        expected = [10, 0, 11, 1, 12]
        self.assertEqual(actual, expected) 
Example #5
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend(self):
        """Tests intersperesed ``prepend()`` and ``next()`` calls"""
        it = mi.peekable(range(2))
        actual = []

        # Test prepend() before next()
        it.prepend(10)
        actual += [next(it), next(it)]

        # Test prepend() between next()s
        it.prepend(11)
        actual += [next(it), next(it)]

        # Test prepend() after source iterable is consumed
        it.prepend(12)
        actual += [next(it)]

        expected = [10, 0, 11, 1, 12]
        self.assertEqual(actual, expected) 
Example #6
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend_slicing(self):
        """Tests interaction between prepending and slicing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)
        pseq = [30, 40, 50] + seq  # pseq for prepended_seq

        # adapt the specific tests from test_slicing
        self.assertEqual(p[0], 30)
        self.assertEqual(p[1:8], pseq[1:8])
        self.assertEqual(p[1:], pseq[1:])
        self.assertEqual(p[:5], pseq[:5])
        self.assertEqual(p[:], pseq[:])
        self.assertEqual(p[:100], pseq[:100])
        self.assertEqual(p[::2], pseq[::2])
        self.assertEqual(p[::-1], pseq[::-1]) 
Example #7
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_slicing(self):
        """Slicing the peekable shouldn't advance the iterator."""
        seq = list('abcdefghijkl')
        p = mi.peekable(seq)

        # Slicing the peekable should just be like slicing a re-iterable
        self.assertEqual(p[1:4], seq[1:4])

        # Advancing the iterator moves the slices up also
        self.assertEqual(next(p), 'a')
        self.assertEqual(p[1:4], seq[1:][1:4])

        # Implicit starts and stop should work
        self.assertEqual(p[:5], seq[1:][:5])
        self.assertEqual(p[:], seq[1:][:])

        # Indexing past the end should work
        self.assertEqual(p[:100], seq[1:][:100])

        # Steps should work, including negative
        self.assertEqual(p[::2], seq[1:][::2])
        self.assertEqual(p[::-1], seq[1:][::-1]) 
Example #8
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_indexing(self):
        """
        Indexing into the peekable shouldn't advance the iterator.
        """
        p = mi.peekable('abcdefghijkl')

        # The 0th index is what ``next()`` will return
        self.assertEqual(p[0], 'a')
        self.assertEqual(next(p), 'a')

        # Indexing further into the peekable shouldn't advance the itertor
        self.assertEqual(p[2], 'd')
        self.assertEqual(next(p), 'b')

        # The 0th index moves up with the iterator; the last index follows
        self.assertEqual(p[0], 'c')
        self.assertEqual(p[9], 'l')

        self.assertEqual(next(p), 'c')
        self.assertEqual(p[8], 'l')

        # Negative indexing should work too
        self.assertEqual(p[-2], 'k')
        self.assertEqual(p[-9], 'd')
        self.assertRaises(IndexError, lambda: p[-10]) 
Example #9
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_prepend_indexing(self):
        """Tests interaction between prepending and indexing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)

        self.assertEqual(p[0], 30)
        self.assertEqual(next(p), 30)
        self.assertEqual(p[2], 0)
        self.assertEqual(next(p), 40)
        self.assertEqual(p[0], 50)
        self.assertEqual(p[9], 8)
        self.assertEqual(next(p), 50)
        self.assertEqual(p[8], 8)
        self.assertEqual(p[-2], 18)
        self.assertEqual(p[-9], 11)
        self.assertRaises(IndexError, lambda: p[-21]) 
Example #10
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_prepend_slicing(self):
        """Tests interaction between prepending and slicing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)
        pseq = [30, 40, 50] + seq  # pseq for prepended_seq

        # adapt the specific tests from test_slicing
        self.assertEqual(p[0], 30)
        self.assertEqual(p[1:8], pseq[1:8])
        self.assertEqual(p[1:], pseq[1:])
        self.assertEqual(p[:5], pseq[:5])
        self.assertEqual(p[:], pseq[:])
        self.assertEqual(p[:100], pseq[:100])
        self.assertEqual(p[::2], pseq[::2])
        self.assertEqual(p[::-1], pseq[::-1]) 
Example #11
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_prepend_indexing(self):
        """Tests interaction between prepending and indexing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)

        self.assertEqual(p[0], 30)
        self.assertEqual(next(p), 30)
        self.assertEqual(p[2], 0)
        self.assertEqual(next(p), 40)
        self.assertEqual(p[0], 50)
        self.assertEqual(p[9], 8)
        self.assertEqual(next(p), 50)
        self.assertEqual(p[8], 8)
        self.assertEqual(p[-2], 18)
        self.assertEqual(p[-9], 11)
        self.assertRaises(IndexError, lambda: p[-21]) 
Example #12
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend_indexing(self):
        """Tests interaction between prepending and indexing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)

        self.assertEqual(p[0], 30)
        self.assertEqual(next(p), 30)
        self.assertEqual(p[2], 0)
        self.assertEqual(next(p), 40)
        self.assertEqual(p[0], 50)
        self.assertEqual(p[9], 8)
        self.assertEqual(next(p), 50)
        self.assertEqual(p[8], 8)
        self.assertEqual(p[-2], 18)
        self.assertEqual(p[-9], 11)
        self.assertRaises(IndexError, lambda: p[-21]) 
Example #13
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_indexing(self):
        """
        Indexing into the peekable shouldn't advance the iterator.
        """
        p = mi.peekable('abcdefghijkl')

        # The 0th index is what ``next()`` will return
        self.assertEqual(p[0], 'a')
        self.assertEqual(next(p), 'a')

        # Indexing further into the peekable shouldn't advance the itertor
        self.assertEqual(p[2], 'd')
        self.assertEqual(next(p), 'b')

        # The 0th index moves up with the iterator; the last index follows
        self.assertEqual(p[0], 'c')
        self.assertEqual(p[9], 'l')

        self.assertEqual(next(p), 'c')
        self.assertEqual(p[8], 'l')

        # Negative indexing should work too
        self.assertEqual(p[-2], 'k')
        self.assertEqual(p[-9], 'd')
        self.assertRaises(IndexError, lambda: p[-10]) 
Example #14
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_prepend(self):
        """Tests intersperesed ``prepend()`` and ``next()`` calls"""
        it = mi.peekable(range(2))
        actual = []

        # Test prepend() before next()
        it.prepend(10)
        actual += [next(it), next(it)]

        # Test prepend() between next()s
        it.prepend(11)
        actual += [next(it), next(it)]

        # Test prepend() after source iterable is consumed
        it.prepend(12)
        actual += [next(it)]

        expected = [10, 0, 11, 1, 12]
        self.assertEqual(actual, expected) 
Example #15
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend_slicing(self):
        """Tests interaction between prepending and slicing"""
        seq = list(range(20))
        p = mi.peekable(seq)

        p.prepend(30, 40, 50)
        pseq = [30, 40, 50] + seq  # pseq for prepended_seq

        # adapt the specific tests from test_slicing
        self.assertEqual(p[0], 30)
        self.assertEqual(p[1:8], pseq[1:8])
        self.assertEqual(p[1:], pseq[1:])
        self.assertEqual(p[:5], pseq[:5])
        self.assertEqual(p[:], pseq[:])
        self.assertEqual(p[:100], pseq[:100])
        self.assertEqual(p[::2], pseq[::2])
        self.assertEqual(p[::-1], pseq[::-1]) 
Example #16
Source File: parser.py    From tappy with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _parse_result(self, ok, match, fh=None):
        """Parse a matching result line into a result instance."""
        peek_match = None
        try:
            if fh is not None and ENABLE_VERSION_13 and isinstance(fh, peekable):
                peek_match = self.yaml_block_start.match(fh.peek())
        except StopIteration:
            pass
        if peek_match is None:
            return Result(
                ok,
                number=match.group("number"),
                description=match.group("description").strip(),
                directive=Directive(match.group("directive")),
            )
        indent = peek_match.group("indent")
        concat_yaml = self._extract_yaml_block(indent, fh)
        return Result(
            ok,
            number=match.group("number"),
            description=match.group("description").strip(),
            directive=Directive(match.group("directive")),
            raw_yaml_block=concat_yaml,
        ) 
Example #17
Source File: util.py    From python-mwviews with MIT License 6 votes vote down vote up
def collate(*iterables, key=lambda i: i):
    # Prepare the iterable queue
    nextq = []
    for i, iterable in enumerate(iterables):
        iterable = peekable(iterable)
        try:
            heapq.heappush(nextq, ((iterable.peek(), i), iterable))
        except StopIteration:
            # Na. We're cool
            pass

    while len(nextq) > 0:
        (_, i), iterable = heapq.heappop(nextq)

        yield next(iterable)

        try:
            heapq.heappush(nextq, ((iterable.peek(), i), iterable))
        except:
            # Again, we're cool.
            pass 
Example #18
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_prepend(self):
        """Tests intersperesed ``prepend()`` and ``next()`` calls"""
        it = mi.peekable(range(2))
        actual = []

        # Test prepend() before next()
        it.prepend(10)
        actual += [next(it), next(it)]

        # Test prepend() between next()s
        it.prepend(11)
        actual += [next(it), next(it)]

        # Test prepend() after source iterable is consumed
        it.prepend(12)
        actual += [next(it)]

        expected = [10, 0, 11, 1, 12]
        self.assertEqual(actual, expected) 
Example #19
Source File: finders.py    From openapi-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def find(self, request):
        paths_iter = self._get_paths_iter(request.full_url_pattern)
        paths_iter_peek = peekable(paths_iter)

        if not paths_iter_peek:
            raise PathNotFound(request.full_url_pattern)

        operations_iter = self._get_operations_iter(
            request.method, paths_iter_peek)
        operations_iter_peek = peekable(operations_iter)

        if not operations_iter_peek:
            raise OperationNotFound(request.full_url_pattern, request.method)

        servers_iter = self._get_servers_iter(
            request.full_url_pattern, operations_iter_peek)

        try:
            return next(servers_iter)
        except StopIteration:
            raise ServerNotFound(request.full_url_pattern) 
Example #20
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_indexing(self):
        """
        Indexing into the peekable shouldn't advance the iterator.
        """
        p = mi.peekable('abcdefghijkl')

        # The 0th index is what ``next()`` will return
        self.assertEqual(p[0], 'a')
        self.assertEqual(next(p), 'a')

        # Indexing further into the peekable shouldn't advance the itertor
        self.assertEqual(p[2], 'd')
        self.assertEqual(next(p), 'b')

        # The 0th index moves up with the iterator; the last index follows
        self.assertEqual(p[0], 'c')
        self.assertEqual(p[9], 'l')

        self.assertEqual(next(p), 'c')
        self.assertEqual(p[8], 'l')

        # Negative indexing should work too
        self.assertEqual(p[-2], 'k')
        self.assertEqual(p[-9], 'd')
        self.assertRaises(IndexError, lambda: p[-10]) 
Example #21
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_indexing(self):
        """
        Indexing into the peekable shouldn't advance the iterator.
        """
        p = mi.peekable('abcdefghijkl')

        # The 0th index is what ``next()`` will return
        self.assertEqual(p[0], 'a')
        self.assertEqual(next(p), 'a')

        # Indexing further into the peekable shouldn't advance the itertor
        self.assertEqual(p[2], 'd')
        self.assertEqual(next(p), 'b')

        # The 0th index moves up with the iterator; the last index follows
        self.assertEqual(p[0], 'c')
        self.assertEqual(p[9], 'l')

        self.assertEqual(next(p), 'c')
        self.assertEqual(p[8], 'l')

        # Negative indexing should work too
        self.assertEqual(p[-2], 'k')
        self.assertEqual(p[-9], 'd')
        self.assertRaises(IndexError, lambda: p[-10]) 
Example #22
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_slicing(self):
        """Slicing the peekable shouldn't advance the iterator."""
        seq = list('abcdefghijkl')
        p = mi.peekable(seq)

        # Slicing the peekable should just be like slicing a re-iterable
        self.assertEqual(p[1:4], seq[1:4])

        # Advancing the iterator moves the slices up also
        self.assertEqual(next(p), 'a')
        self.assertEqual(p[1:4], seq[1:][1:4])

        # Implicit starts and stop should work
        self.assertEqual(p[:5], seq[1:][:5])
        self.assertEqual(p[:], seq[1:][:])

        # Indexing past the end should work
        self.assertEqual(p[:100], seq[1:][:100])

        # Steps should work, including negative
        self.assertEqual(p[::2], seq[1:][::2])
        self.assertEqual(p[::-1], seq[1:][::-1]) 
Example #23
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_slicing(self):
        """Slicing the peekable shouldn't advance the iterator."""
        seq = list('abcdefghijkl')
        p = mi.peekable(seq)

        # Slicing the peekable should just be like slicing a re-iterable
        self.assertEqual(p[1:4], seq[1:4])

        # Advancing the iterator moves the slices up also
        self.assertEqual(next(p), 'a')
        self.assertEqual(p[1:4], seq[1:][1:4])

        # Implicit starts and stop should work
        self.assertEqual(p[:5], seq[1:][:5])
        self.assertEqual(p[:], seq[1:][:])

        # Indexing past the end should work
        self.assertEqual(p[:100], seq[1:][:100])

        # Steps should work, including negative
        self.assertEqual(p[::2], seq[1:][::2])
        self.assertEqual(p[::-1], seq[1:][::-1]) 
Example #24
Source File: doi.py    From python-mwcites with MIT License 5 votes vote down vote up
def extract_search(text, lexicon=LEXICON):

    last_end = 0
    for match in DOI_START_RE.finditer(text):
        if match.span()[0] > last_end:
            tokens = tokenize_search(text, match.span()[0], lexicon=lexicon)
            tokens = peekable(tokens)
            doi = read_doi(tokens)
            last_end = match.span()[0] + len(doi)
            yield Identifier('doi', doi)
        else:
            last_end = max(match.span()[1], last_end) 
Example #25
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_truthiness(self):
        """Make sure a ``peekable`` tests true iff there are items remaining in
        the iterable.

        """
        p = mi.peekable([])
        self.assertFalse(p)

        p = mi.peekable(range(3))
        self.assertTrue(p) 
Example #26
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_prepend_reversed(self):
        """Tests prepending from a reversed iterable"""
        it = mi.peekable(range(3))
        it.prepend(*reversed((10, 11, 12)))
        actual = list(it)
        expected = [12, 11, 10, 0, 1, 2]
        self.assertEqual(actual, expected) 
Example #27
Source File: doi.py    From python-mwcites with MIT License 5 votes vote down vote up
def extract_island(text):
    tokens = tokenize_finditer(text, LEXICON)
    tokens = peekable(tokens)

    while tokens.peek(None) is not None:

        if tokens.peek()[0] == 'doi_start':
            yield ('doi', read_doi(tokens))

        next(tokens) 
Example #28
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_prepend_iterable(self):
        """Tests prepending from an iterable"""
        it = mi.peekable(range(5))
        # Don't directly use the range() object to avoid any range-specific
        # optimizations
        it.prepend(*(x for x in range(5)))
        actual = list(it)
        expected = list(chain(range(5), range(5)))
        self.assertEqual(actual, expected) 
Example #29
Source File: utils.py    From huskar with MIT License 5 votes vote down vote up
def dedupleft(iterable, marker):
    """Deduplicates the marker on the left of an iterable object."""
    iterator = peekable(iterable)
    for x in iterator:
        if iterator.peek(None) != marker:
            break
    return itertools.chain([marker], iterator) 
Example #30
Source File: timeline.py    From byro with Apache License 2.0 5 votes vote down vote up
def augment_timeline(entries):
    last_year = None
    last_month = None

    entries = peekable(add_dummy_entries(entries))

    while entries:
        entry = next(entries)
        nentry = entries.peek(None)
        tl = {
            "year_first": False,
            "year_last": False,
            "month_first": False,
            "month_last": False,
            "entry_id": "{}:{}:{}".format(
                entry["type"], entry["subtype"], entry["instance"].pk
            )
            if entry["instance"]
            else None,
        }
        if last_year != entry["date"].year:
            tl["year_first"] = True
        if last_month != entry["date"].month:
            tl["month_first"] = True
        if not nentry or nentry["date"].year != entry["date"].year:
            tl["year_last"] = True
        if not nentry or nentry["date"].month != entry["date"].month:
            tl["month_last"] = True
        yield dict(entry, tl=tl)
        last_year = entry["date"].year
        last_month = entry["date"].month