Author: Reuben Cummings <reub...@gmail.com> Branch: py3.5 Changeset: r87666:f5ff941a8b4b Date: 2016-10-09 16:48 +0200 http://bitbucket.org/pypy/pypy/changeset/f5ff941a8b4b/
Log: Implement deque.index and fix spacing diff --git a/pypy/module/_collections/interp_deque.py b/pypy/module/_collections/interp_deque.py --- a/pypy/module/_collections/interp_deque.py +++ b/pypy/module/_collections/interp_deque.py @@ -194,7 +194,6 @@ return space.wrap(copied) - def imul(self, w_int): space = self.space copied = self.copy() @@ -205,7 +204,6 @@ return space.wrap(self) - def copy(self): """ A shallow copy """ space = self.space @@ -343,6 +341,45 @@ def iter(self): return W_DequeIter(self) + def index(self, w_x, w_start=None, w_stop=None): + space = self.space + w_iter = space.iter(self) + _len = self.len + start = 0 + stop = _len + + if w_start is not None: + start = space.int_w(w_start) + + if start < 0: + start += _len + + if start < 0: + start = 0 + elif start > _len: + start = _len + + if w_stop is not None: + stop = space.int_w(w_stop) + + if stop < 0: + stop += _len + + if 0 <= stop > _len: + stop = _len + + for i in range(0, stop): + value = space.next(w_iter) + + if i < start: + continue + + if space.eq_w(value, w_x): + return space.wrap(i) + + x = space.repr(w_x) + raise oefmt(self.space.w_ValueError, "%s is not in deque" % x) + def reviter(self): "Return a reverse iterator over the deque." return W_DequeRevIter(self) @@ -508,6 +545,7 @@ count = interp2app(W_Deque.count), extend = interp2app(W_Deque.extend), extendleft = interp2app(W_Deque.extendleft), + index = interp2app(W_Deque.index), pop = interp2app(W_Deque.pop), popleft = interp2app(W_Deque.popleft), remove = interp2app(W_Deque.remove), diff --git a/pypy/module/_collections/test/test_deque.py b/pypy/module/_collections/test/test_deque.py --- a/pypy/module/_collections/test/test_deque.py +++ b/pypy/module/_collections/test/test_deque.py @@ -290,6 +290,21 @@ mut[0] = 11 assert d == e + def test_index(self): + from _collections import deque + d = deque([1,2,'a',1,2]) + assert d.index(1) is 0 + assert d.index('a') is 2 + assert d.index(1,2) is 3 + assert d.index('a',-3) is 2 + assert d.index('a',-3,-1) is 2 + assert d.index('a',-9) is 2 + raises(ValueError, d.index, 2, 2, -1) + raises(ValueError, d.index, 1, 1, 3) + raises(ValueError, d.index, 'a', -3, -3) + raises(ValueError, d.index, 'a', 1, -3) + raises(ValueError, d.index, 'a', -3, -9) + def test_reversed(self): from _collections import deque for s in ('abcd', range(200)): _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit