当前位置: 首页>>代码示例>>Python>>正文


Python more_itertools.peekable方法代码示例

本文整理汇总了Python中more_itertools.peekable方法的典型用法代码示例。如果您正苦于以下问题:Python more_itertools.peekable方法的具体用法?Python more_itertools.peekable怎么用?Python more_itertools.peekable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在more_itertools的用法示例。


在下文中一共展示了more_itertools.peekable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_indexing

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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]) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:27,代码来源:test_more.py

示例2: test_slicing

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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]) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:24,代码来源:test_more.py

示例3: test_prepend

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:21,代码来源:test_more.py

示例4: test_prepend_slicing

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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]) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:19,代码来源:test_more.py

示例5: test_prepend_indexing

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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]) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:20,代码来源:test_more.py

示例6: add_dummy_entries

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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 
开发者ID:byro,项目名称:byro,代码行数:23,代码来源:timeline.py

示例7: find

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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) 
开发者ID:p1c2u,项目名称:openapi-core,代码行数:23,代码来源:finders.py

示例8: _parse_result

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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,
        ) 
开发者ID:python-tap,项目名称:tappy,代码行数:26,代码来源:parser.py

示例9: collate

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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 
开发者ID:mediawiki-utilities,项目名称:python-mwviews,代码行数:23,代码来源:util.py

示例10: test_peek_default

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
def test_peek_default(self):
        """Make sure passing a default into ``peek()`` works."""
        p = mi.peekable([])
        self.assertEqual(p.peek(7), 7) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:6,代码来源:test_more.py

示例11: test_truthiness

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
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) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:12,代码来源:test_more.py

示例12: test_simple_peeking

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
def test_simple_peeking(self):
        """Make sure ``next`` and ``peek`` advance and don't advance the
        iterator, respectively.

        """
        p = mi.peekable(range(10))
        self.assertEqual(next(p), 0)
        self.assertEqual(p.peek(), 1)
        self.assertEqual(next(p), 1) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:11,代码来源:test_more.py

示例13: test_slicing_error

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
def test_slicing_error(self):
        iterable = '01234567'
        p = mi.peekable(iter(iterable))

        # Prime the cache
        p.peek()
        old_cache = list(p._cache)

        # Illegal slice
        with self.assertRaises(ValueError):
            p[1:-1:0]

        # Neither the cache nor the iteration should be affected
        self.assertEqual(old_cache, list(p._cache))
        self.assertEqual(list(p), list(iterable)) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:17,代码来源:test_more.py

示例14: test_passthrough

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
def test_passthrough(self):
        """Iterating a peekable without using ``peek()`` or ``prepend()``
        should just give the underlying iterable's elements (a trivial test but
        useful to set a baseline in case something goes wrong)"""
        expected = [1, 2, 3, 4, 5]
        actual = list(mi.peekable(expected))
        self.assertEqual(actual, expected)

    # prepend() behavior tests 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:11,代码来源:test_more.py

示例15: test_multi_prepend

# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import peekable [as 别名]
def test_multi_prepend(self):
        """Tests prepending multiple items and getting them in proper order"""
        it = mi.peekable(range(5))
        actual = [next(it), next(it)]
        it.prepend(10, 11, 12)
        it.prepend(20, 21)
        actual += list(it)
        expected = [0, 1, 20, 21, 10, 11, 12, 2, 3, 4]
        self.assertEqual(actual, expected) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:11,代码来源:test_more.py


注:本文中的more_itertools.peekable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。