RxPY – Latest Version Update
RxPY – Latest Updates
In this tutorial, we are using RxPY version 3 and Python version 3.7.3. RxPY version 3 works a bit differently than the earlier version, RxPY version 1.
In this chapter, we’ll discuss the differences between the two versions and the changes you need to make when updating your Python and RxPY versions.
Observables in RxPY
In RxPy version 1, Observable was a standalone class −
from rx import Observable
To use Observable, you must use it as follows −
Observable.of(1,2,3,4,5,6,7,8,9,10)
In RxPy version 3, Observable is now part of the rx package.
Example
import rx
rx.of(1,2,3,4,5,6,7,8,9,10)
Operators in RxPy
In version 1, operators are methods on the Observable class. For example, to utilize operators, we must import Observable, as shown below.
from rx import Observable
Operators are used as Observable.operator, for example, as shown below –
Observable.of(1,2,3,4,5,6,7,8,9,10)
.filter(lambda i: i %2 == 0)
.sum()
.subscribe(lambda x: print("Value is {0}".format(x)))
In the case of RxPY version 3, operators are functions and are imported and used as shown below.
import rx
from rx import operators as ops
rx.of(1,2,3,4,5,6,7,8,9,10).pipe(
ops.filter(lambda i: i %2 == 0),
ops.sum()
).subscribe(lambda x: print("Value is {0}".format(x)))
Using the Pipe() Method to Chain Operators
In RxPy version 1, if you must use multiple operators on an observer, you must do so as follows:
Example
from rx import Observable
Observable.of(1,2,3,4,5,6,7,8,9,10)
.filter(lambda i: i %2 == 0)
.sum()
.subscribe(lambda x: print("Value is {0}".format(x)))
However, in RxPY version 3, you can use the pipe() method with multiple operators, as shown below.
Example
import rx
from rx import operators as ops
rx.of(1,2,3,4,5,6,7,8,9,10).pipe(
ops.filter(lambda i: i %2 == 0),
ops.sum()
).subscribe(lambda x: print("Value is {0}".format(x)))