本文整理汇总了Python中rx.operators.map函数的典型用法代码示例。如果您正苦于以下问题:Python map函数的具体用法?Python map怎么用?Python map使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了map函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_map_disposeinsidemapper
def test_map_disposeinsidemapper(self):
scheduler = TestScheduler()
xs = scheduler.create_hot_observable(on_next(100, 1), on_next(200, 2), on_next(500, 3), on_next(600, 4))
results = scheduler.create_observer()
d = SerialDisposable()
invoked = [0]
def projection(x, *args, **kw):
invoked[0] += 1
if scheduler.clock > 400:
d.dispose()
return x
d.disposable = xs.pipe(
map(projection)
).subscribe(results, scheduler)
def action(scheduler, state):
return d.dispose()
scheduler.schedule_absolute(ReactiveTest.disposed, action)
scheduler.start()
assert results.messages == [on_next(100, 1), on_next(200, 2)]
assert xs.subscriptions == [ReactiveTest.subscribe(0, 500)]
assert invoked[0] == 3
示例2: open
def open(self):
scheduler = AsyncIOScheduler()
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.subject = Subject()
# Get all distinct key up events from the input and only fire if long enough and distinct
searcher = self.subject.pipe(
ops.map(lambda x: x["term"]),
ops.filter(lambda text: len(text) > 2), # Only if the text is longer than 2 characters
ops.debounce(0.750), # Pause for 750ms
ops.distinct_until_changed(), # Only if the value has changed
ops.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)
示例3: main
def main():
app = QApplication(sys.argv)
scheduler = QtScheduler(QtCore)
window = Window()
window.show()
text = 'TIME FLIES LIKE AN ARROW'
def on_next(info):
label, (x, y), i = info
label.move(x + i*12 + 15, y)
label.show()
def handle_label(label, i):
delayer = ops.delay(i * 0.100)
mapper = ops.map(lambda xy: (label, xy, i))
return window.mousemove.pipe(
delayer,
mapper,
)
labeler = ops.flat_map_indexed(handle_label)
mapper = ops.map(lambda c: QLabel(c, window))
rx.from_(text).pipe(
mapper,
labeler,
).subscribe(on_next, on_error=print, scheduler=scheduler)
sys.exit(app.exec_())
示例4: handle_label
def handle_label(label, i):
delayer = ops.delay(i*0.100)
mapper = ops.map(lambda xy: (label, xy, i))
return window.mousemove.pipe(
delayer,
mapper,
)
示例5: create
def create():
def mapper(x):
return Timestamp(x.value, x.timestamp)
return xs.pipe(
ops.timestamp(),
ops.map(mapper),
)
示例6: create
def create():
def mapper(x):
return TimeInterval(x.value, x.interval)
return xs.pipe(
ops.time_interval(),
ops.map(mapper),
)
示例7: factory
def factory():
def projection(x):
invoked[0] += 1
if invoked[0] == 3:
raise Exception(ex)
return x + 1
return xs.pipe(map(projection))
示例8: factory
def factory():
def key_mapper(x):
key_invoked[0] += 1
return x.lower().strip()
return xs.pipe(
ops.group_by(key_mapper, lambda x: x),
ops.map(lambda g: g.key),
)
示例9: handle_label
def handle_label(label, i):
label.config(dict(borderwidth=0, padx=0, pady=0))
mapper = ops.map(lambda ev: (label, ev, i))
delayer = ops.delay(i*0.1)
return mousemove.pipe(
delayer,
mapper
)
示例10: _all
def _all(predicate: Predicate) -> Callable[[Observable], Observable]:
filtering = ops.filter(lambda v: not predicate(v))
mapping = ops.map(lambda b: not b)
some = ops.some()
return pipe(
filtering,
some,
mapping
)
示例11: _pluck
def _pluck(key: Any) -> Callable[[Observable], Observable]:
"""Retrieves the value of a specified key using dict-like access (as in
element[key]) from all elements in the Observable sequence.
Args:
key: The key to pluck.
Returns a new Observable {Observable} sequence of key values.
To pluck an attribute of each element, use pluck_attr.
"""
return ops.map(lambda x: x[key])
示例12: average
def average(source: Observable) -> Observable:
"""Partially applied average operator.
Computes the average of an observable sequence of values that
are in the sequence or obtained by invoking a transform
function on each element of the input sequence if present.
Examples:
>>> res = average(source)
Args:
source: Source observable to average.
Returns:
An observable sequence containing a single element with the
average of the sequence of values.
"""
if key_mapper:
return source.pipe(
operators.map(key_mapper),
operators.average()
)
def accumulator(prev, cur):
return AverageValue(sum=prev.sum+cur, count=prev.count+1)
def mapper(s):
if s.count == 0:
raise Exception('The input sequence was empty')
return s.sum / float(s.count)
seed = AverageValue(sum=0, count=0)
return source.pipe(
operators.scan(accumulator, seed),
operators.last(),
operators.map(mapper)
)
示例13: create
def create():
def mapper(xy):
x, y = xy
return "{}{}".format(x.value, y.value)
return xs.pipe(
ops.join(
ys,
lambda x: rx.timer(x.interval),
lambda y: rx.timer(y.interval).pipe(ops.flat_map(rx.throw(ex) if y.value == "tin" else rx.empty())),
),
ops.map(mapper),
)
示例14: subscribe
def subscribe(observer, scheduler_):
_scheduler = scheduler or scheduler_ or timeout_scheduler
last = _scheduler.now
def mapper(value):
nonlocal last
now = _scheduler.now
span = now - last
last = now
return TimeInterval(value=value, interval=span)
return source.pipe(ops.map(mapper)).subscribe(observer, scheduler_)
示例15: observable_window_with_openings
def observable_window_with_openings(self, window_openings, window_closing_mapper):
def mapper(args):
_, window = args
return window
return window_openings.pipe(
ops.group_join(
self,
window_closing_mapper,
lambda _: empty(),
),
ops.map(mapper),
)