From 4ea6619556e02f218fa9b4c893d2d2aa41479c69 Mon Sep 17 00:00:00 2001 From: Ryan Grout Date: Thu, 9 Aug 2018 21:43:53 -0500 Subject: [PATCH 1/3] Avoid overhead of islice. --- toolz/itertoolz.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index 71c7ddc0..bf91903f 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -374,7 +374,9 @@ def second(seq): >>> second('ABC') 'B' """ - return next(itertools.islice(seq, 1, None)) + seq = iter(seq) + next(seq) + return next(seq) def nth(n, seq): From 701712f8947aa845b01eb90505104885dbc95a26 Mon Sep 17 00:00:00 2001 From: Ryan Grout Date: Tue, 14 Aug 2018 21:44:59 -0500 Subject: [PATCH 2/3] Handle StopIteration in Python 3.7+ Add more tests for edge cases --- toolz/itertoolz.py | 7 +++++-- toolz/tests/test_itertoolz.py | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index bf91903f..b8028c49 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -375,8 +375,11 @@ def second(seq): 'B' """ seq = iter(seq) - next(seq) - return next(seq) + try: + next(seq) + return next(seq) + except StopIteration: + raise def nth(n, seq): diff --git a/toolz/tests/test_itertoolz.py b/toolz/tests/test_itertoolz.py index e0a8d17f..bd83db2f 100644 --- a/toolz/tests/test_itertoolz.py +++ b/toolz/tests/test_itertoolz.py @@ -17,6 +17,8 @@ from toolz.compatibility import range, filter from operator import add, mul +import pytest + # is comparison will fail between this and no_default no_default2 = loads(dumps('__no__default__')) @@ -143,6 +145,12 @@ def test_second(): assert second('ABCDE') == 'B' assert second((3, 2, 1)) == 2 assert isinstance(second({0: 'zero', 1: 'one'}), int) + assert second(x for x in range(2)) == 1 + + # Python 3.7, StopIteration should be raised if iterable too short + with pytest.raises(StopIteration): + second([]) + second([0]) def test_last(): From 307b0bbdc1d7247b9d86ea3abf5b1f89648b9f60 Mon Sep 17 00:00:00 2001 From: Ryan Grout Date: Tue, 14 Aug 2018 22:14:54 -0500 Subject: [PATCH 3/3] Revert "Handle StopIteration in Python 3.7+" This reverts commit 701712f8947aa845b01eb90505104885dbc95a26. --- toolz/itertoolz.py | 7 ++----- toolz/tests/test_itertoolz.py | 8 -------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/toolz/itertoolz.py b/toolz/itertoolz.py index b8028c49..bf91903f 100644 --- a/toolz/itertoolz.py +++ b/toolz/itertoolz.py @@ -375,11 +375,8 @@ def second(seq): 'B' """ seq = iter(seq) - try: - next(seq) - return next(seq) - except StopIteration: - raise + next(seq) + return next(seq) def nth(n, seq): diff --git a/toolz/tests/test_itertoolz.py b/toolz/tests/test_itertoolz.py index bd83db2f..e0a8d17f 100644 --- a/toolz/tests/test_itertoolz.py +++ b/toolz/tests/test_itertoolz.py @@ -17,8 +17,6 @@ from toolz.compatibility import range, filter from operator import add, mul -import pytest - # is comparison will fail between this and no_default no_default2 = loads(dumps('__no__default__')) @@ -145,12 +143,6 @@ def test_second(): assert second('ABCDE') == 'B' assert second((3, 2, 1)) == 2 assert isinstance(second({0: 'zero', 1: 'one'}), int) - assert second(x for x in range(2)) == 1 - - # Python 3.7, StopIteration should be raised if iterable too short - with pytest.raises(StopIteration): - second([]) - second([0]) def test_last():