Skip to content

Commit 2fbfcc0

Browse files
committed
add range(), increase to 0.0.4
as well as add some class comment strings
1 parent 904bdba commit 2fbfcc0

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name='types-linq',
8-
version='v0.0.3',
8+
version='v0.0.4',
99
url='https://github.com/cleoold/types-linq',
1010
license='GNU General Public License v3',
1111
author='cleoold',

tests/test_usage.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22
from typing import Generic, Iterable, List, NamedTuple, Sequence, Tuple, TypeVar, cast
3+
from _pytest.python_api import raises
34

45
import pytest
56

@@ -815,6 +816,31 @@ def test_no_mutate(self):
815816
assert en2.to_list() == [8, 7, 10]
816817

817818

819+
class TestRangeMethod:
820+
def test_range(self):
821+
ints = Enumerable.range(0, 6)
822+
assert ints.to_list() == [0, 1, 2, 3, 4, 5]
823+
ints2 = Enumerable.range(1, 10).select(lambda x: x * x)
824+
assert ints2.to_list() == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
825+
826+
def test_no_elem(self):
827+
ints = Enumerable.range(5, 0)
828+
assert ints.to_list() == []
829+
830+
def test_1_elem(self):
831+
ints = Enumerable.range(9, 1)
832+
assert ints.to_list() == [9]
833+
834+
def test_invalid(self):
835+
with pytest.raises(ValueError):
836+
Enumerable.range(99, -1)
837+
838+
def test_no_end(self):
839+
ints = Enumerable.range(77, None)
840+
lst = ints.take(100).to_list()
841+
assert lst == [*range(77, 177)]
842+
843+
818844
class TestSelectMethod:
819845
def test_select(self):
820846
gen_func = lambda: (i for i in range(4))

types_linq/enumerable.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Any, Callable, Container, Dict, Iterable, Iterator, List, NoReturn, Reversible, Sequence, Set, Sized, TYPE_CHECKING, Tuple, Type, Generic, Union
2+
from typing import Any, Callable, Container, Dict, Iterable, Iterator, List, NoReturn, Optional, Reversible, Sequence, Set, Sized, TYPE_CHECKING, Tuple, Type, Generic, Union
33

44
if TYPE_CHECKING:
55
from .lookup import Lookup
@@ -521,6 +521,23 @@ def inner():
521521
yield from self
522522
return Enumerable(inner)
523523

524+
@staticmethod
525+
def range(start: int, count: Optional[int]) -> Enumerable[int]:
526+
if count is not None:
527+
if count < 0:
528+
raise ValueError('count must be nonnegative')
529+
def inner(curr=start, cnt=count):
530+
while cnt > 0:
531+
yield curr
532+
curr += 1
533+
cnt -= 1
534+
else:
535+
def inner(curr=start):
536+
while True:
537+
yield curr
538+
curr += 1
539+
return Enumerable(inner)
540+
524541
def reverse(self) -> Enumerable[TSource_co]:
525542
return Enumerable(lambda: self._reversed_impl(fallback=True))
526543

types_linq/enumerable.pyi

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, Dict, Generic, Iterable, Iterator, List, Sequence, Set, Tuple, Type, Union, overload
1+
from typing import Callable, Dict, Generic, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Type, Union, overload
22

33
from .lookup import Lookup
44
from .grouping import Grouping
@@ -19,6 +19,9 @@ from .more_typing import (
1919

2020

2121
class Enumerable(Sequence[TSource_co], Generic[TSource_co]):
22+
'''
23+
Provides a set of helper methods for querying iterable objects.
24+
'''
2225

2326
@overload
2427
def __init__(self, iterable: Iterable[TSource_co]):
@@ -468,6 +471,15 @@ class Enumerable(Sequence[TSource_co], Generic[TSource_co]):
468471
Adds a value to the beginning of the sequence.
469472
'''
470473

474+
# count: Optional[int] is nonstandard behavior
475+
@staticmethod
476+
def range(start: int, count: Optional[int]) -> Enumerable[int]:
477+
'''
478+
Generates a sequence of `count` integral numbers from `start`, incrementing each by one.
479+
480+
If `count` is `None`, the sequence is infinite.
481+
'''
482+
471483
# @@@ TODO
472484

473485
def reverse(self) -> Enumerable[TSource_co]:

types_linq/grouping.py

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# kind of respect IGrouping's covariant type parameters if the method _append()
1313
# is really treated as an internal method
1414
class Grouping(Enumerable[TValue_co], Generic[TKey_co, TValue_co]):
15+
'''
16+
Represents a collection of objects that have a common key.
17+
'''
1518

1619
_key: TKey_co
1720
_values: List[TValue_co]

types_linq/lookup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# weird.
1717
class Lookup(Enumerable[Grouping[TKey_co, TValue_co]]):
1818
'''
19-
A lookup is a one-to-many dictionary.
19+
A lookup is a one-to-many dictionary. It maps keys to Enumerable sequences of values.
2020
'''
2121

2222
_groupings: Dict[TKey_co, Grouping[TKey_co, TValue_co]]

types_linq/ordered_enumerable.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ from .more_typing import (
1111

1212

1313
class OrderedEnumerable(Enumerable[TSource_co], Generic[TSource_co, TKey]):
14+
'''
15+
Represents a sorted Enumerable sequence that is sorted by some key.
16+
'''
1417

1518
def __init__(self, *args): ...
1619

0 commit comments

Comments
 (0)