Пример #1
0
    async def handle_label(i, label):
        label.config(dict(borderwidth=0, padx=0, pady=0))

        async def asend(ev):
            label.place(x=ev.x + i * 12 + 15, y=ev.y)

        xs = mousemoves | op.delay(i / 10.0)
        await subscribe(xs, AnonymousAsyncObserver(asend))
async def test_from_iterable_happy():
    xs = from_iterable([1, 2, 3])
    result = []

    async def asend(value):
        result.append(value)

    await run(xs, AnonymousAsyncObserver(asend))
    assert result == [1, 2, 3]
Пример #3
0
async def test_distinct_until_changed_changed():
    xs = from_iterable([1, 2, 2, 1, 3, 3, 1, 2, 2])
    result = []

    async def asend(value):
        result.append(value)

    ys = distinct_until_changed(xs)

    await run(ys, AnonymousAsyncObserver(asend))
    assert result == [1, 2, 1, 3, 1, 2]
Пример #4
0
async def test_take_empty() -> None:
    xs = AsyncObservable.empty()
    values = []

    async def asend(value) -> None:
        values.append(value)

    ys = take(42, xs)

    with pytest.raises(CancelledError):
        await run(ys, AnonymousAsyncObserver(asend))

    assert values == []
Пример #5
0
async def test_take_zero() -> None:
    xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5])
    values = []

    async def asend(value) -> None:
        values.append(value)

    ys = take(0, xs)

    with pytest.raises(CancelledError):
        await run(ys, AnonymousAsyncObserver(asend))

    assert values == []
Пример #6
0
async def test_slice_special():
    xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5])
    values = []

    async def asend(value):
        values.append(value)

    ys = xs[1:-1]

    result = await run(ys, AnonymousAsyncObserver(asend))

    assert result == 4
    assert values == [2, 3, 4]
Пример #7
0
async def test_slice_step():
    xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5])
    values = []

    async def asend(value):
        values.append(value)

    ys = xs[::2]

    result = await run(ys, AnonymousAsyncObserver(asend))

    assert result == 5
    assert values == [1, 3, 5]
Пример #8
0
async def test_concat_special_iadd():
    xs = AsyncObservable.from_iterable(range(5))
    ys = AsyncObservable.from_iterable(range(5, 10))
    result = []

    async def asend(value):
        log.debug("test_merge_done:asend(%s)", value)
        result.append(value)

    xs += ys

    await run(xs, AnonymousAsyncObserver(asend))
    assert result == list(range(10))
Пример #9
0
async def test_concat_happy():
    xs = from_iterable(range(5))
    ys = from_iterable(range(5, 10))
    result = []

    async def asend(value):
        log.debug("test_merge_done:send: ", value)
        result.append(value)

    zs = concat(xs, ys)

    await run(zs, AnonymousAsyncObserver(asend))
    assert result == list(range(10))
Пример #10
0
async def test_take_normal() -> None:
    xs = AsyncObservable.from_iterable([1, 2, 3, 4, 5])
    values = []

    async def asend(value) -> None:
        values.append(value)

    ys = take(2, xs)

    result = await run(ys, AnonymousAsyncObserver(asend))

    assert result == 2
    assert values == [1, 2]
Пример #11
0
async def test_pipe_map():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

    ys = pipe(xs, op.map(mapper))

    async def asend(value):
        result.append(value)

    await run(ys, AnonymousAsyncObserver(asend))
    assert result == [10, 20, 30]
Пример #12
0
async def test_forward_pipe_map() -> None:
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value) -> int:
        return value * 10

    ys = xs | op.map(mapper)

    async def asend(value) -> None:
        result.append(value)

    await run(ys, AnonymousAsyncObserver(asend))
    assert result == [10, 20, 30]
async def test_from_iterable_observer_throws():
    xs = from_iterable([1, 2, 3])
    result = []

    async def asend(value):
        result.append(value)
        raise Exception()

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except:
        pass
    assert result == [1]
Пример #14
0
async def test_filter_happy():
    xs = from_iterable([1, 2, 3])
    result = []

    async def asend(value):
        result.append(value)

    async def predicate(value):
        await asyncio.sleep(0.1)
        return value > 1

    ys = filter(predicate, xs)
    value = await run(ys, AnonymousAsyncObserver(asend))
    assert value == 3
    assert result == [2, 3]
Пример #15
0
async def test_chain_map():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

    ys = chain(xs).select(mapper)

    async def on_next(value):
        result.append(value)

    sub = await ys.subscribe(AnonymousAsyncObserver(on_next))
    await sub
    assert result == [10, 20, 30]
Пример #16
0
async def test_withlatestfrom_never_never():
    xs = never()
    ys = never()
    result = []

    async def asend(value):
        nonlocal result
        asyncio.sleep(0.1)
        result.append(value)

    zs = with_latest_from(lambda x, y: x + y, ys, xs)

    await subscribe(zs, AnonymousAsyncObserver(asend))
    await asyncio.sleep(1)

    assert result == []
Пример #17
0
async def test_map_happy():
    xs = from_iterable([1, 2, 3])  # type: AsyncObservable[int]
    values = []

    async def asend(value):
        values.append(value)

    def mapper(value: int) -> int:
        return value * 10

    ys = xs | op.map(mapper)

    result = await run(ys, AnonymousAsyncObserver(asend))

    assert result == 30
    assert values == [10, 20, 30]
Пример #18
0
async def test_filter_predicate_throws():
    xs = from_iterable([1, 2, 3])
    err = MyException("err")
    result = []

    async def asend(value):
        result.append(value)

    async def predicate(value):
        await asyncio.sleep(0.1)
        raise err

    ys = filter(predicate, xs)

    with pytest.raises(MyException):
        await run(ys, AnonymousAsyncObserver(asend))

    assert result == []
Пример #19
0
async def test_withlatestfrom_never_empty():
    xs = empty()
    ys = never()
    result = []

    async def asend(value):
        log.debug("test_withlatestfrom_never_empty:asend(%s)", value)
        nonlocal result
        asyncio.sleep(0.1)
        result.append(value)

    zs = with_latest_from(lambda x, y: x + y, ys, xs)

    try:
        await run(zs, AnonymousAsyncObserver(asend))
    except asyncio.CancelledError:
        pass
    assert result == []
Пример #20
0
async def test_forward_pipe_simple_pipe() -> None:
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value) -> int:
        return value * 10

    async def predicate(value) -> bool:
        await asyncio.sleep(0.1)
        return value > 1

    ys = xs | op.filter(predicate) | op.map(mapper)

    async def asend(value) -> None:
        result.append(value)

    await run(ys, AnonymousAsyncObserver(asend))
    assert result == [20, 30]
async def test_from_iterable_close():
    xs = from_iterable(range(100))
    result = []
    sub = None

    async def asend(value):
        result.append(value)
        sub.cancel()
        await asyncio.sleep(0)

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except asyncio.CancelledError:
        pass

    assert result == [0]
Пример #22
0
async def test_chain_simple_pipe():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

    async def predicate(value):
        await asyncio.sleep(0.1)
        return value > 1

    ys = chain(xs).where(predicate).select(mapper)

    async def on_next(value):
        result.append(value)

    sub = await ys.subscribe(AnonymousAsyncObserver(on_next))
    await sub
    assert result == [20, 30]
Пример #23
0
async def test_flap_map_done():
    xs = AsyncStream()
    result = []

    async def asend(value):
        nonlocal result
        result.append(value)

    async def mapper(value):
        return from_iterable([value])

    ys = flat_map(mapper, xs)
    await subscribe(ys, AnonymousAsyncObserver(asend))
    await xs.asend(10)
    await xs.asend(20)

    await asyncio.sleep(0.6)

    assert result == [10, 20]
Пример #24
0
async def test_debounce_filter():
    xs = AsyncStream()
    result = []

    async def asend(value):
        log.debug("test_debounce_filter:asend(%s)", value)
        nonlocal result
        result.append(value)

    ys = debounce(0.5, xs)
    sub = await subscribe(ys, AnonymousAsyncObserver(asend))
    await xs.asend(1)
    await asyncio.sleep(0.3)
    await xs.asend(2)
    await xs.aclose()
    await asyncio.sleep(0.6)
    await sub

    assert result == [2]
Пример #25
0
async def test_pipe_simple_pipe():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

    async def predicate(value):
        await asyncio.sleep(0.1)
        return value > 1

    ys = pipe(xs,
              op.filter(predicate),
              op.map(mapper)
              )

    async def asend(value):
        result.append(value)

    await run(ys, AnonymousAsyncObserver(asend))
    assert result == [20, 30]
Пример #26
0
async def test_withlatestfrom_done():
    xs = AsyncStream()
    ys = AsyncStream()
    result = []

    async def asend(value):
        log.debug("test_withlatestfrom_done:asend(%s)", value)
        nonlocal result
        asyncio.sleep(0.1)
        result.append(value)

    zs = with_latest_from(lambda x, y: x + y, ys, xs)

    sub = await subscribe(zs, AnonymousAsyncObserver(asend))
    await xs.asend(1)
    await ys.asend(2)
    await xs.asend(3)
    await xs.aclose()
    await sub

    assert result == [5]
Пример #27
0
async def test_map_mapper_throws():
    xs = from_iterable([1])
    exception = None
    error = Exception("ex")

    async def asend(value):
        pass

    async def athrow(ex):
        nonlocal exception
        exception = ex

    def mapper(x):
        raise error

    ys = xs | op.map(mapper)

    try:
        await run(ys, AnonymousAsyncObserver(asend, athrow))
    except Exception as ex:
        assert ex == error

    assert exception == error
Пример #28
0
async def websocket_handler(request):
    print("WebSocket opened")

    stream = AsyncStream()

    # Line break before binary operator is more readable. Disable W503
    xs = (
        stream
        | op.map(lambda x: x["term"])
        | op.filter(lambda text: len(text) > 2)
        | op.debounce(0.75)
        | op.distinct_until_changed()
        | op.flat_map(search_wikipedia)
        #| op.switch_latest()
    )

    ws = web.WebSocketResponse()
    await ws.prepare(request)

    async def asend(value):
        ws.send_str(value)

    async def athrow(ex):
        print(ex)

    await subscribe(xs, AnonymousAsyncObserver(asend, athrow))

    async for msg in ws:
        if msg.type == aiohttp.WSMsgType.TEXT:
            obj = json.loads(msg.data)
            await stream.asend(obj)

        elif msg.type == aiohttp.WSMsgType.ERROR:
            print('ws connection closed with exception %s' % ws.exception())

    print('websocket connection closed')
    return ws
Пример #29
0
async def test_map_subscription_cancel():
    xs = AsyncStream()
    result = []
    sub = None

    def mapper(value):
        return value * 10

    print("-----------------------")
    print([xs, op.map])
    ys = xs | op.map(mapper)

    async def asend(value):
        result.append(value)
        sub.dispose()
        await asyncio.sleep(0)

    async with subscribe(ys, AnonymousAsyncObserver(asend)) as sub:

        await xs.asend(10)
        await asyncio.sleep(0)
        await xs.asend(20)

    assert result == [100]