Replace six.iteritems() with .items()

1.As mentioned in [1], we should avoid using six.iteritems to achieve iterators.
We can use dict.items instead, as it will return iterators in PY3 as well.
And dict.items/keys will more readable. 2.In py2, the performance about
list should be negligible, see the link [2].
[1] https://wiki.openstack.org/wiki/Python3
[2] http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html

Change-Id: Ia235afc3532f62f265f91ca46d2306c72fc2a2a2
This commit is contained in:
Li-zhigang 2016-11-23 11:18:27 +08:00 committed by Zhigang Li
parent 1346a5e830
commit ea8fad47a5
11 changed files with 14 additions and 20 deletions

View File

@ -89,9 +89,9 @@ def pack_context(msg, context):
""" """
if isinstance(context, dict): if isinstance(context, dict):
context_d = six.iteritems(context) context_d = context.items()
else: else:
context_d = six.iteritems(context.to_dict()) context_d = context.to_dict().items()
msg.update(('_context_%s' % key, value) msg.update(('_context_%s' % key, value)
for (key, value) in context_d) for (key, value) in context_d)

View File

@ -89,7 +89,7 @@ class RPCException(Exception):
# log the issue and the kwargs # log the issue and the kwargs
LOG.exception(_LE('Exception in string format operation, ' LOG.exception(_LE('Exception in string format operation, '
'kwargs are:')) 'kwargs are:'))
for name, value in six.iteritems(kwargs): for name, value in kwargs.items():
LOG.error("%s: %s", name, value) LOG.error("%s: %s", name, value)
# at least get the core message out if something happened # at least get the core message out if something happened
message = self.msg_fmt message = self.msg_fmt

View File

@ -344,7 +344,7 @@ class PikaOutgoingMessage(object):
msg = self.message.copy() msg = self.message.copy()
if self.context: if self.context:
for key, value in six.iteritems(self.context): for key, value in self.context.items():
key = six.text_type(key) key = six.text_type(key)
msg['_$_' + key] = value msg['_$_' + key] = value

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import six
from oslo_messaging._drivers import base from oslo_messaging._drivers import base
@ -27,7 +25,7 @@ class ZmqIncomingMessage(base.RpcIncomingMessage):
super(ZmqIncomingMessage, self).__init__(context, message) super(ZmqIncomingMessage, self).__init__(context, message)
self._reply_method = kwargs.pop('reply_method', self._reply_method = kwargs.pop('reply_method',
lambda self, reply, failure: None) lambda self, reply, failure: None)
for key, value in six.iteritems(kwargs): for key, value in kwargs.items():
setattr(self, key, value) setattr(self, key, value)
def acknowledge(self): def acknowledge(self):

View File

@ -16,8 +16,6 @@ import logging
import threading import threading
import time import time
import six
from oslo_messaging._drivers.zmq_driver import zmq_async from oslo_messaging._drivers.zmq_driver import zmq_async
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -77,7 +75,7 @@ class TTLCache(object):
old_size = len(self._cache) old_size = len(self._cache)
self._cache = \ self._cache = \
{key: (value, expiration_time) for {key: (value, expiration_time) for
key, (value, expiration_time) in six.iteritems(self._cache) key, (value, expiration_time) in self._cache.items()
if not self._is_expired(expiration_time, current_time)} if not self._is_expired(expiration_time, current_time)}
new_size = len(self._cache) new_size = len(self._cache)
LOG.debug('Updated cache: current size %(new_size)s ' LOG.debug('Updated cache: current size %(new_size)s '

View File

@ -17,7 +17,6 @@ import logging
from oslo_config import cfg from oslo_config import cfg
from oslo_utils import fnmatch from oslo_utils import fnmatch
import six
from stevedore import dispatch from stevedore import dispatch
import yaml import yaml
@ -88,9 +87,9 @@ class RoutingDriver(notifier.Driver):
""" """
accepted_drivers = set() accepted_drivers = set()
for driver, rules in six.iteritems(group): for driver, rules in group.items():
checks = [] checks = []
for key, patterns in six.iteritems(rules): for key, patterns in rules.items():
if key == 'accepted_events': if key == 'accepted_events':
c = [fnmatch.fnmatch(event_type, p) c = [fnmatch.fnmatch(event_type, p)
for p in patterns] for p in patterns]

View File

@ -23,7 +23,6 @@ import traceback as tb
from oslo_config import cfg from oslo_config import cfg
from oslo_middleware import base from oslo_middleware import base
import six
import webob.dec import webob.dec
import oslo_messaging import oslo_messaging
@ -73,7 +72,7 @@ class RequestNotifier(base.Middleware):
include them. include them.
""" """
return dict((k, v) for k, v in six.iteritems(environ) return dict((k, v) for k, v in environ.items()
if k.isupper() and k != 'HTTP_X_AUTH_TOKEN') if k.isupper() and k != 'HTTP_X_AUTH_TOKEN')
@log_and_ignore_error @log_and_ignore_error

View File

@ -105,7 +105,7 @@ class _BaseCallContext(object):
msg = dict(method=method) msg = dict(method=method)
msg['args'] = dict() msg['args'] = dict()
for argname, arg in six.iteritems(args): for argname, arg in args.items():
msg['args'][argname] = self.serializer.serialize_entity(ctxt, arg) msg['args'][argname] = self.serializer.serialize_entity(ctxt, arg)
if self.target.namespace is not None: if self.target.namespace is not None:

View File

@ -186,7 +186,7 @@ class RPCDispatcher(dispatcher.DispatcherBase):
def _do_dispatch(self, endpoint, method, ctxt, args): def _do_dispatch(self, endpoint, method, ctxt, args):
ctxt = self.serializer.deserialize_context(ctxt) ctxt = self.serializer.deserialize_context(ctxt)
new_args = dict() new_args = dict()
for argname, arg in six.iteritems(args): for argname, arg in args.items():
new_args[argname] = self.serializer.deserialize_entity(ctxt, arg) new_args[argname] = self.serializer.deserialize_entity(ctxt, arg)
func = getattr(endpoint, method) func = getattr(endpoint, method)
result = func(ctxt, **new_args) result = func(ctxt, **new_args)

View File

@ -24,7 +24,7 @@ import threading
from oslo_config import cfg from oslo_config import cfg
from oslotest import base from oslotest import base
from oslotest import moxstubout from oslotest import moxstubout
import six
TRUE_VALUES = ('true', '1', 'yes') TRUE_VALUES = ('true', '1', 'yes')
@ -59,7 +59,7 @@ class BaseTestCase(base.BaseTestCase):
test by the tearDown() method. test by the tearDown() method.
""" """
group = kw.pop('group', None) group = kw.pop('group', None)
for k, v in six.iteritems(kw): for k, v in kw.items():
self.conf.set_override(k, v, group, enforce_type=True) self.conf.set_override(k, v, group, enforce_type=True)

View File

@ -411,7 +411,7 @@ class TransportURL(object):
query = {} query = {}
if url.query: if url.query:
for key, values in six.iteritems(parse.parse_qs(url.query)): for key, values in parse.parse_qs(url.query).items():
query[key] = ','.join(values) query[key] = ','.join(values)
virtual_host = None virtual_host = None