- RxPY Tutorial
- RxPY - Home
- RxPY - Overview
- RxPY - Environment Setup
- RxPY - Latest Release Updates
- RxPY - Working with Observables
- RxPY- Operators
- RxPY - Working With Subject
- RxPY - Concurrency Using Scheduler
- RxPY - Examples
- RxPY Useful Resources
- RxPY - Quick Guide
- RxPY - Useful Resources
- RxPY - Discussion
RxPY - Creating Observables
create
This method is used to create an observable. It will have the observer method, i.e.
on_next() − This function gets called, when the Observable emits an item.
on_completed() − This function gets called, when the Observable is complete.
on_error() − This function gets called, when an error occurs on the Observable.
Here, is a working example −
testrx.py
from rx import create def test_observable(observer, scheduler): observer.on_next("Hello") observer.on_error("Error occured") observer.on_completed() source = create(test_observable) source.subscribe( on_next = lambda i: print("Got - {0}".format(i)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!"), )
Here, is the output of the observable created −
E:\pyrx>python testrx.py Got - Hello Job Done!
empty
This observable will not output anything and directly emit the complete state.
Syntax
empty()
Return value
It will return an observable with no elements.
Example
from rx import empty test = empty() test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py Job Done!
never
This method creates an observable that will never reach the complete state.
Syntax
never()
Return value
It will return an observable that will never complete.
Example
from rx import never test = never() test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
It does not show any output.
throw
This method will create an observable that will throw an error.
Syntax
throw(exception)
Parameters
exception: an object that has error details.
Return value
An observable is returned with error details.
Example
from rx import throw test = throw(Exception('There is an Error!')) test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py Error: There is an Error!
from_
This method will convert the given array or object into an observable.
Syntax
from_(iterator)
Parameters
iterator: This is an object or array.
Return value
This will return an observable for the given iterator.
Example
from rx import from_ test = from_([1,2,3,4,5,6,7,8,9,10]) test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
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 The value is 7 The value is 8 The value is 9 The value is 10 Job Done!
interval
This method will give a series of values produced after a timeout.
Syntax
interval(period)
Parameters
period: to start the integer sequence.
Return value
It returns an observable with all the values in sequential order.
Example
import rx from rx import operators as ops rx.interval(1).pipe( ops.map(lambda i: i * i) ).subscribe(lambda x: print("The value is {0}".format(x))) input("Press any key to exit\n")
Output
E:\pyrx>python testrx.py Press any key to exit The value is 0 The value is 1 The value is 4 The value is 9 The value is 16 The value is 25 The value is 36 The value is 49 The value is 64 The value is 81 The value is 100 The value is 121 The value is 144 The value is 169 The value is 196 The value is 225 The value is 256 The value is 289 The value is 324 The value is 361 The value is 400
just
This method will convert given value into an observable.
Syntax
just(value)
Parameters
value: to be converted to an observable.
Return value
It will return an observable with the given values.
Example
from rx import just test = just([15, 25,50, 55]) test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py The value is [15, 25, 50, 55] Job Done!
range
This method will give a range of integers based on the input given.
Syntax
range(start, stop=None)
Parameters
start: the first value from which the range will start.
stop: optional, the last value for the range to stop.
Return value
This will return an observable with integer value based on the input given.
Example
from rx import range test = range(0,10) test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py The value is 0 The value is 1 The value is 2 The value is 3 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9 Job Done!
repeat_value
This method will create an observable that will repeat the given value as per the count is given.
Syntax
repeat_value(value=None, repeat_count=None)
Parameters
value: optional. The value to be repeated.
repeat_count: optional. The number of times the given value to be repeated.
Return value
It will return an observable that will repeat the given value as per the count is given.
Example
from rx import repeat_value test = repeat_value(44,10) test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 The value is 44 Job Done!
start
This method takes in a function as an input, and returns an observable that will return value from the input function.
Syntax
start(func)
Parameters
func: a function that will be called.
Return value
It returns an observable that will have a return value from the input function.
Example
from rx import start test = start(lambda : "Hello World") test.subscribe( lambda x: print("The value is {0}".format(x)), on_error = lambda e: print("Error : {0}".format(e)), on_completed = lambda: print("Job Done!") )
Output
E:\pyrx>python testrx.py The value is Hello World Job Done!
timer
This method will emit the values in sequence after the timeout is done.
Syntax
timer(duetime)
Parameters
duetime: time after which it should emit the first value.
Return value
It will return an observable with values emitted after duetime.
Example
import rx from rx import operators as ops rx.timer(5.0, 10).pipe( ops.map(lambda i: i * i) ).subscribe(lambda x: print("The value is {0}".format(x))) input("Press any key to exit\n")
Output
E:\pyrx>python testrx.py Press any key to exit The value is 0 The value is 1 The value is 4 The value is 9 The value is 16 The value is 25 The value is 36 The value is 49 The value is 64