RxPY - Latest Release Updates



In this tutorial, we are using RxPY version 3 and python version 3.7.3. The working of RxPY version 3 differs a little bit with the earlier version, i.e. RxPY version 1.

In this chapter, we are going to discuss the differences between the 2 versions and changes that need to be done in case you are updating Python and RxPY versions.

Observable in RxPY

In RxPy version 1, Observable was a separate class −

from rx import Observable

To use the Observable, you have to use it as follows −

Observable.of(1,2,3,4,5,6,7,8,9,10)

In RxPy version 3, Observable is directly a 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, the operator was methods in the Observable class. For example, to make use of operators we have to import Observable as shown below −

from rx import Observable

The 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 function and are imported and used as follows −

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)))

Chaining Operators Using Pipe() method

In RxPy version 1, in case you had to use multiple operators on an observable, it had to be done 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)))

But, in case of RxPY version 3, you can use pipe() method and 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)))
Advertisements