Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(8)  |  Call(4)  |  Derive(0)  |  Import(4)

        def slides(iterable, width=2, fill=None):
    '''Creates a sliding window of a given `width` over an iterable::

        >>> list(slides(range(1, 11)))
        [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

    If the iterator does not yield a width-aligned number of items, the last
    slice returned is filled with `fill` (by default None)::

        >>> list(slides(range(1, 11), width=3))   # doctest: +ELLIPSIS
        [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, None, None)]

    .. versionchanged:: 1.4.0 If the `fill` argument is a collection is cycled
                        over to get the filling, just like in :func:`first_n`.

    .. versionchanged:: 1.4.2 The `fill` argument now defaults to None,
                        instead of Unset.

    '''
    from itertools import cycle, repeat
    from xoutil.types import is_collection
    pos = 0
    res = []
    iterator = iter(iterable)
    current = next(iterator, Unset)
    while current is not Unset:
        if pos < width:
            res.append(current)
            current = next(iterator, Unset)
            pos = pos + 1
        else:
            yield tuple(res)
            res = []
            pos = 0
    if res:
        if is_collection(fill):
            fill = cycle(fill)
        else:
            fill = repeat(fill)
        while pos < width:
            res.append(next(fill))
            pos += 1
        yield tuple(res)
        


src/x/o/xoutil-1.5.4/tests/test_iterators.py   xoutil(Download)
def test_slides():
    from xoutil.iterators import slides
    assert list(slides(range(1, 11))) == [(1, 2),
                                          (3, 4),
                                          (5, 6),
                                          (7, 8),
                                          (9, 10)]
 
def test_slides_filling():
    from xoutil.iterators import slides
def test_slides_filling():
    from xoutil.iterators import slides
    assert list(slides(range(1, 5), 3)) == [(1, 2, 3), (4, None, None)]
 
 
def test_slides_with_repeating_filling():
    from xoutil.iterators import slides