示例#1
0
文件: test_let.py 项目: AlexMost/RxPY
    def test_let_calls_function_immediately(self):
        called = [False]

        def func(x):
            called[0] = True
            return x

        Observable.empty().let_bind(func)
        assert(called[0])
示例#2
0
文件: test_let.py 项目: xyicheng/RxPY
    def test_let_calls_function_immediately(self):
        called = [False]

        def func(x):
            called[0] = True
            return x

        Observable.empty().let(func)
        assert (called[0])
示例#3
0
    def test_merge_empty2(self):
        scheduler = TestScheduler()
        e1 = Observable.empty()
        e2 = Observable.empty()

        def create():
            return Observable.merge(scheduler, e1, e2)

        results = scheduler.start(create)
        results.messages.assert_equal(on_completed(203))
示例#4
0
    def test_merge_empty2(self):
        scheduler = TestScheduler()
        e1 = Observable.empty()
        e2 = Observable.empty()

        def create():
            return Observable.merge(scheduler, e1, e2)

        results = scheduler.start(create)
        results.messages.assert_equal(on_completed(203))
示例#5
0
    def test_merge_empty2(self):
        scheduler = TestScheduler()
        e1 = Observable.empty()
        e2 = Observable.empty()

        def create():
            return Observable.merge(e1, e2)

        results = scheduler.start(create)
        assert results.messages == [on_completed(200)]
示例#6
0
 def create():
     return xs.group_join(
         ys, lambda x: Observable.timer(x.interval).flat_map(
             Observable.throw(ex)
             if x.value == 6 else Observable.empty()),
         lambda y: Observable.timer(y.interval), lambda x, yy: yy.map(
             lambda y: str(x.value) + y.value)).merge_all()
示例#7
0
 def create():
     return xs.join(ys,
         lambda x: Observable.timer(x.interval),
         lambda y: Observable.timer(y.interval).flat_map(
             Observable.throw(ex) if y.value == "tin" else Observable.empty()),
         lambda x, y: str(x.value) + y.value
     )
示例#8
0
 def default_resolver(root, info):
     # type: (Any, ResolveInfo) -> Union[Observable, Subject]
     func = getattr(root, "importantEmail", None)
     if func:
         func = get_unbound_function(func)
         return func()
     return Observable.empty()
示例#9
0
文件: ifthen.py 项目: jesonjn/RxPY
    def if_then(cls, condition, then_source, else_source=None, scheduler=None):
        """Determines whether an observable collection contains values. 
        
        Example:
        1 - res = rx.Observable.if(condition, obs1)
        2 - res = rx.Observable.if(condition, obs1, obs2)
        3 - res = rx.Observable.if(condition, obs1, scheduler=scheduler)

        Keyword parameters:
        condition -- {Function} The condition which determines if the 
            then_source or else_source will be run.
        then_source -- {Observable} The observable sequence or Promise that 
            will be run if the condition function returns true.
        else_source -- {Observable} [Optional] The observable sequence or 
            Promise that will be run if the condition function returns False. 
            If this is not provided, it defaults to Rx.Observabe.Empty
        scheduler -- [Optional] Scheduler to use.
   
        Returns an observable {Observable} sequence which is either the 
        then_source or else_source."""

        else_source = else_source or Observable.empty(scheduler=scheduler)

        then_source = Observable.from_future(then_source)
        else_source = Observable.from_future(else_source)

        def factory():

            return then_source if condition() else else_source

        return Observable.defer(factory)
示例#10
0
 def __call__(self, e):
     self.buffer += e
     if self.delimiter not in self.buffer:
         return Observable.empty()
     packages = self.buffer.split(self.delimiter)
     self.buffer = packages[-1]
     return Observable.from_(packages[:-1])
示例#11
0
 def create():
     return xs.join(ys,
         lambda x: Observable.timer(x.interval, scheduler=scheduler),
         lambda y: Observable.timer(y.interval, scheduler=scheduler).select_many(
             Observable.throw_exception(ex) if y.value == "tin" else Observable.empty()),
         lambda x, y: str(x.value) + y.value
     )
示例#12
0
文件: case.py 项目: AlexMost/RxPY
def case(cls, selector, sources, default_source=None, scheduler=None):
    """Uses selector to determine which source in sources to use.
    There is an alias 'switch_case'.

    Example:
    1 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 })
    2 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 }, obs0)
    3 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 },
                                 scheduler=scheduler)

    Keyword arguments:
    :param types.FunctionType selector: The function which extracts the value
        for to test in a case statement.
    :param list sources: A object which has keys which correspond to the case
        statement labels.
    :param Observable default_source: The observable sequence or Promise that
        will be run if the sources are not matched. If this is not provided, it
        defaults to rx.Observabe.empty with the specified scheduler.

    :returns: An observable sequence which is determined by a case statement.
    :rtype: Observable
    """

    default_source = default_source or Observable.empty(scheduler=scheduler)

    def factory():
        try:
            result = sources[selector()]
        except KeyError:
            result = default_source

        result = Observable.from_future(result)

        return result
    return Observable.defer(factory)
示例#13
0
文件: ifthen.py 项目: jesonjn/RxPY
    def if_then(cls, condition, then_source, else_source=None, scheduler=None):
        """Determines whether an observable collection contains values. 
        
        Example:
        1 - res = rx.Observable.if(condition, obs1)
        2 - res = rx.Observable.if(condition, obs1, obs2)
        3 - res = rx.Observable.if(condition, obs1, scheduler=scheduler)

        Keyword parameters:
        condition -- {Function} The condition which determines if the 
            then_source or else_source will be run.
        then_source -- {Observable} The observable sequence or Promise that 
            will be run if the condition function returns true.
        else_source -- {Observable} [Optional] The observable sequence or 
            Promise that will be run if the condition function returns False. 
            If this is not provided, it defaults to Rx.Observabe.Empty
        scheduler -- [Optional] Scheduler to use.
   
        Returns an observable {Observable} sequence which is either the 
        then_source or else_source."""
        
        else_source = else_source or Observable.empty(scheduler=scheduler)
        
        then_source = Observable.from_future(then_source)
        else_source = Observable.from_future(else_source)
        
        def factory():
            
            return then_source if condition() else else_source
        return Observable.defer(factory)
示例#14
0
 def create():
     return xs.group_join(
         ys, lambda x: Observable.timer(
             x.interval, scheduler=scheduler).select_many(
                 Observable.throw_exception(ex)
                 if x.value == 6 else Observable.empty()),
         lambda y: Observable.timer(y.interval, scheduler=scheduler),
         lambda x, yy: yy.select(lambda y: str(x.value) + y.value
                                 )).merge_observable()
示例#15
0
 def task_from_file(file_name):
     try:
         with open(file_name) as data_file:
             script = json.load(data_file)
             # parse, clean, and push words in text file
             return Observable.from_(script).distinct(lambda t: t['timestamp'])
     except Exception, Argument:
         print(Argument)
         return Observable.empty()
def test_empty_observer_throw_exception():
    scheduler = TestScheduler()
    xs = Observable.empty(scheduler)
    xs.subscribe(lambda x: None, lambda ex: None, lambda: _raise('ex'))
    
    try:
        return scheduler.start()
    except RxException:
        pass
def test_empty_observer_throw_exception():
    scheduler = TestScheduler()
    xs = Observable.empty(scheduler)
    xs.subscribe(lambda x: None, lambda ex: None, lambda: _raise('ex'))

    try:
        return scheduler.start()
    except RxException:
        pass
示例#18
0
 def __call__(self, command: Command, handler=lambda x: None) -> Observable:
     try:
         result = command.exec(self.shared_manager)
         if isinstance(result, Observable):
             return result
         return Observable.just(result)
     except Exception as ex:
         handler(ex)
         return Observable.empty()
示例#19
0
def get_pipeline_run_observable(graphene_info, run_id, after=None):
    check.inst_param(graphene_info, 'graphene_info', ResolveInfo)
    check.str_param(run_id, 'run_id')
    check.opt_int_param(after, 'after')
    instance = graphene_info.context.instance
    run = instance.get_run(run_id)

    if not run:

        def _get_error_observable(observer):
            observer.on_next(
                graphene_info.schema.
                type_named('PipelineRunLogsSubscriptionFailure')(
                    missingRunId=run_id,
                    message='Could not load run with id {}'.format(run_id)))

        return Observable.create(_get_error_observable)  # pylint: disable=E1101

    if not instance.can_watch_events:

        def _get_error_observable(observer):
            observer.on_next(
                graphene_info.schema.type_named(
                    'PipelineRunLogsSubscriptionFailure')
                (message=
                 'Event log storage on current DagsterInstance is not watchable.'
                 ))

        return Observable.create(_get_error_observable)  # pylint: disable=E1101

    pipeline = get_dauphin_pipeline_reference_from_selector(
        graphene_info, run.selector)

    from ..schema.pipelines import DauphinPipeline

    if not isinstance(pipeline, DauphinPipeline):
        return Observable.empty()  # pylint: disable=no-member

    execution_plan = create_execution_plan(pipeline.get_dagster_pipeline(),
                                           run.environment_dict,
                                           RunConfig(mode=run.mode))

    # pylint: disable=E1101
    return Observable.create(
        PipelineRunObservableSubscribe(
            instance, run_id, after_cursor=after)).map(
                lambda events: graphene_info.schema.
                type_named('PipelineRunLogsSubscriptionSuccess')(
                    runId=run_id,
                    messages=[
                        from_event_record(graphene_info, event, pipeline,
                                          execution_plan) for event in events
                    ],
                ))
示例#20
0
    def _logErrorAndResume(self, error, task):

        self.logger.error("Failed processing task {}. Reason: {}:{}".format(
            task, error.__class__.__name__, error))
        try:
            if error.__class__.__name__ == QueryProcessingException.__name__:
                self._notify_task(task, "ERROR", error.get_status())
            else:
                self._notify_task(task, "ERROR")
        except:
            traceback.print_exc()

        return Observable.empty()
示例#21
0
def run():
    raw_image = sys.stdin.read(360 * 640 * 3)

    image = np.fromstring(raw_image, dtype='uint8')
    image = image.reshape((360, 640, 3))

    if check_startscreen(image):
        return Observable.just('startscreen')

    p1winner = check_winner(image, 207)
    p2winner = check_winner(image, 357)
    if p1winner or p2winner:
        return Observable.just('winner is ' + ('p1' if p1winner else 'p2'))

    return Observable.empty()
示例#22
0
def take(self, count, scheduler=None):
    """Returns a specified number of contiguous elements from the start of
    an observable sequence, using the specified scheduler for the edge case
    of take(0).

    1 - source.take(5)
    2 - source.take(0, rx.Scheduler.timeout)

    Keyword arguments:
    count -- The number of elements to return.
    scheduler -- [Optional] Scheduler used to produce an OnCompleted
        message in case count is set to 0.

    Returns an observable sequence that contains the specified number of
    elements from the start of the input sequence.
    """

    if count < 0:
        raise ArgumentOutOfRangeException()

    if not count:
        return Observable.empty(scheduler)

    observable = self

    def subscribe(observer):
        remaining = [count]

        def on_next(value):
            if remaining[0] > 0:
                remaining[0] -= 1
                observer.on_next(value)
                if not remaining[0]:
                    observer.on_completed()

        return observable.subscribe(on_next, observer.on_error,
                                    observer.on_completed)

    return AnonymousObservable(subscribe)
示例#23
0
文件: case.py 项目: jesonjn/RxPY
    def case(cls, selector, sources, default_source=None, scheduler=None):
        """Uses selector to determine which source in sources to use.
        There is an alias 'switch_case'.

        Example:
        1 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 })
        2 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 }, obs0)
        3 - res = rx.Observable.case(selector, { '1': obs1, '2': obs2 },
                                     scheduler=scheduler)

        Keyword arguments:
        selector -- {Function} The function which extracts the value for to
            test in a case statement.
        sources -- {Array} A object which has keys which correspond to the case
            statement labels.
        default_source-- {Observable} [Optional] The observable sequence or
            Promise that will be run if the sources are not matched. If this is
            not provided, it defaults to Rx.Observabe.empty with the specified
            scheduler.

        Returns an observable {Observable} sequence which is determined by a
        case statement."""

        default_source = default_source or Observable.empty(
            scheduler=scheduler)

        def factory():
            try:
                result = sources[selector()]
            except KeyError:
                result = default_source

            result = Observable.from_future(result)

            return result

        return Observable.defer(factory)
示例#24
0
文件: take.py 项目: jesonjn/RxPY
    def take(self, count, scheduler=None):
        """Returns a specified number of contiguous elements from the start of
        an observable sequence, using the specified scheduler for the edge case
        of take(0).

        1 - source.take(5)
        2 - source.take(0, rx.Scheduler.timeout)

        Keyword arguments:
        count -- The number of elements to return.
        scheduler -- [Optional] Scheduler used to produce an OnCompleted
            message in case count is set to 0.

        Returns an observable sequence that contains the specified number of
        elements from the start of the input sequence.
        """

        if count < 0:
            raise ArgumentOutOfRangeException()

        if not count:
            return Observable.empty(scheduler)

        observable = self
        def subscribe(observer):
            remaining = [count]

            def on_next(value):
                if remaining[0] > 0:
                    remaining[0] -= 1
                    observer.on_next(value)
                    if not remaining[0]:
                        observer.on_completed()

            return observable.subscribe(on_next, observer.on_error, observer.on_completed)
        return AnonymousObservable(subscribe)
示例#25
0
 def on_error(e):
     logger.error("fail to fetch nodes by url: %s, %s", hub_url, str(e))
     return Observable.empty()
示例#26
0
 def create():
     return Observable.empty(scheduler).throttle(10, scheduler)
示例#27
0
 def create():
     return Observable.empty(scheduler).time_interval(
         scheduler=scheduler)
示例#28
0
 def wrapper(*args, **kwargs):
     try:
         return Observable.just(map_function(*args, **kwargs))
     except Exception as ex:
         handler(ex)
         return Observable.empty()
示例#29
0
文件: test_join.py 项目: jesonjn/RxPY
 def right_duration_selector(y):
     if len(y.value) >= 0:
         raise Exception(ex)
     else:
         return Observable.empty()
示例#30
0
from rx import Observable

Observable.empty() \
    .subscribe(on_next=lambda s: print(s),
               on_completed=lambda: print("Done!")
               )
示例#31
0
def comment(text):
    if True:
        obs = commentObservable(Observable.from_iterable(text.splitlines())) if text else Observable.empty()
        return obs.reduce(lambda acc, line: acc + line + "\n", "")
    else:
        return "r"
示例#32
0
 def test_for_each_empty(self):
     lst = []
     Observable.empty().to_blocking().for_each(lambda x: lst.append(x))
     assert(lst == [])
示例#33
0
 def create():
     return Observable.empty(scheduler=scheduler).sample(0, scheduler=scheduler)
示例#34
0
def create_app():
    def setup_logging():
        handler = StreamHandler(stream=sys.stdout)
        handler.setLevel(config.log_level)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        flask_app.logger.addHandler(handler)
        flask_app.logger.setLevel(config.log_level)
        flask_app.logger.name = "docker_enforcer"

    flask_app = Flask(__name__)
    if not flask_app.debug:
        setup_logging()
    flask_app.logger.info(
        "Starting docker-enforcer v{0} with docker socket {1}".format(
            config.version, config.docker_socket))

    task_scheduler = NewThreadScheduler()
    # task_scheduler = ThreadPoolScheduler(multiprocessing.cpu_count())
    if config.run_start_events:
        events = Observable.from_iterable(docker_helper.get_events_observable()) \
            .observe_on(scheduler=task_scheduler) \
            .where(lambda e: is_configured_event(e)) \
            .map(lambda e: e['id']) \
            .map(lambda cid: docker_helper.check_container(cid, CheckSource.Event, remove_from_cache=True))

    if config.run_periodic:
        periodic = Observable.interval(config.interval_sec * 1000)

        if config.immediate_periodical_start:
            flask_app.logger.debug("Run periodic immediately")
            periodic = periodic.start_with(-1)

        periodic = periodic.observe_on(scheduler=task_scheduler) \
            .map(lambda _: docker_helper.check_containers(CheckSource.Periodic)) \
            .flat_map(lambda c: c)

    detections = Observable.empty()
    if config.run_start_events:
        detections = detections.merge(events)
    if config.run_periodic:
        detections = detections.merge(periodic)

    verdicts = detections \
        .map(lambda container: judge.should_be_killed(container)) \
        .where(lambda v: v.verdict)

    threaded_verdicts = verdicts \
        .retry() \
        .subscribe_on(task_scheduler) \
        .publish() \
        .auto_connect(2)

    if not config.run_start_events and not config.run_periodic:
        flask_app.logger.info(
            "Neither start events or periodic checks are enabled. Docker Enforcer will be working in "
            "authz plugin mode only.")
    else:
        killer_subs = threaded_verdicts.subscribe(jurek)
        trigger_subs = threaded_verdicts.subscribe(trigger_handler)

    def on_exit(sig, frame):
        flask_app.logger.info("Stopping docker monitoring")
        if config.run_start_events or config.run_periodic:
            killer_subs.dispose()
            trigger_subs.dispose()
        flask_app.logger.debug("Complete, ready to finish")
        quit()

    signal.signal(signal.SIGINT, on_exit)
    signal.signal(signal.SIGTERM, on_exit)

    return flask_app
 def factory():
     return Observable.empty(scheduler)
示例#36
0
 def create():
     return Observable.empty(scheduler).debounce(10, scheduler)
示例#37
0
 def on_error(e):
     logger.error("fail to fetch appium sessions by url: %s, %s",
                  appium_url, str(e))
     return Observable.empty()
示例#38
0
 def test_For_each_index_empty(self):
     lstX = []
     Observable.empty().to_blocking().for_each(lambda x, i: lstX.append(x))
     assert(lstX == [])
示例#39
0
 def create2():
     Observable.empty().transduce(
         compose(
             filtering(even), mapping(mul10))
         ).subscribe(noop, noop, throw_error)
示例#40
0
文件: test_join.py 项目: jesonjn/RxPY
 def left_duration_selector(x):
     if x.value >= 0:
         raise Exception(ex)
     else:
         return Observable.empty()
示例#41
0
 def right_duration_selector(y):
     if len(y.value) >= 0:
         raise Exception(ex)
     else:
         return Observable.empty()
示例#42
0
 def create2():
     Observable.empty().transduce(
         compose(filtering(even),
                 mapping(mul10))).subscribe_(noop, noop, throw_error)
示例#43
0
 def create():
     return Observable.empty(scheduler).time_interval(scheduler)
示例#44
0
 def observable_window_with_openings(window_openings, window_closing_selector):
     return self.window_openings.group_join(window_closing_selector, lambda: Observable.empty(), lambda _, window: window)
示例#45
0
 def left_duration_selector(x):
     if x.value >= 0:
         raise Exception(ex)
     else:
         return Observable.empty()
示例#46
0
 def default_resolver(root, info):
     func = getattr(root, 'importantEmail', None)
     if func:
         func = get_unbound_function(func)
         return func()
     return Observable.empty()
 def catch_item(i):
     sink_observer.on_next(convert(i))
     return Observable.empty()
示例#48
0
 def on_error(exception=None):
     if isinstance(exception, HttpResponse):
         return Observable.just(exception)
     if exception is not None:
         logger.debug('unexpected error %r', exception)
     return Observable.empty()
示例#49
0
 def create():
     return xs.group_join(ys,
         lambda x: Observable.timer(x.interval, scheduler=scheduler).select_many(Observable.throw_exception(ex) if x.value==6 else Observable.empty()),
         lambda y: Observable.timer(y.interval, scheduler=scheduler),
         lambda x, yy: yy.select(lambda y: str(x.value) + y.value)
     ).merge_observable()