Skip to content
Snippets Groups Projects
Commit afed5bf5 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

even, odd and count will now accept iterables, 2.7.12

parent a907b3a8
No related branches found
No related tags found
No related merge requests found
# v2.7.12 # v2.7.12
* _TBA_ * bugfix release: `even`, `odd` and `count` will now accept Iterables
# v2.7.11 # v2.7.11
......
__version__ = '2.7.12_a1' __version__ = '2.7.12'
...@@ -3,10 +3,15 @@ import itertools ...@@ -3,10 +3,15 @@ import itertools
import typing as tp import typing as tp
import warnings import warnings
from ..decorators import for_argument
T, U = tp.TypeVar('T'), tp.TypeVar('U') T, U = tp.TypeVar('T'), tp.TypeVar('U')
IteratorOrIterable = tp.Union[tp.Iterator[T], tp.Iterable[T]]
def even(sq: tp.Iterator[T]) -> tp.Iterator[T]: @for_argument(iter)
def even(sq: IteratorOrIterable) -> tp.Iterator[T]:
""" """
Return only elements with even indices in this iterable (first element will be returned, Return only elements with even indices in this iterable (first element will be returned,
as indices are counted from 0) as indices are counted from 0)
...@@ -16,7 +21,8 @@ def even(sq: tp.Iterator[T]) -> tp.Iterator[T]: ...@@ -16,7 +21,8 @@ def even(sq: tp.Iterator[T]) -> tp.Iterator[T]:
next(sq) next(sq)
def odd(sq: tp.Iterator[T]) -> tp.Iterator[T]: @for_argument(iter)
def odd(sq: IteratorOrIterable) -> tp.Iterator[T]:
""" """
Return only elements with odd indices in this iterable. Return only elements with odd indices in this iterable.
""" """
...@@ -25,7 +31,7 @@ def odd(sq: tp.Iterator[T]) -> tp.Iterator[T]: ...@@ -25,7 +31,7 @@ def odd(sq: tp.Iterator[T]) -> tp.Iterator[T]:
yield next(sq) yield next(sq)
def count(sq: tp.Iterator, start: tp.Optional[int] = None, step: int = 1) -> tp.Iterator[int]: def count(sq: IteratorOrIterable, start: tp.Optional[int] = None, step: int = 1) -> tp.Iterator[int]:
""" """
Return a sequence of integers, for each entry in the sequence with provided step. Return a sequence of integers, for each entry in the sequence with provided step.
...@@ -78,9 +84,6 @@ def is_instance(classes: tp.Union[tp.Tuple[type, ...], type]) -> tp.Callable[[ob ...@@ -78,9 +84,6 @@ def is_instance(classes: tp.Union[tp.Tuple[type, ...], type]) -> tp.Callable[[ob
return inner return inner
T = tp.TypeVar('T')
IteratorOrIterable = tp.Union[tp.Iterator[T], tp.Iterable[T]]
def other_sequence_no_longer_than(base_sequence: IteratorOrIterable, def other_sequence_no_longer_than(base_sequence: IteratorOrIterable,
......
...@@ -3,13 +3,18 @@ import unittest ...@@ -3,13 +3,18 @@ import unittest
from satella.coding.sequences import choose, infinite_counter, take_n, is_instance, is_last, \ from satella.coding.sequences import choose, infinite_counter, take_n, is_instance, is_last, \
add_next, half_product, skip_first, zip_shifted, stop_after, group_quantity, \ add_next, half_product, skip_first, zip_shifted, stop_after, group_quantity, \
iter_dict_of_list, shift, other_sequence_no_longer_than, count iter_dict_of_list, shift, other_sequence_no_longer_than, count, even, odd
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class TestSequences(unittest.TestCase): class TestSequences(unittest.TestCase):
def test_even_and_odd(self):
a = [0, 1, 2, 3, 4, 5, 6]
self.assertEqual(list(even(a)), [0, 2, 4, 6])
self.assertEqual(list(odd(a)), [1, 3, 5])
def test_count(self): def test_count(self):
self.assertEqual(list(count([None, None, None], 5, -2)), [5, 3, 1]) self.assertEqual(list(count([None, None, None], 5, -2)), [5, 3, 1])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment