/
__init__.py
4375 lines (3313 loc) · 128 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=too-many-lines,redefined-builtin,import-outside-toplevel
from asyncio import Future
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Set,
Tuple,
Type,
TypeVar,
Union,
cast,
overload,
)
from reactivex import (
ConnectableObservable,
GroupedObservable,
Notification,
Observable,
abc,
compose,
typing,
)
from reactivex.curry import curry_flip
from reactivex.internal.basic import identity
from reactivex.internal.utils import NotSet
from reactivex.subject import Subject
from reactivex.typing import (
Accumulator,
Comparer,
Mapper,
MapperIndexed,
Predicate,
PredicateIndexed,
)
_T = TypeVar("_T")
_T1 = TypeVar("_T1")
_T2 = TypeVar("_T2")
_TKey = TypeVar("_TKey")
_TState = TypeVar("_TState")
_TValue = TypeVar("_TValue")
_TRight = TypeVar("_TRight")
_TLeft = TypeVar("_TLeft")
_A = TypeVar("_A")
_B = TypeVar("_B")
_C = TypeVar("_C")
_D = TypeVar("_D")
def all(predicate: Predicate[_T]) -> Callable[[Observable[_T]], Observable[bool]]:
"""Determines whether all elements of an observable sequence satisfy
a condition.
.. marble::
:alt: all
--1--2--3--4--5--6----|
[ all(i: i<8) ]
------------------true|
--1--2--3--4--5--6----|
[ all(i: i<4) ]
------false|
Example:
>>> op = all(lambda value: value.length > 3)
Args:
predicate: A function to test each element for a condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element
determining whether all elements in the source sequence pass
the test in the specified predicate.
If a predicate returns false, the result sequence emits false
and completes immediately, regardless of the state of the
source sequence.
If all items pass the predicate test, the emission of true
will only happen as the source completes.
"""
from ._all import all_
return all_(predicate)
def amb(right_source: Observable[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Propagates the observable sequence that reacts first.
.. marble::
:alt: amb
---8--6--9---------|
--1--2--3---5--------|
[ amb() ]
--1--2--3---5--------|
Example:
>>> op = amb(ys)
Returns:
An operator function that takes an observable source and
returns an observable sequence that surfaces any of the given
sequences, whichever reacted first.
"""
from ._amb import amb_
return amb_(right_source)
def as_observable() -> Callable[[Observable[_T]], Observable[_T]]:
"""Hides the identity of an observable sequence.
Returns:
An operator function that takes an observable source and
returns and observable sequence that hides the identity of the
source sequence.
"""
from ._asobservable import as_observable_
return as_observable_()
def average(
key_mapper: Optional[Mapper[_T, float]] = None
) -> Callable[[Observable[_T]], Observable[float]]:
"""The average operator.
Computes the average of an observable sequence of values that
are in the sequence or obtained by invoking a transform function on
each element of the input sequence if present.
.. marble::
:alt: average
---1--2--3--4----|
[ average() ]
-----------------2.5-|
Examples:
>>> op = average()
>>> op = average(lambda x: x.value)
Args:
key_mapper: [Optional] A transform function to apply to each element.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element with
the average of the sequence of values.
"""
from ._average import average_
return average_(key_mapper)
def buffer(
boundaries: Observable[Any],
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer
---a-----b-----c--------|
--1--2--3--4--5--6--7---|
[ buffer() ]
---1-----2,3---4,5------|
Examples:
>>> res = buffer(reactivex.interval(1.0))
Args:
boundaries: Observable sequence whose elements denote the
creation and completion of buffers.
Returns:
A function that takes an observable source and returns an
observable sequence of buffers.
"""
from ._buffer import buffer_
return buffer_(boundaries)
def buffer_when(
closing_mapper: Callable[[], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer_when
--------c-|
--------c-|
--------c-|
---1--2--3--4--5--6-------|
[ buffer_when() ]
+-------1,2-----3,4,5---6-|
Examples:
>>> res = buffer_when(lambda: reactivex.timer(0.5))
Args:
closing_mapper: A function invoked to define the closing of each
produced buffer. A buffer is started when the previous one is
closed, resulting in non-overlapping buffers. The buffer is closed
when one item is emitted or when the observable completes.
Returns:
A function that takes an observable source and returns an
observable sequence of windows.
"""
from ._buffer import buffer_when_
return buffer_when_(closing_mapper)
def buffer_toggle(
openings: Observable[Any], closing_mapper: Callable[[Any], Observable[Any]]
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or
more buffers.
.. marble::
:alt: buffer_toggle
---a-----------b--------------|
---d--|
--------e--|
----1--2--3--4--5--6--7--8----|
[ buffer_toggle() ]
------1----------------5,6,7--|
>>> res = buffer_toggle(reactivex.interval(0.5), lambda i: reactivex.timer(i))
Args:
openings: Observable sequence whose elements denote the
creation of buffers.
closing_mapper: A function invoked to define the closing of each
produced buffer. Value from openings Observable that initiated
the associated buffer is provided as argument to the function. The
buffer is closed when one item is emitted or when the observable
completes.
Returns:
A function that takes an observable source and returns an
observable sequence of windows.
"""
from ._buffer import buffer_toggle_
return buffer_toggle_(openings, closing_mapper)
def buffer_with_count(
count: int, skip: Optional[int] = None
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or more
buffers which are produced based on element count information.
.. marble::
:alt: buffer_with_count
----1-2-3-4-5-6------|
[buffer_with_count(3)]
--------1,2,3-4,5,6--|
Examples:
>>> res = buffer_with_count(10)(xs)
>>> res = buffer_with_count(10, 1)(xs)
Args:
count: Length of each buffer.
skip: [Optional] Number of elements to skip between
creation of consecutive buffers. If not provided, defaults to
the count.
Returns:
A function that takes an observable source and returns an
observable sequence of buffers.
"""
from ._buffer import buffer_with_count_
return buffer_with_count_(count, skip)
def buffer_with_time(
timespan: typing.RelativeTime,
timeshift: Optional[typing.RelativeTime] = None,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into zero or more
buffers which are produced based on timing information.
.. marble::
:alt: buffer_with_time
---1-2-3-4-5-6-----|
[buffer_with_time()]
-------1,2,3-4,5,6-|
Examples:
>>> # non-overlapping segments of 1 second
>>> res = buffer_with_time(1.0)
>>> # segments of 1 second with time shift 0.5 seconds
>>> res = buffer_with_time(1.0, 0.5)
Args:
timespan: Length of each buffer (specified as a float denoting seconds
or an instance of timedelta).
timeshift: [Optional] Interval between creation of consecutive buffers
(specified as a float denoting seconds or an instance of timedelta).
If not specified, the timeshift will be the same as the timespan
argument, resulting in non-overlapping adjacent buffers.
scheduler: [Optional] Scheduler to run the timer on. If not specified,
the timeout scheduler is used
Returns:
An operator function that takes an observable source and
returns an observable sequence of buffers.
"""
from ._bufferwithtime import buffer_with_time_
return buffer_with_time_(timespan, timeshift, scheduler)
def buffer_with_time_or_count(
timespan: typing.RelativeTime,
count: int,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[List[_T]]]:
"""Projects each element of an observable sequence into a buffer
that is completed when either it's full or a given amount of time
has elapsed.
.. marble::
:alt: buffer_with_time_or_count
--1-2-3-4-5-6------|
[ buffer() ]
------1,2,3-4,5,6--|
Examples:
>>> # 5s or 50 items in an array
>>> res = source._buffer_with_time_or_count(5.0, 50)
>>> # 5s or 50 items in an array
>>> res = source._buffer_with_time_or_count(5.0, 50, Scheduler.timeout)
Args:
timespan: Maximum time length of a buffer.
count: Maximum element count of a buffer.
scheduler: [Optional] Scheduler to run buffering timers on. If
not specified, the timeout scheduler is used.
Returns:
An operator function that takes an observable source and
returns an observable sequence of buffers.
"""
from ._bufferwithtimeorcount import buffer_with_time_or_count_
return buffer_with_time_or_count_(timespan, count, scheduler)
def catch(
handler: Union[
Observable[_T], Callable[[Exception, Observable[_T]], Observable[_T]]
]
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Continues an observable sequence that is terminated by an
exception with the next observable sequence.
.. marble::
:alt: catch
---1---2---3-*
a-7-8-|
[ catch(a) ]
---1---2---3---7-8-|
Examples:
>>> op = catch(ys)
>>> op = catch(lambda ex, src: ys(ex))
Args:
handler: Second observable sequence used to produce
results when an error occurred in the first sequence, or an
exception handler function that returns an observable sequence
given the error and source observable that occurred in the
first sequence.
Returns:
A function taking an observable source and returns an
observable sequence containing the first sequence's elements,
followed by the elements of the handler sequence in case an
exception occurred.
"""
from ._catch import catch_
return catch_(handler)
def combine_latest(
*others: Observable[Any],
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Merges the specified observable sequences into one observable
sequence by creating a tuple whenever any of the
observable sequences produces an element.
.. marble::
:alt: combine_latest
---a-----b--c------|
--1---2--------3---|
[ combine_latest() ]
---a1-a2-b2-c2-c3--|
Examples:
>>> obs = combine_latest(other)
>>> obs = combine_latest(obs1, obs2, obs3)
Returns:
An operator function that takes an observable sources and
returns an observable sequence containing the result of
combining elements of the sources into a tuple.
"""
from ._combinelatest import combine_latest_
return combine_latest_(*others)
def concat(*sources: Observable[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Concatenates all the observable sequences.
.. marble::
:alt: concat
---1--2--3--|
--6--8--|
[ concat() ]
---1--2--3----6--8-|
Examples:
>>> op = concat(xs, ys, zs)
Returns:
An operator function that takes one or more observable sources and
returns an observable sequence that contains the elements of
each given sequence, in sequential order.
"""
from ._concat import concat_
return concat_(*sources)
@curry_flip(1)
def concat_map(
source: Observable[_T1], project: Mapper[_T1, Observable[_T2]]
) -> Observable[_T2]:
"""Projects each source value to an Observable which is merged in the
output Observable, in a serialized fashion waiting for each one to complete
before merging the next.
Warning: if source values arrive endlessly and faster than their corresponding
inner Observables can complete, it will result in memory issues as inner
Observables amass in an unbounded buffer waiting
for their turn to be subscribed to.
Note: concatMap is equivalent to mergeMap with concurrency parameter set to 1.
.. marble::
:alt: concat_map
---1------------2-----3----------------------------|
[ concat_map(i: 10*i---10*i---10*i|) ]
---10---10---10-20---20---(20,30)---30---30--------|
Examples:
>>> op = concat(lambda i: reactivex.timer(1.0).pipe(take(3)))
Args:
project: Projecting function which takes the outer observable value
and emits the inner observable
Returns:
An operator function that maps each value to the inner observable
and emits its values in order.
"""
return source.pipe(map(project), merge(max_concurrent=1))
def contains(
value: _T, comparer: Optional[typing.Comparer[_T]] = None
) -> Callable[[Observable[_T]], Observable[bool]]:
"""Determines whether an observable sequence contains a specified
element with an optional equality comparer.
.. marble::
:alt: contains
--1--2--3--4--|
[ contains(3) ]
--------------true-|
Examples:
>>> op = contains(42)
>>> op = contains({ "value": 42 }, lambda x, y: x["value"] == y["value"])
Args:
value: The value to locate in the source sequence.
comparer: [Optional] An equality comparer to compare elements.
Returns:
A function that takes a source observable that returns an
observable sequence containing a single element determining
whether the source sequence contains an element that has the
specified value.
"""
from ._contains import contains_
return contains_(value, comparer)
def count(
predicate: Optional[typing.Predicate[_T]] = None,
) -> Callable[[Observable[_T]], Observable[int]]:
"""Returns an observable sequence containing a value that
represents how many elements in the specified observable sequence
satisfy a condition if provided, else the count of items.
.. marble::
:alt: count
--1--2--3--4--|
[ count(i: i>2) ]
--------------2-|
Examples:
>>> op = count()
>>> op = count(lambda x: x > 3)
Args:
predicate: A function to test each element for a condition.
Returns:
An operator function that takes an observable source and
returns an observable sequence containing a single element with
a number that represents how many elements in the input
sequence satisfy the condition in the predicate function if
provided, else the count of items in the sequence.
"""
from ._count import count_
return count_(predicate)
def debounce(
duetime: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Ignores values from an observable sequence which are followed by
another value before duetime.
.. marble::
:alt: debounce
--1--2-3-4--5------|
[ debounce() ]
----1------4---5---|
Example:
>>> res = debounce(5.0) # 5 seconds
Args:
duetime: Duration of the throttle period for each value
(specified as a float denoting seconds or an instance of timedelta).
scheduler: Scheduler to debounce values on.
Returns:
An operator function that takes the source observable and
returns the debounced observable sequence.
"""
from ._debounce import debounce_
return debounce_(duetime, scheduler)
throttle_with_timeout = debounce
@overload
def default_if_empty(
default_value: _T,
) -> Callable[[Observable[_T]], Observable[_T]]:
...
@overload
def default_if_empty() -> Callable[[Observable[_T]], Observable[Optional[_T]]]:
...
def default_if_empty(
default_value: Any = None,
) -> Callable[[Observable[Any]], Observable[Any]]:
"""Returns the elements of the specified sequence or the specified
value in a singleton sequence if the sequence is empty.
.. marble::
:alt: default_if_empty
----------|
[default_if_empty(42)]
----------42-|
Examples:
>>> res = obs = default_if_empty()
>>> obs = default_if_empty(False)
Args:
default_value: The value to return if the sequence is empty. If
not provided, this defaults to None.
Returns:
An operator function that takes an observable source and
returns an observable sequence that contains the specified
default value if the source is empty otherwise, the elements of
the source.
"""
from ._defaultifempty import default_if_empty_
return default_if_empty_(default_value)
def delay_subscription(
duetime: typing.AbsoluteOrRelativeTime,
scheduler: Optional[abc.SchedulerBase] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Time shifts the observable sequence by delaying the
subscription.
.. marble::
:alt: delay_subscription
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Example:
>>> res = delay_subscription(5.0) # 5s
Args:
duetime: Absolute or relative time to perform the subscription
at.
scheduler: Scheduler to delay subscription on.
Returns:
A function that take a source observable and returns a
time-shifted observable sequence.
"""
from ._delaysubscription import delay_subscription_
return delay_subscription_(duetime, scheduler=scheduler)
def delay_with_mapper(
subscription_delay: Union[
Observable[Any],
typing.Mapper[Any, Observable[Any]],
None,
] = None,
delay_duration_mapper: Optional[typing.Mapper[_T, Observable[Any]]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Time shifts the observable sequence based on a subscription
delay and a delay mapper function for each element.
.. marble::
:alt: delay_with_mapper
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Examples:
>>> # with mapper only
>>> res = source.delay_with_mapper(lambda x: reactivex.timer(5.0))
>>> # with delay and mapper
>>> res = source.delay_with_mapper(
reactivex.timer(2.0), lambda x: reactivex.timer(x)
)
Args:
subscription_delay: [Optional] Sequence indicating the delay
for the subscription to the source.
delay_duration_mapper: [Optional] Selector function to retrieve
a sequence indicating the delay for each given element.
Returns:
A function that takes an observable source and returns a
time-shifted observable sequence.
"""
from ._delaywithmapper import delay_with_mapper_
return delay_with_mapper_(subscription_delay, delay_duration_mapper)
def dematerialize() -> Callable[[Observable[Notification[_T]]], Observable[_T]]:
"""Dematerialize operator.
Dematerializes the explicit notification values of an
observable sequence as implicit notifications.
Returns:
An observable sequence exhibiting the behavior
corresponding to the source sequence's notification values.
"""
from ._dematerialize import dematerialize_
return dematerialize_()
def delay(
duetime: typing.RelativeTime, scheduler: Optional[abc.SchedulerBase] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""The delay operator.
.. marble::
:alt: delay
----1--2--3--4-----|
[ delay() ]
--------1--2--3--4-|
Time shifts the observable sequence by duetime. The relative time
intervals between the values are preserved.
Examples:
>>> res = delay(timedelta(seconds=10))
>>> res = delay(5.0)
Args:
duetime: Relative time, specified as a float denoting seconds or an
instance of timedelta, by which to shift the observable sequence.
scheduler: [Optional] Scheduler to run the delay timers on.
If not specified, the timeout scheduler is used.
Returns:
A partially applied operator function that takes the source
observable and returns a time-shifted sequence.
"""
from ._delay import delay_
return delay_(duetime, scheduler)
def distinct(
key_mapper: Optional[Mapper[_T, _TKey]] = None,
comparer: Optional[Comparer[_TKey]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns an observable sequence that contains only distinct
elements according to the key_mapper and the comparer. Usage of
this operator should be considered carefully due to the maintenance
of an internal lookup structure which can grow large.
.. marble::
:alt: distinct
-0-1-2-1-3-4-2-0---|
[ distinct() ]
-0-1-2---3-4-------|
Examples:
>>> res = obs = xs.distinct()
>>> obs = xs.distinct(lambda x: x.id)
>>> obs = xs.distinct(lambda x: x.id, lambda a,b: a == b)
Args:
key_mapper: [Optional] A function to compute the comparison
key for each element.
comparer: [Optional] Used to compare items in the collection.
Returns:
An operator function that takes an observable source and
returns an observable sequence only containing the distinct
elements, based on a computed key value, from the source
sequence.
"""
from ._distinct import distinct_
return distinct_(key_mapper, comparer)
def distinct_until_changed(
key_mapper: Optional[Mapper[_T, _TKey]] = None,
comparer: Optional[Comparer[_TKey]] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns an observable sequence that contains only distinct
contiguous elements according to the key_mapper and the comparer.
.. marble::
:alt: distinct_until_changed
-0-1-1-2-3-1-2-2-3-|
[ distinct() ]
-0-1---2-3-1-2---3-|
Examples:
>>> op = distinct_until_changed();
>>> op = distinct_until_changed(lambda x: x.id)
>>> op = distinct_until_changed(lambda x: x.id, lambda x, y: x == y)
Args:
key_mapper: [Optional] A function to compute the comparison key
for each element. If not provided, it projects the value.
comparer: [Optional] Equality comparer for computed key values.
If not provided, defaults to an equality comparer function.
Returns:
An operator function that takes an observable source and
returns an observable sequence only containing the distinct
contiguous elements, based on a computed key value, from the
source sequence.
"""
from ._distinctuntilchanged import distinct_until_changed_
return distinct_until_changed_(key_mapper, comparer)
def do(observer: abc.ObserverBase[_T]) -> Callable[[Observable[_T]], Observable[_T]]:
"""Invokes an action for each element in the observable sequence
and invokes an action on graceful or exceptional termination of the
observable sequence. This method can be used for debugging,
logging, etc. of query behavior by intercepting the message stream
to run arbitrary actions for messages on the pipeline.
.. marble::
:alt: do
----1---2---3---4---|
[ do(i: foo()) ]
----1---2---3---4---|
>>> do(observer)
Args:
observer: Observer
Returns:
An operator function that takes the source observable and
returns the source sequence with the side-effecting behavior
applied.
"""
from ._do import do_
return do_(observer)
def do_action(
on_next: Optional[typing.OnNext[_T]] = None,
on_error: Optional[typing.OnError] = None,
on_completed: Optional[typing.OnCompleted] = None,
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Invokes an action for each element in the observable sequence
and invokes an action on graceful or exceptional termination of the
observable sequence. This method can be used for debugging,
logging, etc. of query behavior by intercepting the message stream
to run arbitrary actions for messages on the pipeline.
.. marble::
:alt: do_action
----1---2---3---4---|
[do_action(i: foo())]
----1---2---3---4---|
Examples:
>>> do_action(send)
>>> do_action(on_next, on_error)
>>> do_action(on_next, on_error, on_completed)
Args:
on_next: [Optional] Action to invoke for each element in the
observable sequence.
on_error: [Optional] Action to invoke on exceptional
termination of the observable sequence.
on_completed: [Optional] Action to invoke on graceful
termination of the observable sequence.
Returns:
An operator function that takes the source observable an
returns the source sequence with the side-effecting behavior
applied.
"""
from ._do import do_action_
return do_action_(on_next, on_error, on_completed)
def do_while(
condition: Predicate[Observable[_T]],
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Repeats source as long as condition holds emulating a do while
loop.
.. marble::
:alt: do_while
--1--2--|
[ do_while() ]
--1--2--1--2--1--2--|
Args:
condition: The condition which determines if the source will be
repeated.
Returns:
An observable sequence which is repeated as long
as the condition holds.
"""
from ._dowhile import do_while_
return do_while_(condition)
def element_at(index: int) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the element at a specified index in a sequence.
.. marble::
:alt: element_at
----1---2---3---4---|
[ element_at(2) ]
------------3-|
Example:
>>> res = source.element_at(5)
Args:
index: The zero-based index of the element to retrieve.
Returns:
An operator function that takes an observable source and
returns an observable sequence that produces the element at
the specified position in the source sequence.
"""
from ._elementatordefault import element_at_or_default_
return element_at_or_default_(index, False)
def element_at_or_default(
index: int, default_value: Optional[_T] = None
) -> Callable[[Observable[_T]], Observable[_T]]:
"""Returns the element at a specified index in a sequence or a
default value if the index is out of range.
.. marble::
:alt: element_at_or_default
--1---2---3---4-|
[ element_at(6, a) ]
----------------a-|
Example:
>>> res = source.element_at_or_default(5)
>>> res = source.element_at_or_default(5, 0)
Args:
index: The zero-based index of the element to retrieve.
default_value: [Optional] The default value if the index is
outside the bounds of the source sequence.
Returns:
A function that takes an observable source and returns an
observable sequence that produces the element at the
specified position in the source sequence, or a default value if
the index is outside the bounds of the source sequence.
"""
from ._elementatordefault import element_at_or_default_
return element_at_or_default_(index, True, default_value)