cpython: cabd7261ae80 (original) (raw)
Mercurial > cpython
changeset 96227:cabd7261ae80
Issue #23086: Add start and stop arguments to the Sequence.index() mixin method. [#23086]
Raymond Hettinger python@rcn.com | |
---|---|
date | Fri, 22 May 2015 19:29:22 -0700 |
parents | 29b95625a07c |
children | e729b946cc03 |
files | Doc/library/collections.abc.rst Lib/_collections_abc.py Lib/test/test_collections.py Misc/ACKS Misc/NEWS |
diffstat | 5 files changed, 69 insertions(+), 5 deletions(-)[+] [-] Doc/library/collections.abc.rst 14 Lib/_collections_abc.py 20 Lib/test/test_collections.py 35 Misc/ACKS 1 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Doc/library/collections.abc.rst
+++ b/Doc/library/collections.abc.rst
@@ -121,6 +121,20 @@ ABC Inherits from
ABCs for read-only and mutable :term:sequences <sequence>
.
- Implementation note: Some of the mixin methods, such as
- :meth:
__iter__
, :meth:__reversed__
and :meth:index
, make - repeated calls to the underlying :meth:
__getitem__
method. - Consequently, if :meth:
__getitem__
is implemented with constant - access speed, the mixin methods will have linear performance;
- however, if the underlying method is linear (as it would be with a
- linked list), the mixins will have quadratic performance and will
- likely need to be overridden. +
- .. versionchanged:: 3.5
The index() method added support for *stop* and *start*[](#l1.17)
arguments.[](#l1.18)
--- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -825,13 +825,23 @@ class Sequence(Sized, Iterable, Containe for i in reversed(range(len(self))): yield self[i]
- def index(self, value, start=0, stop=None):
'''S.index(value, [start, [stop]]) -> integer -- return first index of value.[](#l2.10) Raises ValueError if the value is not present.[](#l2.11) '''[](#l2.12)
for i, v in enumerate(self):[](#l2.13)
if v == value:[](#l2.14)
return i[](#l2.15)
if start is not None and start < 0:[](#l2.16)
start = max(len(self) + start, 0)[](#l2.17)
if stop is not None and stop < 0:[](#l2.18)
stop += len(self)[](#l2.19)
i = start[](#l2.21)
while stop is None or i < stop:[](#l2.22)
try:[](#l2.23)
if self[i] == value:[](#l2.24)
return i[](#l2.25)
except IndexError:[](#l2.26)
break[](#l2.27)
i += 1[](#l2.28) raise ValueError[](#l2.29)
--- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1227,6 +1227,41 @@ class TestCollectionABCs(ABCTestCase): self.validate_abstract_methods(Sequence, 'contains', 'iter', 'len', 'getitem')
- def test_Sequence_mixins(self):
class SequenceSubclass(Sequence):[](#l3.8)
def __init__(self, seq=()):[](#l3.9)
self.seq = seq[](#l3.10)
def __getitem__(self, index):[](#l3.12)
return self.seq[index][](#l3.13)
def __len__(self):[](#l3.15)
return len(self.seq)[](#l3.16)
# Compare Sequence.index() behavior to (list|str).index() behavior[](#l3.18)
def assert_index_same(seq1, seq2, index_args):[](#l3.19)
try:[](#l3.20)
expected = seq1.index(*index_args)[](#l3.21)
except ValueError:[](#l3.22)
with self.assertRaises(ValueError):[](#l3.23)
seq2.index(*index_args)[](#l3.24)
else:[](#l3.25)
actual = seq2.index(*index_args)[](#l3.26)
self.assertEqual([](#l3.27)
actual, expected, '%r.index%s' % (seq1, index_args))[](#l3.28)
for ty in list, str:[](#l3.30)
nativeseq = ty('abracadabra')[](#l3.31)
indexes = [-10000, -9999] + list(range(-3, len(nativeseq) + 3))[](#l3.32)
seqseq = SequenceSubclass(nativeseq)[](#l3.33)
for letter in set(nativeseq) | {'z'}:[](#l3.34)
assert_index_same(nativeseq, seqseq, (letter,))[](#l3.35)
for start in range(-3, len(nativeseq) + 3):[](#l3.36)
assert_index_same(nativeseq, seqseq, (letter, start))[](#l3.37)
for stop in range(-3, len(nativeseq) + 3):[](#l3.38)
assert_index_same([](#l3.39)
nativeseq, seqseq, (letter, start, stop))[](#l3.40)
+ def test_ByteString(self): for sample in [bytes, bytearray]: self.assertIsInstance(sample(), ByteString)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -660,6 +660,7 @@ Bill Janssen Thomas Jarosch Juhana Jauhiainen Rajagopalasarma Jayakrishnan +Devin Jeanpierre Zbigniew Jędrzejewski-Szmek Julien Jehannet Muhammad Jehanzeb