def __init__( self, columns, add_row=None, remove_row=None, edit_row_at_index=None, clear=None, initial_rows=None, ): self.add_row = Subject() if add_row is None else add_row self.remove_row = Subject() if remove_row is None else remove_row self.clear = Subject() if clear is None else clear self.edit_row_at_index = Subject( ) if edit_row_at_index is None else edit_row_at_index self.initial_rows = pr.v() if initial_rows is None else initial_rows self.initial_columns = pr.v(*columns) self.history = rx.merge(self.add_row, self.remove_row, self.clear, self.edit_row_at_index) self._values_with_columns = self.history.pipe( ops.scan( self.reduce_table_state, (self.initial_rows, self.initial_columns), )) self.values = self._values_with_columns.pipe(ops.map(lambda x: x[0])) self.columns = self._values_with_columns.pipe(ops.map(lambda x: x[1])) self.values_with_history = self.values.pipe(ops.zip(self.history))
def __init__( self, add=None, remove=None, reindex=None, clear=None, edit_at_index=None, initial_state=None, ): self.add = Subject() if add is None else add self.remove = Subject() if remove is None else remove self.reindex = Subject() if reindex is None else reindex self.clear = Subject() if clear is None else clear self.edit_at_index = Subject( ) if edit_at_index is None else edit_at_index self.initial_state = pr.v() if initial_state is None else initial_state self.history = rx.merge(self.add, self.remove, self.reindex, self.clear, self.edit_at_index) self._values = self.history.pipe( ops.scan(self.reduce_list_state, self.initial_state)) self.values = self._values self.values_with_history = self._values.pipe( ops.zip(self.history), # ops.replay(buffer_size=1) )
def __init__(self, inv_chance=0.2): super().__init__() self.inv_chance = inv_chance self.input_danger.pipe( ops.zip(self.input_opportunity), ops.map(lambda x: self.should_flip(x))).subscribe( lambda x: self.output_invert_movement.on_next(x))
def demo_zip(): a = rx.of(1, 2, 3, 4) b = rx.of(2, 2, 4, 4) a.pipe( ops.zip(b), # returns a tuple with the items of a and b ops.map(lambda z: operator.mul(z[0], z[1])) ).subscribe(print)
def demo_starmap(): '''tuple unpacking''' a = rx.of(1, 2, 3, 4) b = rx.of(2, 2, 4, 4) a.pipe( ops.zip(b), ops.starmap(operator.mul) ).subscribe(print)
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)))
def __init__(self): super().__init__() # read pre-trained models for predicting two different periodic functions. self.forward_model = keras.models.load_model("../model/sine.h5") self.backward_model = keras.models.load_model("../model/spiked.h5") self.input_danger.pipe(ops.zip(self.input_opportunity, self.input_flip))\ .subscribe(lambda x: setattr(self, "last_input", x)) Timer().ticks.pipe( ops.filter(lambda x: x % self.muscle_tick_rate == 0), ops.map(lambda x: self.input_to_prediction(self.last_input)), ops.map(lambda x: self.prediction_to_stimuli(x)))\ .subscribe(lambda x: self.output_muscle_stimuli.on_next(x))
def mapper(_xs): return _xs.pipe(ops.zip(_xs.pipe(ops.skip(1))), ops.map(sum))
import rx import rx.operators as ops numbers = rx.from_([1, 2, 3, 4]) characters = rx.from_(['a', 'b', 'c', 'd', 'e']) characters.pipe( ops.zip(numbers), ops.map(lambda c: "{}: {}".format(c[0], c[1])), ).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") )
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))
import time import rx from rx import operators as ops t = 0.01 a = rx.interval(t) b = rx.interval(10 * t) a.pipe( ops.map(lambda x: 1 / 0), ops.zip(b), ).subscribe(print) time.sleep(1000 * t)
def mapper(ys): return ys.pipe( ops.zip(ys), ops.map(sum), )
import rx from rx import operators as ops import operator a = rx.of(1, 2, 3, 4) b = rx.of(2, 2, 4, 4) a.pipe( ops.zip(b), # returns a tuple with the items of a and b ops.starmap(operator.mul) ).subscribe(print)
import rx from rx import operators as ops letters = rx.from_(["A", "B", "C", "D", "E", "F"]) numbers = rx.range(1, 5) letters.pipe(ops.zip(numbers), ops.map( lambda z: "{0}-{1}".format(z[0], z[1]))).subscribe(lambda i: print(i))
def create(): return e1.pipe( ops.zip(e2), ops.map(sum))
def create(): return e2.pipe( ops.zip(e1), ops.map(sum))
def create(): return o1.pipe(ops.zip(o2))
def mapper(_xs): return _xs.pipe( ops.zip(_xs.pipe(ops.skip(1))), ops.map(sum) )
op.catch() op.retry() """Utility""" op.delay() op.materialize() op.time_interval() op.timeout() op.timestamp() """Conditional and Boolean""" op.all() op.contains() op.default_if_empty() op.sequence_equal() op.skip_until() op.skip_while() op.take_until() op.take_while() """Connectable""" op.publish() op.ref_count() op.replay() """Combining""" op.combine_latest() op.merge() op.start_with() op.zip()
import rx from rx import operators as ops letters = rx.from_(["Alpha", "Beta", "Gamma", "Delta", "Epsilon"]) intervals = rx.interval(1.0) letters.pipe(ops.zip(intervals), ops.map(lambda z: z[0])).subscribe(lambda s: print(s)) input("Press any key to quit\n")
def mapper(ys): return ys.pipe( ops.zip(ys), ops.map(sum), )