Exemplo n.º 1
0
class WSHandler(WebSocketHandler):
    def open(self):
        print("WebSocket opened")

        # A Subject is both an observable and observer, so we can both subscribe
        # to it and also feed (on_next) it with new values
        self.stream = Subject()

        # Get all distinct key up events from the input and only fire if long enough and distinct
        query = (
            self.stream.map(lambda x: x["term"])
            .filter(lambda text: len(text) > 2)  # Only if the text is longer than 2 characters
            .debounce(0.750, scheduler=scheduler)  # Pause for 750ms
            .distinct_until_changed()
        )  # Only if the value has changed

        searcher = query.flat_map_latest(search_wikipedia)

        def send_response(x):
            self.write_message(x.body)

        def on_error(ex):
            print(ex)

        searcher.subscribe(send_response, on_error)

    def on_message(self, message):
        obj = json_decode(message)
        self.stream.on_next(obj)

    def on_close(self):
        print("WebSocket closed")
Exemplo n.º 2
0
class WSHandler(WebSocketHandler):
    def open(self):
        print("WebSocket opened")

        # A Subject is both an observable and observer, so we can both subscribe
        # to it and also feed (on_next) it with new values
        self.subject = Subject()

        # Now we take on our magic glasses and project the stream of bytes into
        # a ...
        query = self.subject.map(
            lambda obj: obj["keycode"]  # 1. stream of keycodes
        ).window_with_count(
            10,
            1  # 2. stream of windows (10 ints long)
        ).select_many(
            # 3. stream of booleans, True or False
            lambda win: win.sequence_equal(codes)).filter(
                lambda equal: equal  # 4. stream of Trues
            )
        # 4. we then subscribe to the Trues, and signal Konami! if we see any
        query.subscribe(lambda x: self.write_message("Konami!"))

    def on_message(self, message):
        obj = json_decode(message)
        self.subject.on_next(obj)

    def on_close(self):
        print("WebSocket closed")
Exemplo n.º 3
0
class WSHandler(WebSocketHandler):
    def open(self):
        print("WebSocket opened")

        # A Subject is both an observable and observer, so we can both subscribe
        # to it and also feed (send) it with new values
        self.stream = Subject()

        # Get all distinct key up events from the input and only fire if long enough and distinct
        query = (
            self.stream.map(lambda x: x["term"]).filter(
                lambda text: len(text) > 2
            )  # Only if the text is longer than 2 characters
            .debounce(0.750)  # Pause for 750ms
            .distinct_until_changed()  # Only if the value has changed
        )
        searcher = query.flat_map_latest(search_wikipedia)

        def send_response(x):
            self.write_message(x.body)

        def on_error(ex):
            print(ex)

        searcher.subscribe_(send_response, on_error, scheduler=scheduler)

    def on_message(self, message):
        obj = json_decode(message)
        self.stream.on_next(obj)

    def on_close(self):
        print("WebSocket closed")
Exemplo n.º 4
0
class WSHandler(WebSocketHandler):
    def open(self):
        print("WebSocket opened")

        # A Subject is both an observable and observer, so we can both subscribe
        # to it and also feed (on_next) it with new values
        self.subject = Subject()

        # Now we take on our magic glasses and project the stream of bytes into
        # a ...
        query = self.subject.map(
                lambda obj: obj["keycode"] # 1. stream of keycodes
            ).window_with_count(
                10, 1 # 2. stream of windows (10 ints long)
            ).select_many(
                # 3. stream of booleans, True or False
                lambda win: win.sequence_equal(codes)
            ).filter(
                lambda equal: equal # 4. stream of Trues
            )
        # 4. we then subscribe to the Trues, and signal Konami! if we see any
        query.subscribe(lambda x: self.write_message("Konami!"))

    def on_message(self, message):
        obj = json_decode(message)
        self.subject.on_next(obj)

    def on_close(self):
        print("WebSocket closed")
Exemplo n.º 5
0
# -*- coding: utf-8 -*-
import logging

from rx.subjects import Subject

from src.core.models import ChatRoomMessage

logger = logging.getLogger(__name__)

# 收到微信的原始 dict 数据
raw_stream = Subject()

# 收指定微信群的数据
stream = raw_stream.map(lambda message_data: ChatRoomMessage(message_data)
                        ).filter(lambda message: message.chatroom)


def subscribe(observable):
    """ 订阅装饰器,cls订阅observable """
    def wrapper(cls):
        observable.subscribe(cls())
        return cls

    return wrapper


def no_exception(func):
    """ 无异常装饰器,直接打log """
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
Exemplo n.º 6
0
class OptiRx(object):
    def __init__(self, chr_contr, executor, gen0=None, opts=default_options):
        self.gen0 = gen0
        self.chr_contr = chr_contr
        self.pauser = Pauser()
        self.executor = executor
        self.executor.kpp_fun = self.pauser.kpp
        self.sub_chr_best = ReplaySubject()
        self.sub_gen_solved = Subject()
        self.sub_gen_unsolved = Subject()
        self.subj_done = Subject()

        self.su_genetic = StepUpGenetic(chr_contr)

        self.opts = dict(default_options)
        self.init_opts(opts)
        self.stop_flag = True
        self.gen_flag = True
        self.init_logic()

    def init_opts(self, opts):
        for k in opts:
            self.opts[k] = opts[k]
        self.su_genetic.pop_count = self.opts['pop_count']
        self.su_genetic.prob_mut = self.opts['prob_mut']
        self.su_genetic.prob_mut_gene = self.opts['prob_mut_gene']
        self.su_genetic.prob_cross = self.opts['prob_cross']
        self.su_genetic.elite_count = self.opts['elite_count']

    def init_logic(self):
        # (fit, op)...
        def best_fun(gener):
            fit, op = gener.get_best(True)
            return gener.num_g, fit, op

        best_stream = self.sub_gen_solved.map(best_fun).publish()
        improve_best = best_stream.distinct(lambda tp: tp[1])
        improve_best.subscribe(self.sub_chr_best)

        def wind_switch(beep_inds):
            beep_set = set(beep_inds)

            def inner(s):
                inds = Observable.interval(0)
                return s.zip(inds, lambda x, ind: ind) \
                    .filter(lambda ind: ind in beep_set)

            return inner

        switch = best_stream\
            .window(improve_best.skip(1))\
            .flat_map(wind_switch([self.opts['n_gener_switch'], self.opts['n_gener_done']-2]))

        done = best_stream\
            .window(improve_best.skip(1))\
            .flat_map(wind_switch([self.opts['n_gener_done']]))

        def stop(ind):
            self.stop_flag = True

        def switch_f(ind):
            self.gen_flag = False

        switch.subscribe(switch_f)
        done.subscribe(stop)
        best_stream.skip(self.opts['n_gener_max'] - 1).subscribe(stop)

        best_stream.connect()

    def paused(self):
        return self.pauser.paused

    def pause(self):
        self.pauser.pause()

    def unpause(self):
        self.pauser.play()

    def run_sync(self):
        if self.gen0:
            gen0 = self.gen0
        else:
            gen0 = Generation(self.chr_contr, 0)
            gen0.get_init_pop(self.opts['pop_count'], self.opts['seed0'])
        self.stop_flag = False
        self.gen_flag = False
        while not self.stop_flag:
            self.sub_gen_unsolved.on_next(gen0)
            if self.gen_flag:
                gen0 = self.stepup_gen(gen0)
            else:
                gen0 = self.stepup_slsqp(gen0)
                self.gen_flag = True
        self.subj_done.on_completed()

    def run(self):
        cs1 = threading.Thread(name='calc_thread', target=self.run_sync)
        cs1.start()

    def stepup_gen(self, gen, seed=None):
        self.calc_gen(gen)
        self.sub_gen_solved.on_next(gen)
        gen_dict = self.su_genetic.step_up(gen.pop, seed)
        gen2 = Generation(self.chr_contr, gen.num_g + 1, gen_dict)
        return gen2

    def calc_gen(self, gen):
        fitnessless = gen.get_fitlessness()
        if len(fitnessless) == 0:
            return
        tick_tack = ReplaySubject()
        fset = seq(fitnessless) \
            .map(lambda wchr: (wchr['name'], wchr['id'])) \
            .to_set()
        results = []

        def remove_fset(result):
            wchr, fit = result
            tp = wchr['name'], wchr['id']
            if tp in fset:
                fset.discard(tp)
                results.append(result)
            if len(fset) == 0:
                tick_tack.on_next(0)

        s1 = self.executor.sub_compl.subscribe(on_next=remove_fset)

        for fn in fitnessless:
            self.executor.sub_in.on_next(fn)
        if len(fset) == 0:
            tick_tack.on_next(0)
        tick_tack.to_blocking().first()

        tick_tack.dispose()
        s1.dispose()

        gen.init_fitnesses(results)
        if len(gen.get_fitlessness()) > 0:
            raise AssertionError("Посчитались не все гены в хромосоме")

    def stepup_slsqp(self, gen0):
        self.calc_gen(gen0)
        lst = reduce_pop(gen0.pop_list, self.opts['n_op_direct'])
        opti_lst = [
            OptiSLSQP(op, self.executor.sub_in, self.executor.sub_compl)
            for op in lst
        ]
        tp = ThreadPoolScheduler()
        calc_stream = Observable \
            .from_(opti_lst) \
            .flat_map(lambda op: Observable.just(op).observe_on(tp).map(lambda op: op.run()))

        calc_stream.to_blocking().last_or_default(0)
        for op in lst:
            op.remove_exept_best()

        for o_sls in opti_lst:
            o_sls.dispose_conn()
        gen1 = Generation(self.chr_contr, gen0.num_g + 1, lst)
        self.sub_gen_solved.on_next(gen1)
        gen1.fill_pop(self.opts['pop_count'])
        return gen1
Exemplo n.º 7
0
import math

from rx import Observable
from rx.subjects import Subject

# import needed for to_backpressure operator
import rxbackpressure

# time value samples recorded by some device
time_value_record = Subject()

# separate time and value
time = time_value_record.map(lambda pair: pair[0])
signal = time_value_record.map(lambda pair: pair[1])

# timebase synchronization
sync1 = Subject()
sync2 = Subject()

# synchronize time samples to two timebases
time_sync2_bp = time.to_backpressure().zip(sync2.repeat_first(),
                                           lambda t, sync_time: t + sync_time)
time_sync1_bp = time.to_backpressure().zip(sync1.repeat_first(),
                                           lambda t, sync_time: t + sync_time)

# pairing time value observables
time_sync1_bp.to_observable().zip(signal, lambda t, v:
                                  (t, v)).unsafe_subscribe()
time_sync2_bp.to_observable().zip(signal, lambda t, v:
                                  (t, v)).unsafe_subscribe(
                                      print,