Python six 模块,Iterator() 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用six.Iterator()

项目:deb-oslo.vmware    作者:openstack    | 项目源码 | 文件源码
def test_read(self):
        max_items = 10
        item = [1] * 10

        class ImageReadIterator(six.Iterator):

            def __init__(self):
                self.num_items = 0

            def __iter__(self):
                return self

            def __next__(self):
                if (self.num_items < max_items):
                    self.num_items += 1
                    return item
                raise StopIteration

            next = __next__

        handle = rw_handles.ImageReadHandle(ImageReadIterator())
        for _ in range(0, max_items):
            self.assertEqual(item, handle.read(10))
        self.assertFalse(handle.read(10))
项目:nuts-flow    作者:maet3608    | 项目源码 | 文件源码
def chunked(iterable, n):
    """
    Split iterable in chunks of size n, where each chunk is also an iterator.

    for chunk in chunked(range(10), 3):
        for element in chunk:
            print element

    >>> it = chunked(range(7), 2)
    >>> list(map(tuple, it))
    [(0, 1), (2, 3), (4, 5), (6,)]

    :param iterable iterable: Any iterable, e.g. list, range, ...
    :param n: Chunk size
    :return: Chunked iterable
    :rtype: Iterator over iterators
    """
    it = iter(iterable)
    while True:
        chunk_it = itt.islice(it, n)
        try:
            first_el = next(chunk_it)
        except StopIteration:
            return
        yield itt.chain((first_el,), chunk_it)
项目:nuts-flow    作者:maet3608    | 项目源码 | 文件源码
def flatmap(func, iterable):
    """
    Map function to iterable and flatten.

    >>> f = lambda n: str(n) * n
    >>> list( flatmap(f, [1, 2, 3]) )
    ['1', '2', '2', '3', '3', '3']

    >>> list( map(f, [1, 2, 3]) )  # map instead of flatmap
    ['1', '22', '333']

    :param function func: Function to map on iterable.
    :param iterable iterable: Any iterable, e.g. list, range, ...
    :return: Iterator of iterable elements transformed via func and flattened.
    :rtype: Iterator
    """
    return itt.chain.from_iterable(map(func, iterable))
项目:obsoleted-vpduserv    作者:InfraSIM    | 项目源码 | 文件源码
def test_iterator():
    class myiter(six.Iterator):
        def __next__(self):
            return 13
    assert six.advance_iterator(myiter()) == 13
    class myitersub(myiter):
        def __next__(self):
            return 14
    assert six.advance_iterator(myitersub()) == 14
项目:Deploy_XXNET_Server    作者:jzp820927    | 项目源码 | 文件源码
def test_iterator():
    class myiter(six.Iterator):
        def __next__(self):
            return 13
    assert six.advance_iterator(myiter()) == 13
    class myitersub(myiter):
        def __next__(self):
            return 14
    assert six.advance_iterator(myitersub()) == 14
项目:six    作者:benjaminp    | 项目源码 | 文件源码
def test_iterator():
    class myiter(six.Iterator):
        def __next__(self):
            return 13
    assert six.advance_iterator(myiter()) == 13
    class myitersub(myiter):
        def __next__(self):
            return 14
    assert six.advance_iterator(myitersub()) == 14
项目:nuts-flow    作者:maet3608    | 项目源码 | 文件源码
def take(iterable, n):
    """
    Return iterator over last n elements of given iterable.

    >>> list(take(range(10), 3))
    [0, 1, 2]

    See: https://docs.python.org/2/library/itertools.html#itertools.islice

    :param iterable iterable: Any iterable, e.g. list, range, ...
    :param int n: Number of elements to take
    :return: Iterator over last n elements
    :rtype: iterator
    """
    return itt.islice(iterable, n)
项目:nuts-flow    作者:maet3608    | 项目源码 | 文件源码
def unique(iterable, key=None):
    """
    Return only unique elements in iterable. Potentially high mem. consumption!

    >>> list(unique([2,3,1,1,2,4]))
    [2, 3, 1, 4]

    >>> ''.join(unique('this is a test'))
    'this ae'

    >>> data = [(1,'a'), (2,'a'), (3,'b')]
    >>> list(unique(data, key=lambda t: t[1]))
    [(1, 'a'), (3, 'b')]

    :param iterable iterable: Any iterable, e.g. list, range, ...
    :param key: Function used to compare for equality.
    :return: Iterator over unique elements.
    :rtype: Iterator
    """
    seen = set()
    for e in iterable:
        k = key(e) if key else e
        if k not in seen:
            seen.add(k)
            yield e
项目:nuts-flow    作者:maet3608    | 项目源码 | 文件源码
def flatten(iterable):
    """
    Return flattened iterable.

    >>> list(flatten([(1,2), (3,4,5)]))
    [1, 2, 3, 4, 5]

    :param iterable iterable:
    :return: Iterator over flattened elements of iterable
    :rtype: Iterator
    """
    return itt.chain(*iterable)