Skip to content

Commit a38abbe

Browse files
committed
Merge branch 'netsix' into main
2 parents 27f3794 + abbe61c commit a38abbe

File tree

11 files changed

+732
-91
lines changed

11 files changed

+732
-91
lines changed

RunTest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
coverage run -m pytest
1+
coverage run -m pytest @args
22
if ($?)
33
{
44
coverage report -m

doc/api/more/types_linq.more.more_enumerable.rst

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ MoreEnumerable provides more query methods. Instances of this class can be creat
1212
constructing, using as_more(), or invoking MoreEnumerable methods that return MoreEnumerable
1313
instead of Enumerable.
1414

15-
Some APIs are subject to change as updates to .NET (6) are happening - they will move to Enumerable
16-
class if one day they appear in the official .NET doc.
15+
These APIs may have breaking changes more frequently than those in Enumerable class because updates
16+
in .NET are happening and sometimes ones of these APIs could be moved to Enumerable with modification,
17+
or changed to accommodate changes to Enumerable.
1718

1819
Bases
1920
======
@@ -82,25 +83,6 @@ Returns the original MoreEnumerable reference.
8283

8384
----
8485

85-
instancemethod ``distinct_by(key_selector)``
86-
----------------------------------------------
87-
88-
Parameters
89-
- `key_selector` (``Callable[[TSource_co], object]``)
90-
91-
Returns
92-
- ``MoreEnumerable[TSource_co]``
93-
94-
Returns distinct elements from the sequence where "distinctness" is determined by the value
95-
returned by the selector.
96-
97-
Example
98-
>>> ints = [1, 4, 5, 6, 4, 3, 1, 99]
99-
>>> MoreEnumerable(ints).distinct_by(lambda x: x // 2).to_list()
100-
[1, 4, 6, 3, 99]
101-
102-
----
103-
10486
instancemethod ``enumerate(start=0)``
10587
---------------------------------------
10688

@@ -120,8 +102,8 @@ Example
120102

121103
----
122104

123-
instancemethod ``except_by(second, key_selector)``
124-
----------------------------------------------------
105+
instancemethod ``except_by2(second, key_selector)``
106+
-----------------------------------------------------
125107

126108
Parameters
127109
- `second` (``Iterable[TSource_co]``)
@@ -136,7 +118,7 @@ determines "distinctness". Note the second iterable is homogenous to self.
136118
Example
137119
>>> first = [(16, 'x'), (9, 'y'), (12, 'd'), (16, 't')]
138120
>>> second = [(24, 'd'), (77, 'y')]
139-
>>> MoreEnumerable(first).except_by(second, lambda x: x[1]).to_list()
121+
>>> MoreEnumerable(first).except_by2(second, lambda x: x[1]).to_list()
140122
[(16, 'x'), (16, 't')]
141123

142124
----

doc/api/types_linq.enumerable.rst

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ Example
8686
8787
----
8888

89+
instancemethod ``__getitem__[TDefault](__index_and_default)``
90+
---------------------------------------------------------------
91+
92+
Parameters
93+
- `__index_and_default` (``Tuple[int, TDefault]``)
94+
95+
Returns
96+
- ``Union[TSource_co, TDefault]``
97+
98+
Returns the element at specified index in the sequence or returns the default value if it does not
99+
exist. Prefers calling `__getitem__()` on the wrapped iterable if available, otherwise, calls
100+
`self.element_at()`.
101+
102+
Example
103+
.. code-block:: python
104+
105+
>>> def gen():
106+
... yield 1; yield 10; yield 100
107+
108+
>>> Enumerable(gen())[3, 1000]
109+
1000
110+
111+
----
112+
89113
instancemethod ``__getitem__(index)``
90114
---------------------------------------
91115

@@ -496,6 +520,35 @@ Example
496520
497521
----
498522

523+
instancemethod ``chunk(size)``
524+
--------------------------------
525+
526+
Parameters
527+
- `size` (``int``)
528+
529+
Returns
530+
- ``Enumerable[MutableSequence[TSource_co]]``
531+
532+
Splits the elements of a sequence into chunks of size at most the provided size. Raises
533+
`InvalidOperationError` if `size` is less than 1.
534+
535+
Example
536+
.. code-block:: python
537+
538+
>>> def source(i):
539+
... while True:
540+
... yield i
541+
... i *= 3
542+
543+
>>> en = Enumerable(source(1)).chunk(4).take(3)
544+
>>> for chunk in en:
545+
... print(chunk)
546+
[1, 3, 9, 27]
547+
[81, 243, 729, 2187]
548+
[6561, 19683, 59049, 177147]
549+
550+
----
551+
499552
instancemethod ``concat(second)``
500553
-----------------------------------
501554

@@ -641,6 +694,25 @@ Example
641694

642695
----
643696

697+
instancemethod ``distinct_by(key_selector)``
698+
----------------------------------------------
699+
700+
Parameters
701+
- `key_selector` (``Callable[[TSource_co], object]``)
702+
703+
Returns
704+
- ``Enumerable[TSource_co]``
705+
706+
Returns distinct elements from the sequence where "distinctness" is determined by the value
707+
returned by the selector.
708+
709+
Example
710+
>>> ints = [1, 4, 5, 6, 4, 3, 1, 99]
711+
>>> Enumerable(ints).distinct_by(lambda x: x // 2).to_list()
712+
[1, 4, 6, 3, 99]
713+
714+
----
715+
644716
instancemethod ``element_at(index)``
645717
--------------------------------------
646718

@@ -653,6 +725,8 @@ Returns
653725
Returns the element at specified index in the sequence. `IndexOutOfRangeError` is raised if
654726
no such element exists.
655727

728+
If the index is negative, it means counting from the end.
729+
656730
This method always uses a generic list element-finding method (O(n)) regardless the
657731
implementation of the wrapped iterable.
658732

@@ -665,6 +739,9 @@ Example
665739
>>> Enumerable(gen()).element_at(1)
666740
10
667741
742+
>>> Enumerable(gen()).element_at(-1)
743+
100
744+
668745
----
669746

670747
instancemethod ``element_at[TDefault](index, __default)``
@@ -680,6 +757,8 @@ Returns
680757
Returns the element at specified index in the sequence. Default value is returned if no
681758
such element exists.
682759

760+
If the index is negative, it means counting from the end.
761+
683762
This method always uses a generic list element-finding method (O(n)) regardless the
684763
implementation of the wrapped iterable.
685764

@@ -731,6 +810,27 @@ Example
731810

732811
----
733812

813+
instancemethod ``except_by[TKey](second, key_selector)``
814+
----------------------------------------------------------
815+
816+
Parameters
817+
- `second` (``Iterable[TKey]``)
818+
- `key_selector` (``Callable[[TSource_co], TKey]``)
819+
820+
Returns
821+
- ``Enumerable[TSource_co]``
822+
823+
Produces the set difference of two sequences: self - second, according to a key selector that
824+
determines "distinctness".
825+
826+
Example
827+
>>> first = [(16, 'x'), (9, 'y'), (12, 'd'), (16, 't')]
828+
>>> second = ['y', 'd']
829+
>>> Enumerable(first).except_by(second, lambda x: x[1]).to_list()
830+
[(16, 'x'), (16, 't')]
831+
832+
----
833+
734834
instancemethod ``first()``
735835
----------------------------
736836

@@ -1026,6 +1126,26 @@ Example
10261126

10271127
----
10281128

1129+
instancemethod ``intersect_by[TKey](second, key_selector)``
1130+
-------------------------------------------------------------
1131+
1132+
Parameters
1133+
- `second` (``Iterable[TKey]``)
1134+
- `key_selector` (``Callable[[TSource_co], TKey]``)
1135+
1136+
Returns
1137+
- ``Enumerable[TSource_co]``
1138+
1139+
Produces the set intersection of two sequences: self * second according to a
1140+
specified key selector.
1141+
1142+
Example
1143+
>>> strs = ['+1', '-3', '+5', '-7', '+9', '-11']
1144+
>>> Enumerable(strs).intersect_by([1, 2, 3, 5, 9], lambda x: abs(int(x))).to_list()
1145+
['+1', '-3', '+5', '+9']
1146+
1147+
----
1148+
10291149
instancemethod ``join[TInner, TKey, TResult](inner, outer_key_selector, inner_key_selector, result_selector)``
10301150
----------------------------------------------------------------------------------------------------------------
10311151

@@ -1237,6 +1357,43 @@ Example
12371357

12381358
----
12391359

1360+
instancemethod ``max_by[TSupportsLessThan](key_selector)``
1361+
------------------------------------------------------------
1362+
1363+
Parameters
1364+
- `key_selector` (``Callable[[TSource_co], TSupportsLessThan]``)
1365+
1366+
Returns
1367+
- ``TSource_co``
1368+
1369+
Returns the maximal element of the sequence based on the given key selector. Raises
1370+
`InvalidOperationError` if there is no value.
1371+
1372+
Example
1373+
>>> strs = ['aaa', 'bb', 'c', 'dddd']
1374+
>>> Enumerable(strs).max_by(len)
1375+
'dddd'
1376+
1377+
----
1378+
1379+
instancemethod ``max_by[TKey](key_selector, __comparer)``
1380+
-----------------------------------------------------------
1381+
1382+
Parameters
1383+
- `key_selector` (``Callable[[TSource_co], TKey]``)
1384+
- `__comparer` (``Callable[[TKey, TKey], int]``)
1385+
1386+
Returns
1387+
- ``TSource_co``
1388+
1389+
Returns the maximal element of the sequence based on the given key selector and the comparer.
1390+
Raises `InvalidOperationError` if there is no value.
1391+
1392+
Such comparer takes two values and return positive ints when lhs > rhs, negative ints
1393+
if lhs < rhs, and 0 if they are equal.
1394+
1395+
----
1396+
12401397
instancemethod ``min[TSupportsLessThan]()``
12411398
---------------------------------------------
12421399

@@ -1294,6 +1451,38 @@ resulting values. Returns the default one if there is no value.
12941451

12951452
----
12961453

1454+
instancemethod ``min_by[TSupportsLessThan](key_selector)``
1455+
------------------------------------------------------------
1456+
1457+
Parameters
1458+
- `key_selector` (``Callable[[TSource_co], TSupportsLessThan]``)
1459+
1460+
Returns
1461+
- ``TSource_co``
1462+
1463+
Returns the minimal element of the sequence based on the given key selector. Raises
1464+
`InvalidOperationError` if there is no value.
1465+
1466+
----
1467+
1468+
instancemethod ``min_by[TKey](key_selector, __comparer)``
1469+
-----------------------------------------------------------
1470+
1471+
Parameters
1472+
- `key_selector` (``Callable[[TSource_co], TKey]``)
1473+
- `__comparer` (``Callable[[TKey, TKey], int]``)
1474+
1475+
Returns
1476+
- ``TSource_co``
1477+
1478+
Returns the minimal element of the sequence based on the given key selector and the comparer.
1479+
Raises `InvalidOperationError` if there is no value.
1480+
1481+
Such comparer takes two values and return positive ints when lhs > rhs, negative ints
1482+
if lhs < rhs, and 0 if they are equal.
1483+
1484+
----
1485+
12971486
instancemethod ``of_type[TResult](t_result)``
12981487
-----------------------------------------------
12991488

@@ -1926,6 +2115,30 @@ Example
19262115

19272116
----
19282117

2118+
instancemethod ``take(__index)``
2119+
----------------------------------
2120+
2121+
Parameters
2122+
- `__index` (``slice``)
2123+
2124+
Returns
2125+
- ``Enumerable[TSource_co]``
2126+
2127+
Produces a subsequence defined by the given slice notation.
2128+
2129+
This method currently is identical to `elements_in()` when it takes a slice.
2130+
2131+
Example
2132+
.. code-block:: python
2133+
2134+
>>> def gen():
2135+
... yield 1; yield 10; yield 100; yield 1000; yield 10000
2136+
2137+
>>> Enumerable(gen()).take(slice(1, 3)).to_list()
2138+
[10, 100]
2139+
2140+
----
2141+
19292142
instancemethod ``take_last(count)``
19302143
-------------------------------------
19312144

@@ -2089,6 +2302,26 @@ Example
20892302

20902303
----
20912304

2305+
instancemethod ``union_by(second, key_selector)``
2306+
---------------------------------------------------
2307+
2308+
Parameters
2309+
- `second` (``Iterable[TSource_co]``)
2310+
- `key_selector` (``Callable[[TSource_co], object]``)
2311+
2312+
Returns
2313+
- ``Enumerable[TSource_co]``
2314+
2315+
Produces the set union of two sequences: self + second according to a specified key
2316+
selector.
2317+
2318+
Example
2319+
>>> en = Enumerable([1, 9, -2, -7, 14])
2320+
>>> en.union_by([15, 2, -26, -7], abs).to_list()
2321+
[1, 9, -2, -7, 14, 15, -26] # abs(-2) == abs(2)
2322+
2323+
----
2324+
20922325
instancemethod ``where(predicate)``
20932326
-------------------------------------
20942327

@@ -2306,6 +2539,9 @@ Returns
23062539

23072540
Produces a subsequence defined by the given slice notation.
23082541

2542+
This method always uses a generic list slicing method regardless the implementation of the
2543+
wrapped iterable.
2544+
23092545
Example
23102546
.. code-block:: python
23112547
@@ -2330,6 +2566,8 @@ Returns
23302566

23312567
Produces a subsequence with indices that define a slice.
23322568

2569+
This method always uses a generic list slicing method regardless the implementation of the
2570+
wrapped iterable.
23332571

23342572
Example
23352573
.. code-block:: python

0 commit comments

Comments
 (0)