Beispiel #1
0
async def main():
    xs = AsyncObservable.from_iterable(range(10))

    # Split into odds and evens
    odds = xs | op.filter(lambda x: x % 2 == 1)
    evens = xs | op.filter(lambda x: x % 2 == 0)

    async def mysink(value):
        print(value)

    await subscribe(odds, AsyncAnonymousObserver(mysink))
    await subscribe(evens, AsyncAnonymousObserver(mysink))
Beispiel #2
0
async def test_chain_complex_pipe():
    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

    async def long_running(value) -> AsyncObservable:
        return AsyncObservable.from_iterable([value])

    ys = (xs
          .where(predicate)
          .select(mapper)
          .select_many(long_running))

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

    obv = AsyncAnonymousObserver(on_next)
    sub = await ys.subscribe(obv)
    await obv
    assert result == [20, 30]
Beispiel #3
0
async def websocket_handler(request):
    print("WebSocket opened")

    stream = AsyncStream()

    xs = (
        stream
        | op.map(lambda x: x["term"])
        | op.filter(lambda text: len(text) > 2)
        | op.debounce(0.5)
        | op.distinct_until_changed()
        | op.flat_map(search_wikipedia)
    )

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

    async def asend(value) -> None:
        ws.send_str(value)

    async def athrow(ex) -> None:
        print(ex)

    await subscribe(xs, AsyncAnonymousObserver(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
Beispiel #4
0
    async def p_subscribe(list_, stream_):
        from aioreactive.core import subscribe

        async def append(x):
            list_.append(x)

        obv = AsyncAnonymousObserver(append)
        await subscribe(stream_, obv)
Beispiel #5
0
    async def handle_label(i, label) -> None:
        label.config(dict(borderwidth=0, padx=0, pady=0))

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

        obv = AsyncAnonymousObserver(asend)

        await (mousemoves | delay(i / 10.0) > obv)
Beispiel #6
0
async def test_from_iterable_happy():
    xs = from_iterable([1, 2, 3])
    result = []

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

    await run(xs, AsyncAnonymousObserver(asend))
    assert result == [1, 2, 3]
Beispiel #7
0
async def test_catch_one_with_error():
    xs = from_async_iterable(iter_error())
    result = []

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

    zs = catch_exception([xs])

    await run(zs, AsyncAnonymousObserver(asend))
    assert result == list(range(5))
Beispiel #8
0
async def test_catch_no_error():
    xs = from_iterable(range(0, 5))
    ys = from_iterable(range(5, 10))
    result = []

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

    zs = catch_exception((xs, ys))

    await run(zs, AsyncAnonymousObserver(asend))
    assert result == list(range(5))
Beispiel #9
0
async def test_concat_async():
    xs = from_async_iterable(asynciter())
    ys = from_iterable(range(5, 10))
    result = []

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

    zs = concat(xs, ys)

    await run(zs, AsyncAnonymousObserver(asend))
    assert result == list(range(10))
Beispiel #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, AsyncAnonymousObserver(asend))

    assert result == 2
    assert values == [1, 2]
Beispiel #11
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, AsyncAnonymousObserver(asend))

    assert values == []
Beispiel #12
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, AsyncAnonymousObserver(asend))

    assert result == 5
    assert values == [1, 3, 5]
Beispiel #13
0
async def test_retry():
    xs = from_async_iterable(iter_error())
    result = []

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

    zs = catch_exception([xs])

    await run(zs, AsyncAnonymousObserver(asend))
    assert result == list(range(10))
Beispiel #14
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, AsyncAnonymousObserver(asend))
    assert result == list(range(10))
Beispiel #15
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, AsyncAnonymousObserver(asend))

    assert values == []
Beispiel #16
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, AsyncAnonymousObserver(asend))
    assert result == list(range(10))
Beispiel #17
0
async def test_pipe_map():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

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

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

    await run(ys, AsyncAnonymousObserver(asend))
    assert result == [10, 20, 30]
Beispiel #18
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 | _.map(mapper)

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

    await run(ys, AsyncAnonymousObserver(asend))
    assert result == [10, 20, 30]
Beispiel #19
0
async def test_filter_happy() -> None:
    xs = from_iterable([1, 2, 3])
    result = []

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

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

    ys = filter(predicate, xs)
    value = await run(ys, AsyncAnonymousObserver(asend))
    assert value == 3
    assert result == [2, 3]
Beispiel #20
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 | _.map(mapper)

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

    assert result == 30
    assert values == [10, 20, 30]
Beispiel #21
0
async def test_from_iterable_observer_throws():
    xs = from_iterable([1, 2, 3])
    result = []

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

    obv = AsyncAnonymousObserver(asend)
    await subscribe(xs, obv)

    try:
        await obv
    except Exception:
        pass
    assert result == [1]
Beispiel #22
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, AsyncAnonymousObserver(asend))
    await asyncio.sleep(1)

    assert result == []
Beispiel #23
0
async def test_chain_map():
    xs = AsyncObservable.from_iterable([1, 2, 3])
    result = []

    def mapper(value):
        return value * 10

    ys = xs.select(mapper)

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

    obv = AsyncAnonymousObserver(on_next)
    sub = await ys.subscribe(obv)
    await obv
    assert result == [10, 20, 30]
Beispiel #24
0
async def test_filter_predicate_throws() -> None:
    xs = from_iterable([1, 2, 3])
    err = MyException("err")
    result = []

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

    async def predicate(value: int) -> bool:
        await asyncio.sleep(0.1)
        raise err

    ys = filter(predicate, xs)

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

    assert result == []
Beispiel #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, _.filter(predicate), _.map(mapper))

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

    await run(ys, AsyncAnonymousObserver(asend))
    assert result == [20, 30]
Beispiel #26
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 | _.filter(predicate) | _.map(mapper)

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

    await run(ys, AsyncAnonymousObserver(asend))
    assert result == [20, 30]
Beispiel #27
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, AsyncAnonymousObserver(asend))
    except asyncio.CancelledError:
        pass
    assert result == []
Beispiel #28
0
async def test_from_iterable_close():
    xs = from_iterable(range(100))
    result = []
    sub = None

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

    obv = AsyncAnonymousObserver(asend)
    sub = await subscribe(xs, obv)

    try:
        await obv
    except asyncio.CancelledError:
        pass

    assert result == [0]
Beispiel #29
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)
    obv = AsyncAnonymousObserver(asend)
    sub = await subscribe(ys, obv)
    await xs.asend(1)
    await asyncio.sleep(0.3)
    await xs.asend(2)
    await xs.aclose()
    await asyncio.sleep(0.6)
    await obv

    assert result == [2]
Beispiel #30
0
    async def init(self):
        await self.connection.connect()
        unpack = itemgetter("config", "old_val", "new_val")

        async def update(x):
            cfg, old, new = unpack(x)

            try:
                if new is None:
                    self.latest_config[cfg].delete(old)
                else:
                    self.latest_config[cfg].set(new)
            except:
                logger.exception(f"Unhandled error while updating config {cfg}.")
            else:
                logger.info(f"Updated config {cfg} with {x}")

        return await subscribe(
            from_iterable(self.configs.values())
                | Operators.flat_map(self.config_observable),
            AsyncAnonymousObserver(update))