reactivex 4.1.0__py3-none-any.whl → 5.0.0a2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reactivex/__init__.py +35 -39
- reactivex/_version.py +1 -1
- reactivex/abc/disposable.py +3 -4
- reactivex/abc/observable.py +13 -6
- reactivex/abc/observer.py +2 -1
- reactivex/abc/periodicscheduler.py +7 -6
- reactivex/abc/scheduler.py +10 -9
- reactivex/abc/subject.py +5 -5
- reactivex/disposable/compositedisposable.py +4 -4
- reactivex/disposable/disposable.py +1 -2
- reactivex/disposable/multipleassignmentdisposable.py +2 -3
- reactivex/disposable/refcountdisposable.py +1 -2
- reactivex/disposable/serialdisposable.py +4 -5
- reactivex/disposable/singleassignmentdisposable.py +3 -4
- reactivex/internal/__init__.py +2 -0
- reactivex/internal/basic.py +2 -2
- reactivex/internal/concurrency.py +2 -1
- reactivex/internal/curry.py +59 -0
- reactivex/internal/exceptions.py +7 -12
- reactivex/internal/priorityqueue.py +2 -2
- reactivex/internal/utils.py +3 -2
- reactivex/notification.py +22 -21
- reactivex/observable/case.py +5 -6
- reactivex/observable/catch.py +3 -2
- reactivex/observable/combinelatest.py +4 -5
- reactivex/observable/concat.py +3 -2
- reactivex/observable/connectableobservable.py +7 -7
- reactivex/observable/defer.py +4 -3
- reactivex/observable/empty.py +3 -4
- reactivex/observable/forkjoin.py +5 -5
- reactivex/observable/fromcallback.py +4 -3
- reactivex/observable/fromfuture.py +2 -2
- reactivex/observable/fromiterable.py +4 -3
- reactivex/observable/generate.py +2 -2
- reactivex/observable/generatewithrelativetime.py +4 -3
- reactivex/observable/groupedobservable.py +4 -4
- reactivex/observable/ifthen.py +3 -2
- reactivex/observable/interval.py +1 -4
- reactivex/observable/marbles.py +18 -17
- reactivex/observable/mixins/__init__.py +32 -0
- reactivex/observable/mixins/combination.py +481 -0
- reactivex/observable/mixins/conditional.py +135 -0
- reactivex/observable/mixins/error_handling.py +130 -0
- reactivex/observable/mixins/filtering.py +1119 -0
- reactivex/observable/mixins/mathematical.py +277 -0
- reactivex/observable/mixins/multicasting.py +306 -0
- reactivex/observable/mixins/testing.py +193 -0
- reactivex/observable/mixins/time_based.py +209 -0
- reactivex/observable/mixins/transformation.py +632 -0
- reactivex/observable/mixins/utility.py +811 -0
- reactivex/observable/mixins/windowing.py +688 -0
- reactivex/observable/never.py +2 -2
- reactivex/observable/observable.py +72 -25
- reactivex/observable/onerrorresumenext.py +7 -6
- reactivex/observable/range.py +6 -6
- reactivex/observable/repeat.py +2 -2
- reactivex/observable/returnvalue.py +6 -5
- reactivex/observable/start.py +3 -2
- reactivex/observable/startasync.py +2 -1
- reactivex/observable/throw.py +3 -3
- reactivex/observable/timer.py +12 -12
- reactivex/observable/toasync.py +3 -2
- reactivex/observable/using.py +5 -4
- reactivex/observable/withlatestfrom.py +4 -5
- reactivex/observable/zip.py +7 -6
- reactivex/observer/autodetachobserver.py +4 -4
- reactivex/observer/observer.py +5 -4
- reactivex/observer/scheduledobserver.py +2 -2
- reactivex/operators/__init__.py +162 -208
- reactivex/operators/_all.py +23 -6
- reactivex/operators/_amb.py +88 -75
- reactivex/operators/_asobservable.py +20 -17
- reactivex/operators/_average.py +48 -45
- reactivex/operators/_buffer.py +81 -35
- reactivex/operators/_bufferwithtime.py +29 -9
- reactivex/operators/_bufferwithtimeorcount.py +27 -8
- reactivex/operators/_catch.py +33 -32
- reactivex/operators/_combinelatest.py +28 -20
- reactivex/operators/_concat.py +16 -13
- reactivex/operators/_contains.py +25 -6
- reactivex/operators/_count.py +24 -8
- reactivex/operators/_debounce.py +141 -138
- reactivex/operators/_defaultifempty.py +45 -42
- reactivex/operators/_delay.py +24 -23
- reactivex/operators/_delaysubscription.py +23 -21
- reactivex/operators/_delaywithmapper.py +10 -11
- reactivex/operators/_dematerialize.py +25 -21
- reactivex/operators/_distinct.py +50 -46
- reactivex/operators/_distinctuntilchanged.py +60 -57
- reactivex/operators/_do.py +123 -116
- reactivex/operators/_dowhile.py +3 -2
- reactivex/operators/_elementatordefault.py +57 -33
- reactivex/operators/_exclusive.py +59 -53
- reactivex/operators/_expand.py +82 -77
- reactivex/operators/_filter.py +63 -68
- reactivex/operators/_finallyaction.py +3 -2
- reactivex/operators/_find.py +49 -32
- reactivex/operators/_first.py +18 -11
- reactivex/operators/_firstordefault.py +5 -4
- reactivex/operators/_flatmap.py +89 -83
- reactivex/operators/_forkjoin.py +23 -18
- reactivex/operators/_groupby.py +27 -6
- reactivex/operators/_groupbyuntil.py +8 -5
- reactivex/operators/_groupjoin.py +7 -6
- reactivex/operators/_ignoreelements.py +20 -15
- reactivex/operators/_isempty.py +15 -4
- reactivex/operators/_join.py +6 -5
- reactivex/operators/_last.py +36 -31
- reactivex/operators/_lastordefault.py +8 -8
- reactivex/operators/_map.py +54 -39
- reactivex/operators/_materialize.py +30 -31
- reactivex/operators/_max.py +18 -11
- reactivex/operators/_maxby.py +5 -5
- reactivex/operators/_merge.py +132 -129
- reactivex/operators/_min.py +16 -10
- reactivex/operators/_minby.py +9 -8
- reactivex/operators/_multicast.py +9 -9
- reactivex/operators/_observeon.py +35 -31
- reactivex/operators/_onerrorresumenext.py +2 -1
- reactivex/operators/_pairwise.py +38 -34
- reactivex/operators/_partition.py +80 -73
- reactivex/operators/_pluck.py +4 -3
- reactivex/operators/_publish.py +36 -21
- reactivex/operators/_publishvalue.py +8 -7
- reactivex/operators/_reduce.py +16 -12
- reactivex/operators/_repeat.py +33 -30
- reactivex/operators/_replay.py +9 -9
- reactivex/operators/_retry.py +12 -10
- reactivex/operators/_sample.py +31 -27
- reactivex/operators/_scan.py +41 -39
- reactivex/operators/_sequenceequal.py +8 -7
- reactivex/operators/_single.py +20 -13
- reactivex/operators/_singleordefault.py +6 -5
- reactivex/operators/_skip.py +35 -32
- reactivex/operators/_skiplast.py +38 -34
- reactivex/operators/_skiplastwithtime.py +5 -4
- reactivex/operators/_skipuntil.py +40 -35
- reactivex/operators/_skipuntilwithtime.py +4 -3
- reactivex/operators/_skipwhile.py +65 -44
- reactivex/operators/_skipwithtime.py +50 -46
- reactivex/operators/_slice.py +58 -53
- reactivex/operators/_some.py +48 -47
- reactivex/operators/_startswith.py +17 -15
- reactivex/operators/_subscribeon.py +44 -41
- reactivex/operators/_sum.py +23 -6
- reactivex/operators/_switchlatest.py +71 -69
- reactivex/operators/_take.py +37 -33
- reactivex/operators/_takelast.py +37 -36
- reactivex/operators/_takelastbuffer.py +38 -37
- reactivex/operators/_takelastwithtime.py +60 -56
- reactivex/operators/_takeuntil.py +33 -32
- reactivex/operators/_takeuntilwithtime.py +42 -39
- reactivex/operators/_takewhile.py +108 -100
- reactivex/operators/_takewithtime.py +46 -41
- reactivex/operators/_throttlefirst.py +52 -45
- reactivex/operators/_timeinterval.py +40 -36
- reactivex/operators/_timeout.py +81 -79
- reactivex/operators/_timeoutwithmapper.py +6 -5
- reactivex/operators/_timestamp.py +24 -22
- reactivex/operators/_todict.py +51 -43
- reactivex/operators/_tofuture.py +24 -15
- reactivex/operators/_toiterable.py +33 -27
- reactivex/operators/_tomarbles.py +5 -5
- reactivex/operators/_toset.py +29 -19
- reactivex/operators/_whiledo.py +2 -1
- reactivex/operators/_window.py +100 -99
- reactivex/operators/_windowwithcount.py +56 -54
- reactivex/operators/_windowwithtime.py +95 -79
- reactivex/operators/_windowwithtimeorcount.py +85 -69
- reactivex/operators/_withlatestfrom.py +13 -9
- reactivex/operators/_zip.py +67 -63
- reactivex/operators/connectable/_refcount.py +4 -3
- reactivex/pipe.py +2 -1
- reactivex/run.py +8 -4
- reactivex/scheduler/catchscheduler.py +11 -10
- reactivex/scheduler/currentthreadscheduler.py +2 -3
- reactivex/scheduler/eventloop/asyncioscheduler.py +7 -6
- reactivex/scheduler/eventloop/asynciothreadsafescheduler.py +12 -14
- reactivex/scheduler/eventloop/eventletscheduler.py +4 -4
- reactivex/scheduler/eventloop/geventscheduler.py +4 -4
- reactivex/scheduler/eventloop/ioloopscheduler.py +4 -4
- reactivex/scheduler/eventloop/twistedscheduler.py +4 -4
- reactivex/scheduler/eventloopscheduler.py +9 -12
- reactivex/scheduler/historicalscheduler.py +1 -2
- reactivex/scheduler/immediatescheduler.py +5 -4
- reactivex/scheduler/mainloop/gtkscheduler.py +6 -7
- reactivex/scheduler/mainloop/pygamescheduler.py +4 -4
- reactivex/scheduler/mainloop/qtscheduler.py +6 -6
- reactivex/scheduler/mainloop/tkinterscheduler.py +4 -4
- reactivex/scheduler/mainloop/wxscheduler.py +7 -7
- reactivex/scheduler/newthreadscheduler.py +6 -8
- reactivex/scheduler/periodicscheduler.py +4 -4
- reactivex/scheduler/scheduleditem.py +4 -4
- reactivex/scheduler/scheduler.py +5 -5
- reactivex/scheduler/threadpoolscheduler.py +3 -3
- reactivex/scheduler/timeoutscheduler.py +5 -4
- reactivex/scheduler/trampoline.py +1 -2
- reactivex/scheduler/trampolinescheduler.py +5 -6
- reactivex/scheduler/virtualtimescheduler.py +4 -4
- reactivex/subject/asyncsubject.py +2 -2
- reactivex/subject/behaviorsubject.py +2 -2
- reactivex/subject/innersubscription.py +2 -2
- reactivex/subject/replaysubject.py +8 -8
- reactivex/subject/subject.py +4 -4
- reactivex/testing/coldobservable.py +5 -5
- reactivex/testing/hotobservable.py +6 -6
- reactivex/testing/marbles.py +21 -20
- reactivex/testing/mockdisposable.py +1 -3
- reactivex/testing/mockobserver.py +2 -2
- reactivex/testing/reactivetest.py +2 -2
- reactivex/testing/recorded.py +1 -1
- reactivex/testing/subscription.py +3 -3
- reactivex/testing/testscheduler.py +13 -12
- reactivex/typing.py +25 -14
- {reactivex-4.1.0.dist-info → reactivex-5.0.0a2.dist-info}/METADATA +59 -26
- reactivex-5.0.0a2.dist-info/RECORD +236 -0
- {reactivex-4.1.0.dist-info → reactivex-5.0.0a2.dist-info}/WHEEL +1 -1
- reactivex-4.1.0.dist-info/RECORD +0 -223
- {reactivex-4.1.0.dist-info → reactivex-5.0.0a2.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,55 +1,56 @@
|
|
|
1
|
-
from typing import
|
|
1
|
+
from typing import TypeVar
|
|
2
2
|
|
|
3
3
|
from reactivex import Observable, abc
|
|
4
|
+
from reactivex.internal import curry_flip
|
|
4
5
|
|
|
5
6
|
_T = TypeVar("_T")
|
|
6
7
|
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
@curry_flip
|
|
10
|
+
def take_last_buffer_(source: Observable[_T], count: int) -> Observable[list[_T]]:
|
|
11
|
+
"""Returns an array with the specified number of contiguous
|
|
12
|
+
elements from the end of an observable sequence.
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
Example:
|
|
15
|
+
>>> res = source.pipe(take_last_buffer(5))
|
|
16
|
+
>>> res = take_last_buffer(5)(source)
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
This operator accumulates a buffer with a length enough to
|
|
19
|
+
store elements count elements. Upon completion of the source
|
|
20
|
+
sequence, this buffer is drained on the result sequence. This
|
|
21
|
+
causes the elements to be delayed.
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
Args:
|
|
24
|
+
source: Source observable to take elements from.
|
|
25
|
+
count: Number of elements to take from the end.
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
Returns:
|
|
28
|
+
An observable sequence containing a single list with the
|
|
29
|
+
specified number of elements from the end of the source
|
|
30
|
+
sequence.
|
|
31
|
+
"""
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
def subscribe(
|
|
34
|
+
observer: abc.ObserverBase[list[_T]],
|
|
35
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
36
|
+
) -> abc.DisposableBase:
|
|
37
|
+
q: list[_T] = []
|
|
35
38
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
def on_next(x: _T) -> None:
|
|
40
|
+
with source.lock:
|
|
41
|
+
q.append(x)
|
|
42
|
+
if len(q) > count:
|
|
43
|
+
q.pop(0)
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
def on_completed() -> None:
|
|
46
|
+
observer.on_next(q)
|
|
47
|
+
observer.on_completed()
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
return source.subscribe(
|
|
50
|
+
on_next, observer.on_error, on_completed, scheduler=scheduler
|
|
51
|
+
)
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return take_last_buffer
|
|
53
|
+
return Observable(subscribe)
|
|
53
54
|
|
|
54
55
|
|
|
55
56
|
__all__ = ["take_last_buffer_"]
|
|
@@ -1,68 +1,72 @@
|
|
|
1
|
-
from typing import Any,
|
|
1
|
+
from typing import Any, TypeVar
|
|
2
2
|
|
|
3
3
|
from reactivex import Observable, abc, typing
|
|
4
|
+
from reactivex.internal import curry_flip
|
|
4
5
|
from reactivex.scheduler import TimeoutScheduler
|
|
5
6
|
|
|
6
7
|
_T = TypeVar("_T")
|
|
7
8
|
|
|
8
9
|
|
|
10
|
+
@curry_flip
|
|
9
11
|
def take_last_with_time_(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
source: Observable[_T],
|
|
13
|
+
duration: typing.RelativeTime,
|
|
14
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
15
|
+
) -> Observable[_T]:
|
|
16
|
+
"""Returns elements within the specified duration from the end
|
|
17
|
+
of the observable source sequence.
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
>>> source.pipe(take_last_with_time(5.0))
|
|
21
|
+
>>> take_last_with_time(5.0)(source)
|
|
22
|
+
|
|
23
|
+
This operator accumulates a queue with a length enough to store
|
|
24
|
+
elements received during the initial duration window. As more
|
|
25
|
+
elements are received, elements older than the specified
|
|
26
|
+
duration are taken from the queue and produced on the result
|
|
27
|
+
sequence. This causes elements to be delayed with duration.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
source: Source observable to take elements from.
|
|
31
|
+
duration: Duration for taking elements from the end of the
|
|
27
32
|
sequence.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return take_last_with_time
|
|
33
|
+
scheduler: Scheduler to use for timing.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
An observable sequence with the elements taken during the
|
|
37
|
+
specified duration from the end of the source sequence.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def subscribe(
|
|
41
|
+
observer: abc.ObserverBase[_T],
|
|
42
|
+
scheduler_: abc.SchedulerBase | None = None,
|
|
43
|
+
) -> abc.DisposableBase:
|
|
44
|
+
nonlocal duration
|
|
45
|
+
|
|
46
|
+
_scheduler = scheduler or scheduler_ or TimeoutScheduler.singleton()
|
|
47
|
+
duration = _scheduler.to_timedelta(duration)
|
|
48
|
+
q: list[dict[str, Any]] = []
|
|
49
|
+
|
|
50
|
+
def on_next(x: _T) -> None:
|
|
51
|
+
now = _scheduler.now
|
|
52
|
+
q.append({"interval": now, "value": x})
|
|
53
|
+
while q and now - q[0]["interval"] >= duration:
|
|
54
|
+
q.pop(0)
|
|
55
|
+
|
|
56
|
+
def on_completed():
|
|
57
|
+
now = _scheduler.now
|
|
58
|
+
while q:
|
|
59
|
+
_next = q.pop(0)
|
|
60
|
+
if now - _next["interval"] <= duration:
|
|
61
|
+
observer.on_next(_next["value"])
|
|
62
|
+
|
|
63
|
+
observer.on_completed()
|
|
64
|
+
|
|
65
|
+
return source.subscribe(
|
|
66
|
+
on_next, observer.on_error, on_completed, scheduler=scheduler_
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return Observable(subscribe)
|
|
66
70
|
|
|
67
71
|
|
|
68
72
|
__all__ = ["take_last_with_time_"]
|
|
@@ -1,51 +1,52 @@
|
|
|
1
1
|
from asyncio import Future
|
|
2
|
-
from typing import
|
|
2
|
+
from typing import TypeVar
|
|
3
3
|
|
|
4
4
|
from reactivex import Observable, abc, from_future
|
|
5
5
|
from reactivex.disposable import CompositeDisposable
|
|
6
|
-
from reactivex.internal import noop
|
|
6
|
+
from reactivex.internal import curry_flip, noop
|
|
7
7
|
|
|
8
8
|
_T = TypeVar("_T")
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
@curry_flip
|
|
11
12
|
def take_until_(
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
source: Observable[_T],
|
|
14
|
+
other: Observable[_T] | Future[_T],
|
|
15
|
+
) -> Observable[_T]:
|
|
16
|
+
"""Returns the values from the source observable sequence until
|
|
17
|
+
the other observable sequence produces a value.
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
>>> source.pipe(take_until(other))
|
|
21
|
+
>>> take_until(other)(source)
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
source: The source observable sequence.
|
|
25
|
+
other: Observable or Future that terminates propagation.
|
|
26
|
+
|
|
27
|
+
Returns:
|
|
28
|
+
An observable sequence containing the elements of the source
|
|
29
|
+
sequence up to the point the other sequence interrupted
|
|
30
|
+
further propagation.
|
|
31
|
+
"""
|
|
14
32
|
if isinstance(other, Future):
|
|
15
33
|
obs: Observable[_T] = from_future(other)
|
|
16
34
|
else:
|
|
17
35
|
obs = other
|
|
18
36
|
|
|
19
|
-
def
|
|
20
|
-
|
|
21
|
-
|
|
37
|
+
def subscribe(
|
|
38
|
+
observer: abc.ObserverBase[_T],
|
|
39
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
40
|
+
) -> abc.DisposableBase:
|
|
41
|
+
def on_completed(_: _T) -> None:
|
|
42
|
+
observer.on_completed()
|
|
22
43
|
|
|
23
|
-
|
|
24
|
-
source
|
|
44
|
+
return CompositeDisposable(
|
|
45
|
+
source.subscribe(observer, scheduler=scheduler),
|
|
46
|
+
obs.subscribe(on_completed, observer.on_error, noop, scheduler=scheduler),
|
|
47
|
+
)
|
|
25
48
|
|
|
26
|
-
|
|
27
|
-
An observable sequence containing the elements of the source
|
|
28
|
-
sequence up to the point the other sequence interrupted
|
|
29
|
-
further propagation.
|
|
30
|
-
"""
|
|
31
|
-
|
|
32
|
-
def subscribe(
|
|
33
|
-
observer: abc.ObserverBase[_T],
|
|
34
|
-
scheduler: Optional[abc.SchedulerBase] = None,
|
|
35
|
-
) -> abc.DisposableBase:
|
|
36
|
-
def on_completed(_: _T) -> None:
|
|
37
|
-
observer.on_completed()
|
|
38
|
-
|
|
39
|
-
return CompositeDisposable(
|
|
40
|
-
source.subscribe(observer, scheduler=scheduler),
|
|
41
|
-
obs.subscribe(
|
|
42
|
-
on_completed, observer.on_error, noop, scheduler=scheduler
|
|
43
|
-
),
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
return Observable(subscribe)
|
|
47
|
-
|
|
48
|
-
return take_until
|
|
49
|
+
return Observable(subscribe)
|
|
49
50
|
|
|
50
51
|
|
|
51
52
|
__all__ = ["take_until_"]
|
|
@@ -1,53 +1,56 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
|
-
from typing import Any,
|
|
2
|
+
from typing import Any, TypeVar
|
|
3
3
|
|
|
4
4
|
from reactivex import Observable, abc, typing
|
|
5
5
|
from reactivex.disposable import CompositeDisposable
|
|
6
|
+
from reactivex.internal import curry_flip
|
|
6
7
|
from reactivex.scheduler import TimeoutScheduler
|
|
7
8
|
|
|
8
9
|
_T = TypeVar("_T")
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
@curry_flip
|
|
11
13
|
def take_until_with_time_(
|
|
14
|
+
source: Observable[_T],
|
|
12
15
|
end_time: typing.AbsoluteOrRelativeTime,
|
|
13
|
-
scheduler:
|
|
14
|
-
) ->
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return
|
|
16
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
17
|
+
) -> Observable[_T]:
|
|
18
|
+
"""Takes elements for the specified duration until the specified end
|
|
19
|
+
time, using the specified scheduler to run timers.
|
|
20
|
+
|
|
21
|
+
Examples:
|
|
22
|
+
>>> source.pipe(take_until_with_time(dt))
|
|
23
|
+
>>> take_until_with_time(5.0)(source)
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
source: Source observable to take elements from.
|
|
27
|
+
end_time: Absolute or relative time when to complete.
|
|
28
|
+
scheduler: Scheduler to use for timing.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
An observable sequence with the elements taken
|
|
32
|
+
until the specified end time.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def subscribe(
|
|
36
|
+
observer: abc.ObserverBase[_T],
|
|
37
|
+
scheduler_: abc.SchedulerBase | None = None,
|
|
38
|
+
) -> abc.DisposableBase:
|
|
39
|
+
_scheduler = scheduler or scheduler_ or TimeoutScheduler.singleton()
|
|
40
|
+
|
|
41
|
+
def action(scheduler: abc.SchedulerBase, state: Any = None):
|
|
42
|
+
observer.on_completed()
|
|
43
|
+
|
|
44
|
+
if isinstance(end_time, datetime):
|
|
45
|
+
task = _scheduler.schedule_absolute(end_time, action)
|
|
46
|
+
else:
|
|
47
|
+
task = _scheduler.schedule_relative(end_time, action)
|
|
48
|
+
|
|
49
|
+
return CompositeDisposable(
|
|
50
|
+
task, source.subscribe(observer, scheduler=scheduler_)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
return Observable(subscribe)
|
|
51
54
|
|
|
52
55
|
|
|
53
56
|
__all__ = ["take_until_with_time_"]
|
|
@@ -1,121 +1,129 @@
|
|
|
1
|
-
from typing import Any,
|
|
1
|
+
from typing import Any, TypeVar
|
|
2
2
|
|
|
3
3
|
from reactivex import Observable, abc
|
|
4
|
+
from reactivex.internal import curry_flip
|
|
4
5
|
from reactivex.typing import Predicate, PredicateIndexed
|
|
5
6
|
|
|
6
7
|
_T = TypeVar("_T")
|
|
7
8
|
|
|
8
9
|
|
|
10
|
+
@curry_flip
|
|
9
11
|
def take_while_(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
12
|
+
source: Observable[_T],
|
|
13
|
+
predicate: Predicate[_T],
|
|
14
|
+
inclusive: bool = False,
|
|
15
|
+
) -> Observable[_T]:
|
|
16
|
+
"""Returns elements from an observable sequence as long as a
|
|
17
|
+
specified condition is true.
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
>>> result = source.pipe(take_while(lambda x: x < 10))
|
|
21
|
+
>>> result = take_while(lambda x: x < 10)(source)
|
|
22
|
+
>>> result = source.pipe(take_while(lambda x: x < 10, inclusive=True))
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
source: The source observable to take from.
|
|
26
|
+
predicate: A function to test each element for a condition.
|
|
27
|
+
inclusive: Include the element that fails the predicate.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
An observable sequence that contains the elements from the
|
|
31
|
+
input sequence that occur before the element at which the
|
|
32
|
+
test no longer passes.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def subscribe(
|
|
36
|
+
observer: abc.ObserverBase[_T],
|
|
37
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
38
|
+
) -> abc.DisposableBase:
|
|
39
|
+
running = True
|
|
40
|
+
|
|
41
|
+
def on_next(value: _T):
|
|
42
|
+
nonlocal running
|
|
43
|
+
|
|
44
|
+
with source.lock:
|
|
45
|
+
if not running:
|
|
46
|
+
return
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
running = predicate(value)
|
|
50
|
+
except Exception as exn:
|
|
51
|
+
observer.on_error(exn)
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
if running:
|
|
55
|
+
observer.on_next(value)
|
|
56
|
+
else:
|
|
57
|
+
if inclusive:
|
|
48
58
|
observer.on_next(value)
|
|
49
|
-
|
|
50
|
-
if inclusive:
|
|
51
|
-
observer.on_next(value)
|
|
52
|
-
observer.on_completed()
|
|
53
|
-
|
|
54
|
-
return source.subscribe(
|
|
55
|
-
on_next, observer.on_error, observer.on_completed, scheduler=scheduler
|
|
56
|
-
)
|
|
59
|
+
observer.on_completed()
|
|
57
60
|
|
|
58
|
-
return
|
|
61
|
+
return source.subscribe(
|
|
62
|
+
on_next, observer.on_error, observer.on_completed, scheduler=scheduler
|
|
63
|
+
)
|
|
59
64
|
|
|
60
|
-
return
|
|
65
|
+
return Observable(subscribe)
|
|
61
66
|
|
|
62
67
|
|
|
68
|
+
@curry_flip
|
|
63
69
|
def take_while_indexed_(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
observer.on_next(value)
|
|
70
|
+
source: Observable[_T],
|
|
71
|
+
predicate: PredicateIndexed[_T],
|
|
72
|
+
inclusive: bool = False,
|
|
73
|
+
) -> Observable[_T]:
|
|
74
|
+
"""Returns elements from an observable sequence as long as a
|
|
75
|
+
specified condition is true. The element's index is used in the
|
|
76
|
+
logic of the predicate function.
|
|
77
|
+
|
|
78
|
+
Examples:
|
|
79
|
+
>>> result = source.pipe(take_while_indexed(lambda x, i: i < 10))
|
|
80
|
+
>>> result = take_while_indexed(lambda x, i: i < 10)(source)
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
source: Source observable to take from.
|
|
84
|
+
predicate: A function to test each element with its index.
|
|
85
|
+
inclusive: Include the element that fails the predicate.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
An observable sequence that contains the elements from the
|
|
89
|
+
input sequence that occur before the element at which the
|
|
90
|
+
test no longer passes.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def subscribe(
|
|
94
|
+
observer: abc.ObserverBase[_T],
|
|
95
|
+
scheduler: abc.SchedulerBase | None = None,
|
|
96
|
+
) -> abc.DisposableBase:
|
|
97
|
+
running = True
|
|
98
|
+
i = 0
|
|
99
|
+
|
|
100
|
+
def on_next(value: Any) -> None:
|
|
101
|
+
nonlocal running, i
|
|
102
|
+
|
|
103
|
+
with source.lock:
|
|
104
|
+
if not running:
|
|
105
|
+
return
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
running = predicate(value, i)
|
|
109
|
+
except Exception as exn:
|
|
110
|
+
observer.on_error(exn)
|
|
111
|
+
return
|
|
107
112
|
else:
|
|
108
|
-
|
|
109
|
-
observer.on_next(value)
|
|
110
|
-
observer.on_completed()
|
|
113
|
+
i += 1
|
|
111
114
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
if running:
|
|
116
|
+
observer.on_next(value)
|
|
117
|
+
else:
|
|
118
|
+
if inclusive:
|
|
119
|
+
observer.on_next(value)
|
|
120
|
+
observer.on_completed()
|
|
115
121
|
|
|
116
|
-
return
|
|
122
|
+
return source.subscribe(
|
|
123
|
+
on_next, observer.on_error, observer.on_completed, scheduler=scheduler
|
|
124
|
+
)
|
|
117
125
|
|
|
118
|
-
return
|
|
126
|
+
return Observable(subscribe)
|
|
119
127
|
|
|
120
128
|
|
|
121
129
|
__all__ = ["take_while_", "take_while_indexed_"]
|