RxPY - 實用運算子
延遲
此運算子將根據給定的時間或日期延遲源可觀測項的發射。
語法
delay(timespan)
引數
timespan:這將是秒或日期的時間。
返回值
它將返回一個可觀測項,其中源值在超時後發射。
示例
from rx import of, operators as op
import datetime
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.delay(5.0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
input("Press any key to exit\n")
輸出
E:\pyrx>python testrx.py Press any key to exit The value is 1 The value is 2 The value is 3 The value is 4 The value is 5
實體化
該運算子將源可觀測項的值轉換為以顯式通知值形式發射的值。
語法
materialize()
返回值
這將返回一個可觀測項,其中以顯式通知值的形式發射的值。
示例
from rx import of, operators as op
import datetime
test1 = of(1,2,3,4,5)
sub1 = test1.pipe(
op.materialize()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The value is OnNext(1.0) The value is OnNext(2.0) The value is OnNext(3.0) The value is OnNext(4.0) The value is OnNext(5.0) The value is OnCompleted()
時間間隔
此運算子將給出源可觀測項的值之間的經過時間。
語法
time_interval()
返回值
它會返回一個可觀測項,其中會有發出的源值之間的經過時間。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.time_interval()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The value is TimeInterval(value=1, interval=datetime.timedelta(microseconds=1000 )) The value is TimeInterval(value=2, interval=datetime.timedelta(0)) The value is TimeInterval(value=3, interval=datetime.timedelta(0)) The value is TimeInterval(value=4, interval=datetime.timedelta(microseconds=1000 )) The value is TimeInterval(value=5, interval=datetime.timedelta(0)) The value is TimeInterval(value=6, interval=datetime.timedelta(0))
超時
此運算子將提供源可觀測項中的所有值,在經過時間後,否則將觸發一個錯誤。
語法
timeout(duetime)
引數
duetime:給定的秒數。
返回值
它將返回一個可觀測項,其中包含源可觀測項中的所有值。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.timeout(5.0)
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The value is 6
時間戳
此運算子將把時間戳附加到源可觀測項中的所有值。
語法
timestamp()
返回值
它將返回一個可觀測項,其中包含源可觀測項中的所有值以及時間戳。
示例
from rx import of, operators as op
from datetime import date
test = of(1,2,3,4,5,6)
sub1 = test.pipe(
op.timestamp()
)
sub1.subscribe(lambda x: print("The value is {0}".format(x)))
輸出
E:\pyrx>python testrx.py The value is Timestamp(value=1, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 667243)) The value is Timestamp(value=2, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 668243)) The value is Timestamp(value=3, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 668243)) The value is Timestamp(value=4, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 668243)) The value is Timestamp(value=5, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 669243)) The value is Timestamp(value=6, timestamp=datetime.datetime(2019, 11, 4, 4, 57, 44, 669243))
廣告