Example #1
4
async def aitersync(iterable):
    results = []

    async for x in aiter(iterable):
        results.append(x)

    return iter(results)
Example #2
1
async def amax(iterable, key=None, default=_missing):
    if key is None:
        key = lambda x: x
    key = wrapsync(key)

    value = _missing
    kvalue = _missing

    async for x in aiter(iterable):
        kx = await key(x)

        if value is _missing:
            value = x
            kvalue = kx
        else:
            if kx > kvalue:
                value = x
                kvalue = kx

    if value is _missing:
        if default is not _missing:
            return default

        raise ValueError('amax() arg is an empty sequence')

    return value
Example #3
1
async def atakewhile(predicate, iterable):
    predicate = wrapsync(predicate)
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    async for x in aiter(iterable):
        if await predicate(x):
            await async_yield(x)
        else:
            break
Example #4
1
async def afilterfalse(function, iterable):
    if function is None:
        function = lambda x: x
    function = wrapsync(function)

    async for element in aiter(iterable):
        if not await function(element):
            await async_yield(element)
Example #5
0
async def amax(iterable, key=None, default=_missing):
    if key is None:
        key = lambda x: x
    key = wrapsync(key)

    value = _missing
    kvalue = _missing

    async for x in aiter(iterable):
        kx = await key(x)

        if value is _missing:
            value = x
            kvalue = kx
        else:
            if kx > kvalue:
                value = x
                kvalue = kx

    if value is _missing:
        if default is not _missing:
            return default

        raise ValueError('amax() arg is an empty sequence')

    return value
Example #6
0
async def aitersync(iterable):
    results = []

    async for x in aiter(iterable):
        results.append(x)

    return iter(results)
Example #7
0
async def afilterfalse(function, iterable):
    if function is None:
        function = lambda x: x
    function = wrapsync(function)

    async for element in aiter(iterable):
        if not await function(element):
            await async_yield(element)
Example #8
0
async def atakewhile(predicate, iterable):
    predicate = wrapsync(predicate)
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    async for x in aiter(iterable):
        if await predicate(x):
            await async_yield(x)
        else:
            break
Example #9
0
async def acycle(iterable):
    # cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    async for element in aiter(iterable):
        await async_yield(element)
        saved.append(element)
    while saved:
        for element in saved:
            await async_yield(element)
Example #10
0
async def acycle(iterable):
    # cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    async for element in aiter(iterable):
        await async_yield(element)
        saved.append(element)
    while saved:
        for element in saved:
            await async_yield(element)
Example #11
0
async def adropwhile(predicate, iterable):
    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    predicate = wrapsync(predicate)
    iterable = aiter(iterable)
    async for x in iterable:
        if not await predicate(x):
            await async_yield(x)
            break
    async for x in iterable:
        await async_yield(x)
Example #12
0
async def adropwhile(predicate, iterable):
    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    predicate = wrapsync(predicate)
    iterable = aiter(iterable)
    async for x in iterable:
        if not await predicate(x):
            await async_yield(x)
            break
    async for x in iterable:
        await async_yield(x)
Example #13
0
async def azip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterators = []
    for it in iterables:
        iterators.append(aiter(it))

    while iterators:
        result = []
        async for it in iterators:
            elem = await anext(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)

        await async_yield(tuple(result))
Example #14
0
async def azip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterators = []
    for it in iterables:
        iterators.append(aiter(it))

    while iterators:
        result = []
        async for it in iterators:
            elem = await anext(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)

        await async_yield(tuple(result))
Example #15
0
async def aaccumulate(iterable, func=None):
    'Return running totals'

    if func is None:
        func = operator.add
    func = wrapsync(func)

    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = aiter(iterable)
    try:
        total = await anext(it)
    except StopAsyncIteration:
        return
    await async_yield(total)
    async for element in it:
        total = await func(total, element)
        await async_yield(total)
Example #16
0
async def aaccumulate(iterable, func=None):
    'Return running totals'

    if func is None:
        func = operator.add
    func = wrapsync(func)

    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = aiter(iterable)
    try:
        total = await anext(it)
    except StopAsyncIteration:
        return
    await async_yield(total)
    async for element in it:
        total = await func(total, element)
        await async_yield(total)
Example #17
0
async def atee(iterable, n=2):
    it = aiter(iterable)
    deques = [collections.deque() for i in range(n)]

    async def gen(mydeque):
        while True:
            if not mydeque:  # when the local deque is empty
                try:
                    newval = await anext(it)  # fetch a new value and
                except StopAsyncIteration:
                    return
                for d in deques:  # load it to all the deques
                    d.append(newval)
            await async_yield(mydeque.popleft())

    res = []
    for d in deques:
        res.append(await gen(d))
    return tuple(res)
Example #18
0
async def atee(iterable, n=2):
    it = aiter(iterable)
    deques = [collections.deque() for i in range(n)]

    async def gen(mydeque):
        while True:
            if not mydeque:  # when the local deque is empty
                try:
                    newval = await anext(it)  # fetch a new value and
                except StopAsyncIteration:
                    return
                for d in deques:  # load it to all the deques
                    d.append(newval)
            await async_yield(mydeque.popleft())

    res = []
    for d in deques:
        res.append(await gen(d))
    return tuple(res)
Example #19
0
async def aany(iterable):
    async for element in aiter(iterable):
        if element:
            return True
    return False
Example #20
0
async def astarmap(function, iterable):
    # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    function = wrapsync(function)
    async for args in aiter(iterable):
        await async_yield(await function(*args))
Example #21
0
async def afrom_iterable(iterables):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    async for it in aiter(iterables):
        async for element in aiter(it):
            await async_yield(element)
Example #22
0
async def achain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        async for element in aiter(it):
            await async_yield(element)
Example #23
0
async def asum(iterable, start=0):
    async for x in aiter(iterable):
        start += x

    return start
Example #24
0
async def astarmap(function, iterable):
    # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    function = wrapsync(function)
    async for args in aiter(iterable):
        await async_yield(await function(*args))
Example #25
0
async def asum(iterable, start=0):
    async for x in aiter(iterable):
        start += x

    return start
Example #26
0
async def aenumerate(sequence, start=0):
    n = start
    async for elem in aiter(sequence):
        await async_yield(n, elem)
        n += 1
Example #27
0
async def aany(iterable):
    async for element in aiter(iterable):
        if element:
            return True
    return False
Example #28
0
async def aall(iterable):
    async for element in aiter(iterable):
        if not element:
            return False
    return True
Example #29
0
async def asorted(iterable, *args, **kwargs):
    return aiter(sorted(await aitersync(iterable), *args, **kwargs))
Example #30
0
async def afrom_iterable(iterables):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    async for it in aiter(iterables):
        async for element in aiter(it):
            await async_yield(element)
Example #31
0
async def aall(iterable):
    async for element in aiter(iterable):
        if not element:
            return False
    return True
Example #32
0
async def asorted(iterable, *args, **kwargs):
    return aiter(sorted(await aitersync(iterable), *args, **kwargs))
Example #33
0
async def aenumerate(sequence, start=0):
    n = start
    async for elem in aiter(sequence):
        await async_yield(n, elem)
        n += 1
Example #34
0
async def achain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        async for element in aiter(it):
            await async_yield(element)
Example #35
0
async def areversed(seq):
    return aiter(reversed(await aitersync(seq)))
Example #36
0
async def areversed(seq):
    return aiter(reversed(await aitersync(seq)))