Python 3: dict.iteritems() and dict.itervalues()

* Replace dict.itervalues() with dict.values(). The Python 3 dict
  type has no itervalues() method, the old itervalues() method was
  renamed to values().
* Same change for dict.iteritems(), replaced with dict.items()
* Exception: use six.itervalues() to yield on sock_data_by_port

Using six.itervalues() and six.iteritems() would make the code less
readable. The overhead of creating a temporary list is considered as
negligible:
http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html

Change-Id: Ifbe7faa16d419e7fe26f1fb464019b83c9171c45
This commit is contained in:
Victor Stinner 2016-06-23 13:34:51 +02:00
parent a60096769c
commit b923e0f892
4 changed files with 10 additions and 6 deletions

View File

@ -31,6 +31,7 @@ import eventlet.debug
from eventlet import greenio, GreenPool, sleep, wsgi, listen, Timeout from eventlet import greenio, GreenPool, sleep, wsgi, listen, Timeout
from paste.deploy import loadwsgi from paste.deploy import loadwsgi
from eventlet.green import socket, ssl, os as green_os from eventlet.green import socket, ssl, os as green_os
import six
from six import BytesIO from six import BytesIO
from six import StringIO from six import StringIO
from six.moves.urllib.parse import unquote from six.moves.urllib.parse import unquote
@ -643,7 +644,10 @@ class PortPidState(object):
Yield all current listen sockets. Yield all current listen sockets.
""" """
for orphan_data in self.sock_data_by_port.itervalues(): # Use six.itervalues() instead of calling directly the .values() method
# on Python 2 to avoid a temporary list, because sock_data_by_port
# comes from users and can be large.
for orphan_data in six.itervalues(self.sock_data_by_port):
yield orphan_data['sock'] yield orphan_data['sock']
def forget_port(self, port): def forget_port(self, port):

View File

@ -861,7 +861,7 @@ class ContainerBroker(DatabaseBroker):
'DELETE FROM object WHERE ' + query_mod + 'DELETE FROM object WHERE ' + query_mod +
'name=? AND storage_policy_index=?', 'name=? AND storage_policy_index=?',
((rec['name'], rec['storage_policy_index']) ((rec['name'], rec['storage_policy_index'])
for rec in to_delete.itervalues())) for rec in to_delete.values()))
if to_add: if to_add:
curs.executemany( curs.executemany(
'INSERT INTO object (name, created_at, size, content_type,' 'INSERT INTO object (name, created_at, size, content_type,'
@ -870,7 +870,7 @@ class ContainerBroker(DatabaseBroker):
((rec['name'], rec['created_at'], rec['size'], ((rec['name'], rec['created_at'], rec['size'],
rec['content_type'], rec['etag'], rec['deleted'], rec['content_type'], rec['etag'], rec['deleted'],
rec['storage_policy_index']) rec['storage_policy_index'])
for rec in to_add.itervalues())) for rec in to_add.values()))
if source: if source:
# for replication we rely on the remote end sending merges in # for replication we rely on the remote end sending merges in
# order with no gaps to increment sync_points # order with no gaps to increment sync_points

View File

@ -300,7 +300,7 @@ class TestReconSuccess(TestCase):
array.array('H', [4, 2, 4, 3])] array.array('H', [4, 2, 4, 3])]
} }
for ringfn, replica_map in rings.iteritems(): for ringfn, replica_map in rings.items():
ringpath = os.path.join(self.tempdir, ringfn) ringpath = os.path.join(self.tempdir, ringfn)
self._create_ring(ringpath, replica_map, self.ring_devs, self._create_ring(ringpath, replica_map, self.ring_devs,
self.ring_part_shift) self.ring_part_shift)

View File

@ -691,9 +691,9 @@ class TestDatabaseBroker(unittest.TestCase):
self.assertTrue(stub_called[0]) self.assertTrue(stub_called[0])
# ensure that metadata was cleared # ensure that metadata was cleared
m2 = broker.metadata m2 = broker.metadata
self.assertTrue(not any(v[0] for v in m2.itervalues())) self.assertTrue(not any(v[0] for v in m2.values()))
self.assertTrue(all(v[1] == normalize_timestamp('2') self.assertTrue(all(v[1] == normalize_timestamp('2')
for v in m2.itervalues())) for v in m2.values()))
def test_get(self): def test_get(self):
broker = DatabaseBroker(':memory:') broker = DatabaseBroker(':memory:')