Ejemplo n.º 1
0
def main():
    origin = rx.create(source)

    origin.pipe(
        ops.skip(20),
        ops.map(lambda x: x % 7),
        ops.filter(lambda x: x > 3),
    ).subscribe_(on_next=print)
Ejemplo n.º 2
0
def _solve(print=print):
    v = rx.from_iterable(primes()) \
      .pipe(
        ops.skip(10000),
        ops.take(1),
    ).run()
    print(v)
    return True
Ejemplo n.º 3
0
def audio_encoder(sources):
    # Parse configuration
    parser = create_arg_parser()

    read_request, read_response = sources.argv.argv.pipe(
        ops.skip(1),
        argparse.parse(parser),
        ops.filter(lambda i: i.key == 'config'),
        ops.map(lambda i: file.Read(id='config', path=i.value)),
        file.read(sources.file.response),
    )
    config = read_response.pipe(
        ops.filter(lambda i: i.id == "config"),
        ops.flat_map(lambda i: i.data),
        parse_config,
    )

    # Transcode request handling
    encode_init = config.pipe(
        ops.map(
            lambda i: encoder.Initialize(storage_path=i.encode.storage_path)))

    encode_request = sources.httpd.route.pipe(
        ops.filter(lambda i: i.id == 'flac_transcode'),
        ops.flat_map(lambda i: i.request),
        ops.map(lambda i: encoder.EncodeMp3(
            id=i.context, data=i.data, key=i.match_info['key'])),
    )
    encoder_request = rx.merge(encode_init, encode_request)

    # http server
    http_init = config.pipe(
        ops.flat_map(lambda i: rx.from_([
            httpd.Initialize(request_max_size=0),
            httpd.AddRoute(
                methods=['POST'],
                path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}',
                id='flac_transcode',
            ),
            httpd.StartServer(host=i.server.http.host, port=i.server.http.port
                              ),
        ])))

    http_response = sources.encoder.response.pipe(
        ops.map(lambda i: httpd.Response(
            data='ok'.encode('utf-8'),
            context=i.id,
        )))
    http = rx.merge(http_init, http_response)

    # merge sink requests
    file_requests = read_request

    return Sink(
        encoder=encoder.Sink(request=encoder_request),
        file=file.Sink(request=file_requests),
        httpd=httpd.Sink(control=http),
    )
Ejemplo n.º 4
0
    def on_drag(self) -> Observable:
        mouse = MouseInput.input(self)
        position = mouse.observe("position")

        return self.on_drag_start.pipe(
            ops.map(lambda e: position.pipe(
                ops.skip(1), ops.map(lambda p: DragEvent(self, p, e.button)),
                ops.take_until(mouse.on_button_release(e.button)))),
            ops.exclusive())
Ejemplo n.º 5
0
def parse_arguments(args, prog=None):
    parser = argparse.ArgumentParser(prog=prog)
    parser.add_argument(
        '--config', required=True,
        help="Path of the configuration file")

    return args.pipe(
        ops.skip(1),
        argparse.parse(parser),
    )
Ejemplo n.º 6
0
def parse_arguments(argv):
    parser = argparse.ArgumentParser("deepspeech server")
    parser.add_argument(
        '--config', required=True,
        help="Path of the server configuration file")

    return argv.pipe(
        ops.skip(1),
        argparse.parse(parser),
    )
Ejemplo n.º 7
0
def main():
    origin = rx.subjects.Subject()

    origin.pipe(
        ops.skip(20),
        ops.map(lambda x: x % 7),
        ops.filter(lambda x: x > 3),
    ).subscribe_(on_next=print)

    for x in range(37):
        origin.on_next(x)
Ejemplo n.º 8
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the operators
        :func:`skip <rx.operators.skip>`,
        :func:`skip_last <rx.operators.skip_last>`,
        :func:`take <rx.operators.take>`,
        :func:`take_last <rx.operators.take_last>` and
        :func:`filter <rx.operators.filter>`.

        The following diagram helps you remember how slices works with streams.
        Positive numbers are relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

        .. code::

            r---e---a---c---t---i---v---e---!
            0   1   2   3   4   5   6   7   8
           -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and _stop >= 0:
            pipeline.append(ops.take(_stop))

        if has_start and _start > 0:
            pipeline.append(ops.skip(_start))

        if has_start and _start < 0:
            pipeline.append(ops.take_last(abs(_start)))

        if has_stop and _stop < 0:
            pipeline.append(ops.skip_last(abs(_stop)))

        if has_step:
            if _step > 1:
                pipeline.append(
                    ops.filter_indexed(lambda x, i: i % _step == 0))
            elif _step < 0:
                # Reversing events is not supported
                raise TypeError('Negative step not supported.')

        return source.pipe(*pipeline)
Ejemplo n.º 9
0
    def __init__(self, scheduler=None):
        self._observerable = rx.interval(
            ObserveConfig.interval,
            scheduler).pipe(ops.map(lambda dummy: get_merge_requests()),
                            ops.retry(), ops.publish(), ops.ref_count())

        self._ready_to_merge = self._observerable.pipe(
            ops.map(lambda requests: next((request for request in requests if
                                           is_ready_to_merge(request)), None)),
            ops.start_with(None), ops.distinct_until_changed())

        self._ready_to_merge.subscribe(lambda ready_to_merge: logging.info(
            'Ready to merge: ' + str(ready_to_merge)))

        voted_merge_requests = self._observerable.pipe(
            ops.map(_to_voted_merge_requests_set))
        self._new_votes_merge_requests = voted_merge_requests.pipe(
            ops.skip(1), ops.zip(voted_merge_requests),
            ops.map(lambda zipped: zipped[0] - zipped[1]), ops.filter(len),
            ops.map(_to_merge_requests))

        self._new_votes_merge_requests.pipe(
            ops.map(lambda diff_set:
                    [merge_request.get_iid() for merge_request in diff_set])
        ).subscribe(
            lambda ids: logging.info(f'New votes for merge requests: {ids}'))

        awards = self._new_votes_merge_requests.pipe(ops.map(_to_awards_set),
                                                     ops.publish(),
                                                     ops.ref_count(),
                                                     ops.start_with(set()))
        self._new_awards = awards.pipe(
            ops.skip(1), ops.zip(awards),
            ops.map(lambda zipped: zipped[0] - zipped[1]), ops.filter(len),
            ops.flat_map(lambda diff_set: rx.from_iterable(diff_set)),
            ops.map(lambda award_key: award_key.award))

        self._new_awards.subscribe(
            lambda new_award: logging.info('New award: ' + str(new_award)))
Ejemplo n.º 10
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the
        operators skip(), skip_last(), take(), take_last() and filter().

        This marble diagram helps you remember how slices works with streams.
        Positive numbers is relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

         r---e---a---c---t---i---v---e---|
         0   1   2   3   4   5   6   7   8
        -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and stop >= 0:
            pipeline.append(ops.take(stop))

        if has_start and start > 0:
            pipeline.append(ops.skip(start))

        if has_start and start < 0:
            pipeline.append(ops.take_last(abs(start)))

        if has_stop and stop < 0:
            pipeline.append(ops.skip_last(abs(stop)))

        if has_step:
            if step > 1:
                pipeline.append(ops.filter_indexed(lambda x, i: i % step == 0))
            elif step < 0:
                # Reversing events is not supported
                raise TypeError("Negative step not supported.")

        return source.pipe(*pipeline)
Ejemplo n.º 11
0
    def slice(source: Observable) -> Observable:
        """The partially applied slice operator.

        Slices the given observable. It is basically a wrapper around the
        operators skip(), skip_last(), take(), take_last() and filter().

        This marble diagram helps you remember how slices works with streams.
        Positive numbers is relative to the start of the events, while negative
        numbers are relative to the end (close) of the stream.

         r---e---a---c---t---i---v---e---|
         0   1   2   3   4   5   6   7   8
        -8  -7  -6  -5  -4  -3  -2  -1   0

        Examples:
            >>> result = source.slice(1, 10)
            >>> result = source.slice(1, -2)
            >>> result = source.slice(1, -1, 2)

        Args:
            source: Observable to slice

        Returns:
            A sliced observable sequence.
        """

        if has_stop and stop >= 0:
            pipeline.append(ops.take(stop))

        if has_start and start > 0:
            pipeline.append(ops.skip(start))

        if has_start and start < 0:
            pipeline.append(ops.take_last(abs(start)))

        if has_stop and stop < 0:
            pipeline.append(ops.skip_last(abs(stop)))

        if has_step:
            if step > 1:
                pipeline.append(ops.filter_indexed(lambda x, i: i % step == 0))
            elif step < 0:
                # Reversing events is not supported
                raise TypeError("Negative step not supported.")

        return source.pipe(*pipeline)
Ejemplo n.º 12
0
    def play_step(self, step, cancel):
        interval = rx.interval(0.1)
        interval_steps = rx.just(step).pipe(
            ops.flat_map(lambda step: interval.pipe(ops.map(lambda _: step))))

        step_done = interval_steps.pipe(
            ops.filter(lambda step: self.player.position() >= step.step_end),
            ops.do_action(
                lambda step: self.player.set_position(step.loop_start)),
            ops.take(1))

        loop_done = interval_steps.pipe(
            ops.filter(lambda step: self.player.position() >= step.loop_end),
            ops.do_action(
                lambda step: self.player.set_position(step.loop_start)),
            ops.take_until(cancel.pipe(ops.skip(1))))

        return step_done.pipe(ops.merge(loop_done))
Ejemplo n.º 13
0
    "edge": edge_subject,
    "node": node_subject,
    "graph": graph_subject
}
local_subscriber = partial(subscriber, subscribe_map)

# base_obs = rx.from_(open("streamTest.txt"))
base_obs = rx.from_(sys.stdin)

c = ConnectableObservable(base_obs, Subject())
dict_delimiter_subject = Subject()
ti = time.time()
c.pipe(op.filter(lambda line: '}' in line or '{' in line),
       op.map(lambda line: True)).subscribe(dict_delimiter_subject)

c.pipe(
    op.buffer(dict_delimiter_subject),
    op.skip(1),
    op.map(lambda lines: "".join(lines).replace('"', '\"')),
    op.filter(lambda line: '}' not in line),
    op.map(lambda line: "{}{}".format(line, "}")),
    op.map(lambda json_str: json.loads(json_str)),
    op.map(lambda dic: dict_to_dict_with_set(dic)),
    # op.take(20),
    op.group_by(lambda dic: get_dict_type(dic)),
).subscribe(local_subscriber)

print("Start stream time: {}".format(str(time.time() - ti)))
c.connect()
print("Finish time: {}".format(str(time.time() - ti)))
Ejemplo n.º 14
0
 def create():
     return xs.pipe(ops.skip(10))
Ejemplo n.º 15
0
import rx
import rx.operators as ops

numbers = rx.from_([1, 2, 3, 4, 5]).pipe(ops.skip(2))

numbers.subscribe(on_next=lambda i: print("on_next {}".format(i)),
                  on_error=lambda e: print("on_error: {}".format(e)),
                  on_completed=lambda: print("on_completed"))
Ejemplo n.º 16
0
def audio_encoder(sources):
    # Parse configuration
    parser = create_arg_parser()

    read_request, read_response = sources.argv.argv.pipe(
        ops.skip(1),
        argparse.parse(parser),
        ops.filter(lambda i: i.key == 'config'),
        ops.map(lambda i: file.Read(id='config', path=i.value)),
        file.read(sources.file.response),
    )
    config = read_response.pipe(
        ops.filter(lambda i: i.id == "config"),
        ops.flat_map(lambda i: i.data),
        parse_config,
    )

    # Transcode request handling
    encode_init = config.pipe(
        ops.map(
            lambda i: encoder.Initialize(storage_path=i.encode.storage_path)))

    encode_request = sources.httpd.route.pipe(
        ops.filter(lambda i: i.id == 'flac_transcode'),
        ops.flat_map(lambda i: i.request),
        ops.do_action(lambda i: print("[{}]http req: {}".format(
            datetime.datetime.now(), threading.get_ident()))),
        #.observe_on(encode_scheduler)
        ops.flat_map(lambda i: Observable.just(i, encode_scheduler)),
        ops.do_action(lambda i: print("[{}]encode req: {}".format(
            datetime.datetime.now(), threading.get_ident()))),
        ops.map(lambda i: encoder.EncodeMp3(
            id=i.context, data=i.data, key=i.match_info['key'])),
    )
    encoder_request = rx.merge(encode_init, encode_request)

    # store encoded file
    store_requests = sources.encoder.response.pipe(
        ops.do_action(lambda i: print("[{}]encode res: {}".format(
            datetime.datetime.now(), threading.get_ident()))),
        ops.observe_on(s3_scheduler),
        ops.do_action(lambda i: print("[{}]s3 req: {}".format(
            datetime.datetime.now(), threading.get_ident()))),
        ops.map(lambda i: s3.UploadObject(
            key=i.key + '.flac',
            data=i.data,
            id=i.id,
        )),
    )

    # acknowledge http request
    http_response = sources.s3.response.pipe(
        ops.do_action(lambda i: print("[{}]s3 res: {}".format(
            datetime.datetime.now(), threading.get_ident()))),
        ops.do_action(
            lambda i: print("httpd res: {}".format(threading.get_ident()))),
        ops.map(lambda i: httpd.Response(
            data='ok'.encode('utf-8'),
            context=i.id,
        )),
    )

    # http server
    http_init = config.pipe(
        ops.flat_map(lambda i: rx.from_([
            httpd.Initialize(request_max_size=0),
            httpd.AddRoute(
                methods=['POST'],
                path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}',
                id='flac_transcode',
            ),
            httpd.StartServer(host=i.server.http.host, port=i.server.http.port
                              ),
        ])))
    http = rx.merge(http_init, http_response)

    # s3 database
    s3_init = config.pipe(
        ops.map(lambda i: s3.Configure(
            access_key=i.s3.access_key,
            secret_key=i.s3.secret_key,
            bucket=i.s3.bucket,
            endpoint_url=i.s3.endpoint_url,
            region_name=i.s3.region_name,
        )))

    # merge sink requests
    file_requests = read_request
    s3_requests = rx.merge(s3_init, store_requests)

    return Sink(
        encoder=encoder.Sink(request=encoder_request),
        s3=s3.Sink(request=s3_requests),
        file=file.Sink(request=file_requests),
        httpd=httpd.Sink(control=http),
    )
Ejemplo n.º 17
0
 def _load(source):
     return source.pipe(
         ops.skip(skip),
         ops.map(parse_line),
         ops.filter(lambda i: i is not None),
     )
Ejemplo n.º 18
0
 def mapper(_xs):
     return _xs.pipe(
             ops.zip(_xs.pipe(ops.skip(1))),
             ops.map(sum)
             )
Ejemplo n.º 19
0
 def mapper(_xs):
     return _xs.pipe(ops.zip(_xs.pipe(ops.skip(1))), ops.map(sum))
Ejemplo n.º 20
0
def test_tuple_with_previous_using_scan():
    rx.from_iterable(range(10)).pipe(ops.start_with((-1, -1)),
                                     ops.scan(lambda tup, y: (y, tup[0])),
                                     ops.skip(2)).subscribe(lambda x: print(x))
def audio_encoder(sources):
    # Parse configuration
    parser = create_arg_parser()

    parsed_argv = sources.argv.argv.pipe(
        ops.skip(1),
        argparse.parse(parser),
        ops.filter(lambda i: i.key == 'config'),
        ops.subscribe_on(aio_scheduler),
        ops.share(),
    )

    # monitor and parse config file
    monitor_init = parsed_argv.pipe(
        ops.flat_map(lambda i: rx.from_([
            inotify.AddWatch(
                id='config', path=i.value, flags=aionotify.Flags.MODIFY),
            inotify.Start(),
        ])))

    config_update = sources.inotify.response.pipe(
        ops.debounce(5.0, scheduler=aio_scheduler),
        ops.map(lambda i: True),
        ops.start_with(True),
    )

    read_request, read_response = rx.combine_latest(
        parsed_argv, config_update).pipe(
            ops.starmap(
                lambda config, _: file.Read(id='config', path=config.value)),
            file.read(sources.file.response),
        )

    config = read_response.pipe(
        ops.filter(lambda i: i.id == "config"),
        ops.flat_map(lambda i: i.data),
        parse_config,
    )

    # Transcode request handling
    encode_init = config.pipe(
        ops.map(lambda i: i.encode),
        ops.distinct_until_changed(),
        ops.map(lambda i: encoder.Configure(samplerate=i.samplerate,
                                            bitdepth=i.bitdepth)),
    )

    encode_request = sources.httpd.route.pipe(
        ops.filter(lambda i: i.id == 'flac_transcode'),
        ops.flat_map(lambda i: i.request),
        ops.flat_map(lambda i: rx.just(i, encode_scheduler)),
        ops.map(lambda i: encoder.EncodeMp3(
            id=i.context, data=i.data, key=i.match_info['key'])),
    )
    encoder_request = rx.merge(encode_init, encode_request)

    # store encoded file
    store_requests = sources.encoder.response.pipe(
        ops.observe_on(s3_scheduler),
        ops.map(lambda i: s3.UploadObject(
            key=i.key + '.flac',
            data=i.data,
            id=i.id,
        )),
    )

    # acknowledge http request
    http_response = sources.s3.response.pipe(
        ops.map(lambda i: httpd.Response(
            data='ok'.encode('utf-8'),
            context=i.id,
        )))

    # http server
    http_init = config.pipe(
        ops.take(1),
        ops.flat_map(lambda i: rx.from_([
            httpd.Initialize(request_max_size=0),
            httpd.AddRoute(
                methods=['POST'],
                path='/api/transcode/v1/flac/{key:[a-zA-Z0-9-\._]*}',
                id='flac_transcode',
            ),
            httpd.StartServer(host=i.server.http.host, port=i.server.http.port
                              ),
        ])),
    )
    http = rx.merge(http_init, http_response)

    # s3 database
    s3_init = config.pipe(
        ops.take(1),
        ops.map(lambda i: s3.Configure(
            access_key=i.s3.access_key,
            secret_key=i.s3.secret_key,
            bucket=i.s3.bucket,
            endpoint_url=i.s3.endpoint_url,
            region_name=i.s3.region_name,
        )),
    )

    # merge sink requests
    file_requests = read_request
    s3_requests = rx.merge(s3_init, store_requests)

    return Sink(
        encoder=encoder.Sink(request=encoder_request),
        s3=s3.Sink(request=s3_requests),
        file=file.Sink(request=file_requests),
        httpd=httpd.Sink(control=http),
        inotify=inotify.Sink(request=monitor_init),
    )
"""Transformation"""
op.buffer()
op.group_by()
op.map()
op.scan()
# ...

"""Filtering"""
op.debounce()
op.distinct()
op.filter()
op.element_at()
op.first()
op.ignore_elements()
op.last()
op.skip()
op.skip_last()
op.take()
op.take_last()
# ...

"""Error Handling"""
op.catch()
op.retry()

"""Utility"""
op.delay()
op.materialize()
op.time_interval()
op.timeout()
op.timestamp()
Ejemplo n.º 23
0
def echo(sources):
    console = sources.argv.argv.pipe(ops.skip(1), ops.map(lambda i: i + '\n'))

    return Sink(stdout=stdout.Sink(data=console))
Ejemplo n.º 24
0
def test_tuple_with_previous_using_zip():
    from_range = rx.from_iterable(range(10))
    from_range.pipe(
        ops.skip(1),
        ops.zip(from_range),
    ).subscribe(lambda x: print(x))
Ejemplo n.º 25
0
 def create():
     return xs.pipe(ops.skip(0))