This chapter explains about the operators in RxPY in detail. These operators include −
Reactive (Rx) python has almost lots of operators, that make life easy with python coding. You can use these multiple operators together, for example, while working with strings you can use map, filter, merge operators.
You can work with multiple operators together using pipe() method. This method allows chaining multiple operators together.
Here, is a working example of using operators −
test = of(1,2,3) // an observable subscriber = test.pipe( op1(), op2(), op3() )
In the above example, we have created an observable using of() method that takes in values 1, 2 and 3. Now, on this observable, you can perform a different operation, using any numbers of operators using pipe() method as shown above. The execution of operators will go on sequentially on the observable given.
To work with operators, first import it as shown below −
from rx import of, operators as op
Here, is a working example −
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)))
In the above example, there is a list of numbers, from which we are filtering even numbers using a filter operator and later adding it using a reduce operator.
Output
E:\pyrx>python testrx.py Sum of Even numbers is 30
Here is a list of Operators, that we are going to discuss −
Following are the observables, we are going to discuss in Creation category
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. |
The operators we are going to discuss in Mathematical operator category are as follows: −
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. |
The operators we are going to discuss in the Transformation operator category are mentioned below −
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. |
The operators we are going to discuss in Filtering operator category are given below −
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. |
The operators we are going to discuss in the Error handling operator category are: -
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. |
The following are the operators we are going to discuss in the Utility operator category.
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. |
The operators we are going to discuss in Conditional and Boolean Operator category are as given below −
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. |
The operators we are going to discuss in Connectable Operator category are −
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. |
The following are the operators we are going to discuss in the Combining operator category.
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. |