-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: marbles syntax + testing functions #299
Changes from 15 commits
367e75c
ae22327
dbe6856
0778533
7f918ee
812aba3
b7b6dfe
39d9131
3f5224b
1efbea6
bc5cef0
3c6c97c
aaaeeb1
b774bfd
c44e948
1df1055
8fbe13a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import time | ||
|
||
import rx | ||
from rx import concurrency as ccy | ||
|
||
err = ValueError("I don't like 5!") | ||
|
||
src0 = rx.from_marbles('12-----4-----67--|', timespan=0.2) | ||
src1 = rx.from_marbles('----3----5-# ', timespan=0.2, error=err) | ||
|
||
source = rx.merge(src0, src1) | ||
source.subscribe( | ||
on_next=print, | ||
on_error=lambda e: print('boom!! {}'.format(e)), | ||
on_completed=lambda: print('good job!'), | ||
scheduler=ccy.timeout_scheduler, | ||
) | ||
|
||
time.sleep(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import time | ||
|
||
import rx | ||
from rx import operators as ops | ||
from rx import concurrency as ccy | ||
|
||
a = rx.cold(' ---a---a----------------a-|') | ||
b = rx.cold(' ---b---b---| ') | ||
c = rx.cold(' ---c---c---| ') | ||
d = rx.cold(' --d---d---| ') | ||
e1 = rx.cold('a--b--------c--d-------| ') | ||
|
||
observableLookup = {"a": a, "b": b, "c": c, "d": d} | ||
|
||
source = e1.pipe( | ||
ops.flat_map(lambda value: observableLookup[value]), | ||
) | ||
|
||
source.subscribe_( | ||
on_next=print, | ||
on_error=lambda e: print('boom!! {}'.format(e)), | ||
on_completed=lambda: print('good job!'), | ||
scheduler=ccy.timeout_scheduler, | ||
) | ||
|
||
time.sleep(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import time | ||
|
||
import rx | ||
from rx import concurrency as ccy | ||
|
||
lookup0 = {'a': 1, 'b': 3, 'c': 5} | ||
lookup1 = {'x': 2, 'y': 4, 'z': 6} | ||
source0 = rx.cold('a---b----c----|', timespan=0.2, lookup=lookup0) | ||
source1 = rx.cold('---x---y---z--|', timespan=0.2, lookup=lookup1) | ||
|
||
observable = rx.merge(source0, source1) | ||
|
||
observable.subscribe_( | ||
on_next=print, | ||
on_error=lambda e: print('boom!! {}'.format(e)), | ||
on_completed=lambda: print('good job!'), | ||
scheduler=ccy.timeout_scheduler, | ||
) | ||
|
||
time.sleep(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import time | ||
|
||
import rx | ||
from rx import concurrency as ccy | ||
|
||
source0 = rx.cold('a-----d---1--------4-|', timespan=0.1) | ||
source1 = rx.cold('--b-c-------2---3-| ', timespan=0.1) | ||
|
||
observable = rx.merge(source0, source1) | ||
|
||
observable.subscribe( | ||
on_next=print, | ||
on_error=lambda e: print('boom!! {}'.format(e)), | ||
on_completed=lambda: print('good job!'), | ||
scheduler=ccy.timeout_scheduler, | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right, good idea ! |
||
time.sleep(3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from rx.testing import marbles | ||
import rx.concurrency as ccy | ||
import datetime | ||
import rx | ||
|
||
#start_time = 5 | ||
now = datetime.datetime.utcnow() | ||
start_time = now + datetime.timedelta(seconds=3.0) | ||
hot = rx.hot('--a--b--c--|', | ||
# timespan=0.3, | ||
start_time=start_time, | ||
# scheduler=ccy.timeout_scheduler, | ||
) | ||
|
||
hot.subscribe(print, print, lambda: print('completed')) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from rx import operators as ops | ||
from rx.testing import marbles | ||
|
||
""" | ||
Tests debounceTime from rxjs | ||
https://github.com/ReactiveX/rxjs/blob/master/spec/operators/debounceTime-spec.ts | ||
|
||
it should delay all element by the specified time | ||
""" | ||
|
||
start, cold, hot, exp = marbles.test_context(timespan=1) | ||
|
||
e1 = hot('-a--------b------c----|') | ||
ex = exp('------a--------b------(c|)') | ||
expected = ex | ||
|
||
|
||
def create(): | ||
return e1.pipe( | ||
ops.debounce(5), | ||
) | ||
|
||
|
||
results = start(create) | ||
assert results == expected | ||
|
||
print('\ndebounce: results vs expected') | ||
for r, e in zip(results, expected): | ||
print(r, e) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from rx import operators as ops | ||
from rx.testing import marbles | ||
|
||
""" | ||
Tests MergeMap from rxjs | ||
https://github.com/ReactiveX/rxjs/blob/master/spec/operators/mergeMap-spec.ts | ||
|
||
it should flat_map many regular interval inners | ||
""" | ||
start, cold, hot, exp = marbles.test_context(timespan=1) | ||
|
||
a = cold(' ----a---a---a---(a|) ') | ||
b = cold(' ----1---b---(b|) ') | ||
c = cold(' ----c---c---c---c---(c|)') | ||
d = cold(' ----(d|) ') | ||
e1 = hot('-a---b-----------c-------d-------| ') | ||
ex = exp('-----a---(a1)(ab)(ab)c---c---(cd)c---(c|)') | ||
expected = ex | ||
|
||
observableLookup = {"a": a, "b": b, "c": c, "d": d} | ||
|
||
|
||
def create(): | ||
return e1.pipe( | ||
ops.flat_map(lambda value: observableLookup[value]) | ||
) | ||
|
||
|
||
results = start(create) | ||
assert results == expected | ||
|
||
|
||
print('\nflat_map: results vs expected') | ||
for r, e in zip(results, expected): | ||
print(r, e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
subscribe()
from examples now that subscribe supports (again) both observers and callbacks. Subscribe with an _ will still be used by the operators, but it may be confusing for the users that we have two different functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted 👍
@dbrattli By the way, I've reworked all examples + some others things waiting in a big commit. I'm sorry this PR is a bit of a mess because I've pushed a lot of commits that changes everything. Do you prefer I close this one and open a new one when it's ready?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcafhe I think it's time to squash and merge. This feature is self contained and I'm not worried if it needs more work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool !