Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
325191bd
Kaydet (Commit)
325191bd
authored
Nis 02, 2018
tarafından
Cheryl Sabella
Kaydeden (comit)
Raymond Hettinger
Nis 02, 2018
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
[2.7] bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195) (GH-6339)
(cherry picked from commit
da1734c5
)
üst
72f3e088
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
9 deletions
+90
-9
itertools.rst
Doc/library/itertools.rst
+20
-8
test_itertools.py
Lib/test/test_itertools.py
+68
-1
2018-03-22-19-23-04.bpo-27212.wrE5KR.rst
...xt/Documentation/2018-03-22-19-23-04.bpo-27212.wrE5KR.rst
+2
-0
No files found.
Doc/library/itertools.rst
Dosyayı görüntüle @
325191bd
...
...
@@ -411,12 +411,24 @@ loops that truncate the stream.
# islice('ABCDEFG', 2, None) --> C D E F G
# islice('ABCDEFG', 0, None, 2) --> A C E G
s = slice(*args)
it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
nexti = next(it)
for i, element in enumerate(iterable):
if i == nexti:
yield element
nexti = next(it)
start, stop, step = s.start or 0, s.stop or sys.maxint, s.step or 1
it = iter(xrange(start, stop, step)))
try:
nexti = next(it)
except StopIteration:
# Consume *iterable* up to the *start* position.
for i, element in izip(xrange(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 izip(xrange(i + 1, stop), iterable):
pass
If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
then the step defaults to one.
...
...
@@ -681,8 +693,8 @@ which incur interpreter overhead.
"Return function(0), function(1), ..."
return imap(function, count(start))
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is
n
one, consume entirely."
def consume(iterator, n
=None
):
"Advance the iterator n-steps ahead. If n is
N
one, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
...
...
Lib/test/test_itertools.py
Dosyayı görüntüle @
325191bd
...
...
@@ -802,6 +802,7 @@ class TestBasicOps(unittest.TestCase):
(
10
,
20
,
3
),
(
10
,
3
,
20
),
(
10
,
20
),
(
10
,
10
),
(
10
,
3
),
(
20
,)
]:
...
...
@@ -826,6 +827,10 @@ class TestBasicOps(unittest.TestCase):
self
.
assertEqual
(
list
(
islice
(
it
,
3
)),
range
(
3
))
self
.
assertEqual
(
list
(
it
),
range
(
3
,
10
))
it
=
iter
(
range
(
10
))
self
.
assertEqual
(
list
(
islice
(
it
,
3
,
3
)),
[])
self
.
assertEqual
(
list
(
it
),
range
(
3
,
10
))
# Test invalid arguments
self
.
assertRaises
(
TypeError
,
islice
,
xrange
(
10
))
self
.
assertRaises
(
TypeError
,
islice
,
xrange
(
10
),
1
,
2
,
3
,
4
)
...
...
@@ -1084,6 +1089,48 @@ class TestExamples(unittest.TestCase):
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
.
maxint
,
s
.
step
or
1
it
=
iter
(
xrange
(
start
,
stop
,
step
))
try
:
nexti
=
next
(
it
)
except
StopIteration
:
# Consume *iterable* up to the *start* position.
for
i
,
element
in
izip
(
xrange
(
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
izip
(
xrange
(
i
+
1
,
stop
),
iterable
):
pass
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
(
xrange
(
10
))
self
.
assertEqual
(
list
(
self
.
islice
(
it
,
3
)),
range
(
3
))
self
.
assertEqual
(
list
(
it
),
range
(
3
,
10
))
it
=
iter
(
xrange
(
10
))
self
.
assertEqual
(
list
(
self
.
islice
(
it
,
3
,
3
)),
[])
self
.
assertEqual
(
list
(
it
),
range
(
3
,
10
))
# Test that slice finishes in predictable state.
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
):
...
...
@@ -1577,6 +1624,17 @@ Samuele
... "Return function(0), function(1), ..."
... return imap(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)
...
...
@@ -1678,6 +1736,14 @@ perform as purported.
>>> list(islice(tabulate(lambda x: 2*x), 4))
[0, 2, 4, 6]
>>> it = iter(xrange(10))
>>> consume(it, 3)
>>> next(it)
3
>>> consume(it)
>>> next(it, 'Done')
'Done'
>>> nth('abcde', 3)
'd'
...
...
@@ -1753,7 +1819,8 @@ __test__ = {'libreftest' : libreftest}
def
test_main
(
verbose
=
None
):
test_classes
=
(
TestBasicOps
,
TestVariousIteratorArgs
,
TestGC
,
RegressionTests
,
LengthTransparency
,
SubclassWithKwargsTest
,
TestExamples
)
SubclassWithKwargsTest
,
TestExamples
,
TestPurePythonRoughEquivalents
)
test_support
.
run_unittest
(
*
test_classes
)
# verify reference counting
...
...
Misc/NEWS.d/next/Documentation/2018-03-22-19-23-04.bpo-27212.wrE5KR.rst
0 → 100644
Dosyayı görüntüle @
325191bd
Modify documentation for the :func:`islice` recipe to consume initial values
up to the start index.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment