diff options
author | Raymond Hettinger <python@rcn.com> | 2003-11-06 14:06:48 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-11-06 14:06:48 (GMT) |
commit | 85c20a41dfcec04d161ad7da7260e7b94c62d228 (patch) | |
tree | 0d9e5b294ab4890b72ddc61d193036ac1d4b5ca4 /Lib | |
parent | f607fc5395883ff924c76739e9b0921953568e54 (diff) | |
download | cpython-85c20a41dfcec04d161ad7da7260e7b94c62d228.zip cpython-85c20a41dfcec04d161ad7da7260e7b94c62d228.tar.gz cpython-85c20a41dfcec04d161ad7da7260e7b94c62d228.tar.bz2 |
Implement and apply PEP 322, reverse iteration
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/heapq.py | 2 | ||||
-rw-r--r-- | Lib/mhlib.py | 3 | ||||
-rwxr-xr-x | Lib/platform.py | 2 | ||||
-rw-r--r-- | Lib/random.py | 2 | ||||
-rw-r--r-- | Lib/rfc822.py | 3 | ||||
-rw-r--r-- | Lib/test/test_enumerate.py | 20 |
6 files changed, 24 insertions, 8 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py index 2c30b12..2223a97 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -165,7 +165,7 @@ def heapify(x): # or i < (n-1)/2. If n is even = 2*j, this is (2*j-1)/2 = j-1/2 so # j-1 is the largest, which is n//2 - 1. If n is odd = 2*j+1, this is # (2*j+1-1)/2 = j so j-1 is the largest, and that's again n//2-1. - for i in xrange(n//2 - 1, -1, -1): + for i in reversed(xrange(n//2)): _siftup(x, i) # 'heap' is a heap at all indices >= startpos, except possibly for pos. pos diff --git a/Lib/mhlib.py b/Lib/mhlib.py index 5520f82..899939a 100644 --- a/Lib/mhlib.py +++ b/Lib/mhlib.py @@ -975,8 +975,7 @@ def test(): print seqs f.putsequences(seqs) do('f.getsequences()') - testfolders.reverse() - for t in testfolders: do('mh.deletefolder(%s)' % `t`) + for t in reversed(testfolders): do('mh.deletefolder(%s)' % `t`) do('mh.getcontext()') context = mh.getcontext() f = mh.openfolder(context) diff --git a/Lib/platform.py b/Lib/platform.py index 31c47b4..389e50c 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -201,7 +201,7 @@ def _dist_try_harder(distname,version,id): if os.path.isdir('/usr/lib/setup'): # Check for slackware verson tag file (thanks to Greg Andruk) verfiles = os.listdir('/usr/lib/setup') - for n in range(len(verfiles)-1, -1, -1): + for n in reversed(xrange(len(verfiles))): if verfiles[n][:14] != 'slack-version-': del verfiles[n] if verfiles: diff --git a/Lib/random.py b/Lib/random.py index 16b667a..1f279bb 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -253,7 +253,7 @@ class Random(_random.Random): if random is None: random = self.random - for i in xrange(len(x)-1, 0, -1): + for i in reversed(xrange(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = int(random() * (i+1)) x[i], x[j] = x[j], x[i] diff --git a/Lib/rfc822.py b/Lib/rfc822.py index 4f69b22..9a52a90 100644 --- a/Lib/rfc822.py +++ b/Lib/rfc822.py @@ -421,8 +421,7 @@ class Message: hit = 0 if hit: list.append(i) - list.reverse() - for i in list: + for i in reversed(list): del self.headers[i] def setdefault(self, name, default=""): diff --git a/Lib/test/test_enumerate.py b/Lib/test/test_enumerate.py index 88e24e8..b073606 100644 --- a/Lib/test/test_enumerate.py +++ b/Lib/test/test_enumerate.py @@ -124,9 +124,27 @@ class TestBig(EnumerateTestCase): seq = range(10,20000,2) res = zip(range(20000), seq) +class TestReversed(unittest.TestCase): + + def test_simple(self): + class A: + def __getitem__(self, i): + if i < 5: + return str(i) + raise StopIteration + def __len__(self): + return 5 + for data in 'abc', range(5), tuple(enumerate('abc')), A(), xrange(1,17,5): + self.assertEqual(list(data)[::-1], list(reversed(data))) + self.assertRaises(TypeError, reversed, {}) + + def test_xrange_optimization(self): + x = xrange(1) + self.assertEqual(type(reversed(x)), type(iter(x))) def test_main(verbose=None): - testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig) + testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig, + TestReversed) test_support.run_unittest(*testclasses) # verify reference counting |