📜  RxPY-运算符

📅  最后修改于: 2020-11-06 06:39:38             🧑  作者: Mango


本章详细介绍RxPY中的运算符。这些运算符包括-

  • 与运营商合作
  • 数学运算符
  • 转型运算符
  • 过滤运算符
  • 错误处理运算符
  • 公用事业运算符
  • 条件运算符
  • 创作运算符
  • 可连接的运算符
  • 合并运算符

响应(Rx) Python几乎有很多运算符,使用Python编码可以使工作变得轻松。您可以将这些多个运算符一起使用,例如,在处理字符串,可以使用map,filter,merge运算符。

与运营商合作

您可以使用pipe()方法与多个运算符一起使用。此方法允许将多个运算符链接在一起。

这是使用运算符的工作示例-

test = of(1,2,3) // an observable
subscriber = test.pipe(
   op1(),
   op2(),
   op3()
)

在上面的示例中,我们创建了一个observable using()方法,该方法接受值1、2和3。现在,在此observable上,您可以使用pipe()方法使用任意数量的运算符来执行不同的操作,如下所示以上。运算符的执行将按可观察到的顺序继续进行。

要与运算符,请首先将其导入,如下所示:

from rx import of, operators as op

这是一个工作示例-

testrx.py

from rx import of, operators as op
test = of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
sub1 = test.pipe(
   op.filter(lambda s: s%2==0),
   op.reduce(lambda acc, x: acc + x)
)
sub1.subscribe(lambda x: print("Sum of Even numbers is {0}".format(x)))

在上面的示例中,有一个数字列表,我们使用filter运算符从中过滤偶数,然后使用reduce运算符将其相加。

输出

E:\pyrx>python testrx.py
Sum of Even numbers is 30

这是运营商列表,我们将讨论-

  • 创建可观察物
  • 数学运算符
  • 转型运算符
  • 过滤运算符
  • 错误处理运算符
  • 公用事业运算符
  • 有条件的
  • 可连接的
  • 合并运算符

创建可观察物

以下是观察值,我们将在“创作”类别中进行讨论

显示范例

Observable Description
create This method is used to create an observable.
empty This observable will not output anything and directly emit the complete state.
never This method creates an observable that will never reach the complete state.
throw This method will create an observable that will throw an error.
from_ This method will convert the given array or object into an observable.
interval This method will give a series of values produced after a timeout.
just This method will convert given value into an observable.
range This method will give a range of integers based on the input given.
repeat_value This method will create an observable that will repeat the given value as per the count is given.
start This method takes in a function as an input and returns an observable that will return value from the input function.
timer This method will emit the values in sequence after the timeout is done.

数学运算符

是我们将要在数学运算符类,讨论运算符如下: –

显示范例

Operator Description
average This operator will calculate the average from the source observable given and output an observable that will have the average value.
concat This operator will take in two or more observables and given a single observable with all the values in the sequence.
count

This operator takes in an Observable with values and converts it into an Observable that will have a single value. The count function takes in predicate function as an optional argument.

The function is of type boolean and will add value to the output only if it satisfies the condition.

max This operator will give an observable with max value from the source observable.
min This operator will give an observable with min value from the source observable.
reduce This operator takes in a function called accumulator function that is used on the values coming from the source observable, and it returns the accumulated values in the form of an observable, with an optional seed value passed to the accumulator function.
sum This operator will return an observable with the sum of all the values from source observables.

转型运算符

运算符我们将在转型运算符类别,讨论被提及如下-

显示范例

Operator Category
buffer This operator will collect all the values from the source observable, and emit them at regular intervals once the given boundary condition is satisfied.
ground_by This operator will group the values coming from the source observable based on the key_mapper function given.
map This operator will change each value from the source observable into a new value based on the output of the mapper_func given.
scan This operator will apply an accumulator function to the values coming from the source observable and return an observable with new values.

过滤运算符

我们要在过滤运算符类,讨论运算符给出如下-

显示范例

Operator Category
debounce This operator will give the values from the source observable, until the timespan given and ignore the rest of the time passes.
distinct This operator will give all the values that are distinct from the source observable.
element_at This operator will give an element from the source observable for the index given.
filter This operator will filter values from the source observable based on the predicate function given.
first This operator will give the first element from the source observable.
ignore_elements This operator will ignore all the values from the source observable and only execute calls to complete or error callback functions.
last This operator will give the last element from the source observable.
skip This operator will give back an observable that will skip the first occurrence of count items taken as input.
skip_last This operator will give back an observable that will skip the last occurrence of count items taken as input.
take This operator will give a list of source values in continuous order based on the count given.
take_last This operator will give a list of source values in continuous order from last based on the count given.

错误处理运算符

我们要在错误处理运算符类,讨论运算符为: –

显示范例

Operator Description
catch This operator will terminate the source observable when there is an exception.
retry This operator will retry on the source observable when there is an error and once the retry count is done it will terminate.

公用事业运算符

下面是我们要在实用程序运算符类,讨论运算符。

显示范例

Operator Description
delay This operator will delay the source observable emission as per the time or date is given.
materialize This operator will convert the values from the source observable with the values emitted in the form of explicit notification values.
time_interval This operator will give the time elapsed between the values from the source observable.
timeout This operator will give all the values from the source observable after the elapsed time or else will trigger an error.
timestamp This operator will attach a timestamp to all the values from the source observable.

条件运算符和布尔运算符

分别如下:我们将在条件和布尔运算符类,讨论运算符-

显示范例

Operator Description
all This operator will check if all the values from the source observable satisfy the condition given.
contains This operator will return an observable with the value true or false if the given value is present and if it is the value of the source observable.
default_if_empty This operator will return a default value if the source observable is empty.
sequence_equal This operator will compare two sequences of observables or an array of values and return an observable with the value true or false.
skip_until This operator will discard values from the source observable until the second observable emits a value.
skip_while This operator will return an observable with values from the source observable that satisfies the condition passed.
take_until This operator will discard values from the source observable after the second observable emits a value or is terminated.
take_while This operator will discard values from the source observable when the condition fails.

可连接的运营商

我们将在可连接的操作范畴,讨论运算符-

显示范例

Operator Description
publish This method will convert the observable into a connectable observable.
ref_count This operator will make the observable a normal observable.
replay This method works similar to the replaySubject. This method will return the same values, even if the observable has already emitted and some of the subscribers are late in subscribing.

合并运算符

以下是我们将在合并运算符类别,讨论运算符。

显示范例

Operator Description
combine_latest This operator will create a tuple for the observable given as input.
merge This operator will merge given observables.
start_with This operator will take in the given values and add at the start of the source observable return back the full sequence.
zip This operator returns an observable with values in a tuple form which is formed by taking the first value of the given observable and so on.