-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-27212: Modify islice recipe to consume initial values preceding start #6195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1192,6 +1192,7 @@ def test_islice(self): | |
(10, 20, 3), | ||
(10, 3, 20), | ||
(10, 20), | ||
(10, 10), | ||
(10, 3), | ||
(20,) | ||
]: | ||
|
@@ -1218,6 +1219,10 @@ def test_islice(self): | |
self.assertEqual(list(islice(it, 3)), list(range(3))) | ||
self.assertEqual(list(it), list(range(3, 10))) | ||
|
||
it = iter(range(10)) | ||
self.assertEqual(list(islice(it, 3, 3)), []) | ||
self.assertEqual(list(it), list(range(3, 10))) | ||
|
||
# Test invalid arguments | ||
ra = range(10) | ||
self.assertRaises(TypeError, islice, ra) | ||
|
@@ -1604,6 +1609,49 @@ def test_takewhile(self): | |
self.assertEqual(list(takewhile(lambda x: x<5, [1,4,6,4,1])), [1,4]) | ||
|
||
|
||
class TestPurePythonRoughEquivalents(unittest.TestCase): | ||
|
||
@staticmethod | ||
def islice(iterable, *args): | ||
s = slice(*args) | ||
start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1 | ||
it = iter(range(start, stop, step)) | ||
try: | ||
nexti = next(it) | ||
except StopIteration: | ||
# Consume *iterable* up to the *start* position. | ||
for i, element in zip(range(start), iterable): | ||
pass | ||
return | ||
try: | ||
for i, element in enumerate(iterable): | ||
if i == nexti: | ||
yield element | ||
nexti = next(it) | ||
except StopIteration: | ||
# Consume to *stop*. | ||
for i, element in zip(range(i + 1, stop), iterable): | ||
pass | ||
return | ||
|
||
def test_islice_recipe(self): | ||
self.assertEqual(list(self.islice('ABCDEFG', 2)), list('AB')) | ||
self.assertEqual(list(self.islice('ABCDEFG', 2, 4)), list('CD')) | ||
self.assertEqual(list(self.islice('ABCDEFG', 2, None)), list('CDEFG')) | ||
self.assertEqual(list(self.islice('ABCDEFG', 0, None, 2)), list('ACEG')) | ||
# Test items consumed. | ||
it = iter(range(10)) | ||
self.assertEqual(list(self.islice(it, 3)), list(range(3))) | ||
self.assertEqual(list(it), list(range(3, 10))) | ||
it = iter(range(10)) | ||
self.assertEqual(list(self.islice(it, 3, 3)), []) | ||
self.assertEqual(list(it), list(range(3, 10))) | ||
# Test that slice finishes in predictable state. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the part that I'm still thinking about. |
||
c = count() | ||
self.assertEqual(list(self.islice(c, 1, 3, 50)), [1]) | ||
self.assertEqual(next(c), 3) | ||
|
||
|
||
class TestGC(unittest.TestCase): | ||
|
||
def makecycle(self, iterator, container): | ||
|
@@ -2158,6 +2206,17 @@ def test_permutations_sizeof(self): | |
... "Return function(0), function(1), ..." | ||
... return map(function, count(start)) | ||
|
||
>>> import collections | ||
>>> def consume(iterator, n=None): | ||
... "Advance the iterator n-steps ahead. If n is None, consume entirely." | ||
... # Use functions that consume iterators at C speed. | ||
... if n is None: | ||
... # feed the entire iterator into a zero-length deque | ||
... collections.deque(iterator, maxlen=0) | ||
... else: | ||
... # advance to the empty slice starting at position n | ||
... next(islice(iterator, n, n), None) | ||
|
||
>>> def nth(iterable, n, default=None): | ||
... "Returns the nth item or a default value" | ||
... return next(islice(iterable, n, None), default) | ||
|
@@ -2298,6 +2357,14 @@ def test_permutations_sizeof(self): | |
>>> list(islice(tabulate(lambda x: 2*x), 4)) | ||
[0, 2, 4, 6] | ||
|
||
>>> it = iter(range(10)) | ||
>>> consume(it, 3) | ||
>>> next(it) | ||
3 | ||
>>> consume(it) | ||
>>> next(it, 'Done') | ||
'Done' | ||
|
||
>>> nth('abcde', 3) | ||
'd' | ||
|
||
|
@@ -2386,6 +2453,7 @@ def test_main(verbose=None): | |
test_classes = (TestBasicOps, TestVariousIteratorArgs, TestGC, | ||
RegressionTests, LengthTransparency, | ||
SubclassWithKwargsTest, TestExamples, | ||
TestPurePythonRoughEquivalents, | ||
SizeofTest) | ||
support.run_unittest(*test_classes) | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Documentation/2018-03-22-19-23-04.bpo-27212.wrE5KR.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Modify documentation for the :func:`islice` recipe to consume initial values | ||
up to the start index. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop the "return" on the final line so that it doesn't look like an early-out as opposed to a normal end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.