Using dict.items() is better than six.iteritems(dict)

Replacing dict.iteritems()/.itervalues() with
six.iteritems(dict)/six.itervalues(dict) was preferred in the past,
but there was a discussion suggesting to avoid six for this[1].
The overhead of creating a temporary list on Python 2 is negligible.

[1]http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html
Partially-implements blueprint py3-compatibility

Change-Id: Ia2298733188b3d964d43a547504ede2ebeaba9bd
This commit is contained in:
ting.wang 2016-01-18 13:03:53 +08:00 committed by binean
parent 33b3183672
commit 2bc625399f
36 changed files with 56 additions and 73 deletions

View File

@ -18,7 +18,6 @@ import re
from oslo_config import cfg
from oslo_log import log
import six
from six.moves.urllib import parse
import webob
@ -193,7 +192,7 @@ def dict_to_query_str(params):
# TODO(throughnothing): we should just use urllib.urlencode instead of this
# But currently we don't work with urlencoded url's
param_str = ""
for key, val in six.iteritems(params):
for key, val in params.items():
param_str = param_str + '='.join([str(key), str(val)]) + '&'
return param_str.rstrip('&')

View File

@ -96,7 +96,7 @@ class ExtensionsResource(wsgi.Resource):
def index(self, req):
extensions = []
for _alias, ext in six.iteritems(self.extension_manager.extensions):
for _alias, ext in self.extension_manager.extensions.items():
extensions.append(self._translate(ext))
return dict(extensions=extensions)

View File

@ -16,7 +16,6 @@
"""The security service api."""
from oslo_log import log
import six
import webob
from webob import exc
@ -124,7 +123,7 @@ class SecurityServiceController(wsgi.Controller):
not_found = object()
for ss in security_services:
if all(ss.get(opt, not_found) == value for opt, value in
six.iteritems(search_opts)):
search_opts.items()):
results.append(ss)
security_services = results

View File

@ -164,7 +164,7 @@ class ShareNetworkController(wsgi.Controller):
for opt in opts_to_remove:
search_opts.pop(opt, None)
if search_opts:
for key, value in six.iteritems(search_opts):
for key, value in search_opts.items():
if key in ['ip_version', 'segmentation_id']:
value = int(value)
networks = [network for network in networks
@ -284,7 +284,7 @@ class ShareNetworkController(wsgi.Controller):
'add_security_service': self._add_security_service,
'remove_security_service': self._remove_security_service
}
for action, data in six.iteritems(body):
for action, data in body.items():
try:
return _actions[action](req, id, data)
except KeyError:

View File

@ -55,7 +55,7 @@ class ShareServerController(wsgi.Controller):
else:
s.share_network_name = s.share_network_id
if search_opts:
for k, v in six.iteritems(search_opts):
for k, v in search_opts.items():
share_servers = [s for s in share_servers if
(hasattr(s, k) and
s[k] == v or k == 'share_network' and

View File

@ -16,7 +16,6 @@
"""The share snapshots api."""
from oslo_log import log
import six
import webob
from webob import exc
@ -208,7 +207,7 @@ class ShareSnapshotsController(wsgi.Controller, wsgi.AdminActionsMixin):
snapshot.get('display_name'),
snapshot.get('display_description'))
return self._view_builder.detail(
req, dict(six.iteritems(new_snapshot)))
req, dict(new_snapshot.items()))
def create_resource():

View File

@ -33,7 +33,7 @@ class ShareTypeExtraSpecsController(wsgi.Controller):
def _get_extra_specs(self, context, type_id):
extra_specs = db.share_type_extra_specs_get(context, type_id)
specs_dict = {}
for key, value in six.iteritems(extra_specs):
for key, value in extra_specs.items():
specs_dict[key] = value
return dict(extra_specs=specs_dict)
@ -62,7 +62,7 @@ class ShareTypeExtraSpecsController(wsgi.Controller):
and valid_type
and valid_required_extra_spec)
for k, v in six.iteritems(extra_specs):
for k, v in extra_specs.items():
if is_valid_string(k) and isinstance(v, dict):
self._verify_extra_specs(v)
elif not is_valid_extra_spec(k, v):

View File

@ -324,7 +324,7 @@ class ShareMixin(object):
display_description,
**kwargs)
return self._view_builder.detail(req, dict(six.iteritems(new_share)))
return self._view_builder.detail(req, dict(new_share.items()))
@staticmethod
def _validate_common_name(access):

View File

@ -179,8 +179,7 @@ class CGSnapshotController(wsgi.Controller, wsgi.AdminActionsMixin):
except exception.InvalidConsistencyGroup as e:
raise exc.HTTPConflict(explanation=six.text_type(e))
return self._view_builder.detail(req, dict(six.iteritems(
new_snapshot)))
return self._view_builder.detail(req, dict(new_snapshot.items()))
@wsgi.Controller.api_version('2.4', experimental=True)
@wsgi.Controller.authorize('get_cgsnapshot')

View File

@ -213,7 +213,7 @@ class CGController(wsgi.Controller, wsgi.AdminActionsMixin):
except (exception.CGSnapshotNotFound, exception.InvalidInput) as e:
raise exc.HTTPBadRequest(explanation=six.text_type(e))
return self._view_builder.detail(req, dict(six.iteritems(new_cg)))
return self._view_builder.detail(req, dict(new_cg.items()))
def _update(self, *args, **kwargs):
db.consistency_group_update(*args, **kwargs)

View File

@ -64,7 +64,6 @@ i18n.enable_lazy()
from oslo_config import cfg
from oslo_log import log
from oslo_utils import uuidutils
import six
from manila.common import config # Need to register global_opts # noqa
from manila import context
@ -256,7 +255,7 @@ class ConfigCommands(object):
"""Class for exposing the flags defined by flag_file(s)."""
def list(self):
for key, value in six.iteritems(CONF):
for key, value in CONF.items():
if value is not None:
print('%s = %s' % (key, value))

View File

@ -1292,7 +1292,7 @@ def _share_get_query(context, session=None):
def _metadata_refs(metadata_dict, meta_class):
metadata_refs = []
if metadata_dict:
for k, v in six.iteritems(metadata_dict):
for k, v in metadata_dict.items():
value = six.text_type(v) if isinstance(v, bool) else v
metadata_ref = meta_class()
@ -1978,7 +1978,7 @@ def _share_metadata_update(context, share_id, metadata, delete, session=None):
if delete:
original_metadata = _share_metadata_get(context, share_id,
session=session)
for meta_key, meta_value in six.iteritems(original_metadata):
for meta_key, meta_value in original_metadata.items():
if meta_key not in metadata:
meta_ref = _share_metadata_get_item(context, share_id,
meta_key,
@ -2900,7 +2900,7 @@ def share_type_extra_specs_update_or_create(context, share_type_id, specs):
session = get_session()
with session.begin():
spec_ref = None
for key, value in six.iteritems(specs):
for key, value in specs.items():
try:
spec_ref = _share_type_extra_specs_get_item(
context, share_type_id, key, session)

View File

@ -22,7 +22,6 @@ SQLAlchemy models for Manila data.
from oslo_config import cfg
from oslo_db.sqlalchemy import models
from oslo_log import log
import six
from sqlalchemy import Column, Integer, String, schema
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import orm
@ -45,7 +44,7 @@ class ManilaBase(models.ModelBase,
def to_dict(self):
model_dict = {}
for k, v in six.iteritems(self):
for k, v in self.items():
if not issubclass(type(v), ManilaBase):
model_dict[k] = v
return model_dict

View File

@ -81,7 +81,7 @@ class ManilaException(Exception):
self.kwargs['code'] = self.code
except AttributeError:
pass
for k, v in six.iteritems(self.kwargs):
for k, v in self.kwargs.items():
if isinstance(v, Exception):
self.kwargs[k] = six.text_type(v)
@ -93,7 +93,7 @@ class ManilaException(Exception):
# kwargs doesn't match a variable in the message
# log the issue and the kwargs
LOG.exception(_LE('Exception in string format operation.'))
for name, value in six.iteritems(kwargs):
for name, value in kwargs.items():
LOG.error(_LE("%(name)s: %(value)s"), {
'name': name, 'value': value})
if CONF.fatal_exception_format_errors:

View File

@ -237,7 +237,7 @@ class DbQuotaDriver(object):
project_id, user_id)
# Use the project quota for default user quota.
proj_quotas = db.quota_get_all_by_project(context, project_id)
for key, value in six.iteritems(proj_quotas):
for key, value in proj_quotas.items():
if key not in user_quotas.keys():
user_quotas[key] = value
user_usages = None

View File

@ -15,8 +15,6 @@
import logging
import six
from manila.scheduler.filters import base_host
from manila.scheduler.filters import extra_specs_ops
@ -36,7 +34,7 @@ class CapabilitiesFilter(base_host.BaseHostFilter):
if not extra_specs:
return True
for key, req in six.iteritems(extra_specs):
for key, req in extra_specs.items():
# Either not scoped format, or in capabilities scope
scope = key.split(':')

View File

@ -505,12 +505,12 @@ class HostManager(object):
host_state = self.host_state_cls(
host,
capabilities=capabilities,
service=dict(six.iteritems(service)))
service=dict(service.items()))
self.host_state_map[host] = host_state
# Update capabilities and attributes in host_state
host_state.update_from_share_capability(
capabilities, service=dict(six.iteritems(service)))
capabilities, service=dict(service.items()))
def get_all_host_states_share(self, context):
"""Returns a dict of all the hosts the HostManager knows about.
@ -579,7 +579,7 @@ class HostManager(object):
if not filter_dict:
return True
for filter_key, filter_value in six.iteritems(filter_dict):
for filter_key, filter_value in filter_dict.items():
if filter_key not in dict_to_check:
return False
if not re.match(filter_value, dict_to_check.get(filter_key)):

View File

@ -715,7 +715,7 @@ class API(base.Base):
def get_snapshot(self, context, snapshot_id):
policy.check_policy(context, 'share_snapshot', 'get_snapshot')
rv = self.db.share_snapshot_get(context, snapshot_id)
return dict(six.iteritems(rv))
return dict(rv.items())
def get_all_snapshots(self, context, search_opts=None,
sort_key='share_id', sort_dir='desc'):
@ -751,7 +751,7 @@ class API(base.Base):
results = []
not_found = object()
for snapshot in snapshots:
for opt, value in six.iteritems(search_opts):
for opt, value in search_opts.items():
if snapshot.get(opt, not_found) != value:
break
else:
@ -872,7 +872,7 @@ class API(base.Base):
def get_share_metadata(self, context, share):
"""Get all metadata associated with a share."""
rv = self.db.share_metadata_get(context, share['id'])
return dict(six.iteritems(rv))
return dict(rv.items())
@policy.wrap_check_policy('share')
def delete_share_metadata(self, context, share, key):
@ -893,7 +893,7 @@ class API(base.Base):
if not metadata:
metadata = {}
for k, v in six.iteritems(metadata):
for k, v in metadata.items():
if not k:
msg = _("Metadata property key is blank.")
LOG.warning(msg)

View File

@ -128,7 +128,7 @@ def _conf2json(conf):
def _dump_to_conf(confdict, out=sys.stdout, indent=0):
"""Output confdict in Ganesha config format."""
if isinstance(confdict, dict):
for k, v in six.iteritems(confdict):
for k, v in confdict.items():
if v is None:
continue
out.write(' ' * (indent * IWIDTH) + k + ' ')

View File

@ -17,7 +17,6 @@ import os
import pipes
from oslo_concurrency import processutils
import six
from manila import utils
@ -25,7 +24,7 @@ from manila import utils
def patch(base, *overlays):
"""Recursive dictionary patching."""
for ovl in overlays:
for k, v in six.iteritems(ovl):
for k, v in ovl.items():
if isinstance(v, dict) and isinstance(base.get(k), dict):
patch(base[k], v)
else:
@ -35,7 +34,7 @@ def patch(base, *overlays):
def walk(dct):
"""Recursive iteration over dictionary."""
for k, v in six.iteritems(dct):
for k, v in dct.items():
if isinstance(v, dict):
for w in walk(v):
yield w

View File

@ -91,7 +91,7 @@ class GlusterManager(object):
self.components = (address if isinstance(address, dict) else
self.parse(address))
for k, v in six.iteritems(requires):
for k, v in requires.items():
if v is None:
continue
if (self.components.get(k) is not None) != v:

View File

@ -109,7 +109,7 @@ class GlusterfsVolumeMappedLayout(layout.GlusterfsShareLayoutBase):
"""
subdict = {}
for key, val in six.iteritems(PATTERN_DICT):
for key, val in PATTERN_DICT.items():
subdict[key] = val['pattern']
# Using templates with placeholder syntax #{<var>}
@ -131,7 +131,7 @@ class GlusterfsVolumeMappedLayout(layout.GlusterfsShareLayoutBase):
except exception.GlusterfsException as exc:
exceptions[srvaddr] = six.text_type(exc)
if exceptions:
for srvaddr, excmsg in six.iteritems(exceptions):
for srvaddr, excmsg in exceptions.items():
LOG.error(_LE("'gluster version' failed on server "
"%(server)s with: %(message)s"),
{'server': srvaddr, 'message': excmsg})
@ -139,7 +139,7 @@ class GlusterfsVolumeMappedLayout(layout.GlusterfsShareLayoutBase):
"'gluster version' failed on servers %s") % (
','.join(exceptions.keys())))
notsupp_servers = []
for srvaddr, vers in six.iteritems(glusterfs_versions):
for srvaddr, vers in glusterfs_versions.items():
if common.GlusterManager.numreduct(
vers) < self.driver.GLUSTERFS_VERSION_MIN:
notsupp_servers.append(srvaddr)

View File

@ -1020,9 +1020,9 @@ class NetAppCmodeFileStorageLibrary(object):
return
raid_types = self._client.get_aggregate_raid_types(aggregate_names)
for aggregate_name, raid_type in six.iteritems(raid_types):
for aggregate_name, raid_type in raid_types.items():
ssc_stats[aggregate_name]['netapp_raid_type'] = raid_type
disk_types = self._client.get_aggregate_disk_types(aggregate_names)
for aggregate_name, disk_type in six.iteritems(disk_types):
for aggregate_name, disk_type in disk_types.items():
ssc_stats[aggregate_name]['netapp_disk_type'] = disk_type

View File

@ -873,7 +873,7 @@ class ShareManager(manager.SchedulerDependentManager):
def _update_quota_usages(self, context, project_id, usages):
user_id = context.user_id
for resource, usage in six.iteritems(usages):
for resource, usage in usages.items():
try:
current_usage = self.db.quota_usage_get(
context, project_id, resource, user_id)

View File

@ -82,7 +82,7 @@ def get_all_types(context, inactive=0, search_opts=None):
share_types = db.share_type_get_all(context, inactive, filters=filters)
for type_name, type_args in six.iteritems(share_types):
for type_name, type_args in share_types.items():
required_extra_specs = {}
try:
required_extra_specs = get_valid_required_extra_specs(
@ -101,7 +101,7 @@ def get_all_types(context, inactive=0, search_opts=None):
LOG.debug("Searching by: %s", search_opts)
def _check_extra_specs_match(share_type, searchdict):
for k, v in six.iteritems(searchdict):
for k, v in searchdict.items():
if (k not in share_type['extra_specs'].keys()
or share_type['extra_specs'][k] != v):
return False
@ -111,9 +111,9 @@ def get_all_types(context, inactive=0, search_opts=None):
filter_mapping = {'extra_specs': _check_extra_specs_match}
result = {}
for type_name, type_args in six.iteritems(share_types):
for type_name, type_args in share_types.items():
# go over all filters in the list
for opt, values in six.iteritems(search_opts):
for opt, values in search_opts.items():
try:
filter_func = filter_mapping[opt]
except KeyError:
@ -292,11 +292,11 @@ def share_types_diff(context, share_type_id1, share_type_id2):
dict1 = {}
if dict2 is None:
dict2 = {}
for k, v in six.iteritems(dict1):
for k, v in dict1.items():
res[k] = (v, dict2.get(k))
if k not in dict2 or res[k][0] != res[k][1]:
equal = False
for k, v in six.iteritems(dict2):
for k, v in dict2.items():
res[k] = (dict1.get(k), v)
if k not in dict1 or res[k][0] != res[k][1]:
equal = False

View File

@ -33,7 +33,6 @@ from oslo_config import fixture as config_fixture
import oslo_i18n
from oslo_messaging import conffixture as messaging_conffixture
import oslotest.base as base_test
import six
from manila.db import migration
from manila.db.sqlalchemy import api as db_api
@ -171,7 +170,7 @@ class TestCase(base_test.BaseTestCase):
def flags(self, **kw):
"""Override flag variables for a test."""
for k, v in six.iteritems(kw):
for k, v in kw.items():
CONF.set_override(k, v)
def start_service(self, name, host=None, **kwargs):

View File

@ -17,7 +17,6 @@ import uuid
from oslo_utils import timeutils
import routes
import six
import webob
import webob.dec
import webob.request
@ -96,7 +95,7 @@ class FakeToken(object):
def __init__(self, **kwargs):
FakeToken.id_count += 1
self.id = FakeToken.id_count
for k, v in six.iteritems(kwargs):
for k, v in kwargs.items():
setattr(self, k, v)

View File

@ -18,7 +18,6 @@ import datetime
import ddt
import mock
from oslo_utils import timeutils
import six
import webob
from manila.api.v2 import share_types as types
@ -406,7 +405,7 @@ def fake_share_type_get_all(context, inactive=False, filters=None):
if filters is None or filters.get('is_public', None) is None:
return SHARE_TYPES
res = {}
for k, v in six.iteritems(SHARE_TYPES):
for k, v in SHARE_TYPES.items():
if filters['is_public'] and _has_type_access(k, context.project_id):
res.update({k: v})
continue

View File

@ -859,8 +859,8 @@ class ShareNetworkDatabaseAPITestCase(BaseDatabaseAPITestCase):
self.share_nw_dict['id'])
self.assertEqual(new_name, result_update['name'])
self._check_fields(expected=dict(six.iteritems(result_update)),
actual=dict(six.iteritems(result_get)))
self._check_fields(expected=dict(result_update.items()),
actual=dict(result_get.items()))
def test_update_not_found(self):
self.assertRaises(exception.ShareNetworkNotFound,

View File

@ -17,7 +17,6 @@ Fakes For Scheduler tests.
"""
from oslo_utils import timeutils
import six
from manila.scheduler.drivers import filter
from manila.scheduler import host_manager
@ -237,7 +236,7 @@ class FakeHostManager(host_manager.HostManager):
class FakeHostState(host_manager.HostState):
def __init__(self, host, attribute_dict):
super(FakeHostState, self).__init__(host)
for (key, val) in six.iteritems(attribute_dict):
for (key, val) in attribute_dict.items():
setattr(self, key, val)

View File

@ -23,7 +23,6 @@ import tempfile
import ddt
import mock
from oslo_config import cfg
import six
from manila.common import constants
from manila import context
@ -185,7 +184,7 @@ class GlusterfsVolumeMappedLayoutTestCase(test.TestCase):
self.mock_object(self._layout, '_glustermanager',
mock.Mock(side_effect=_glustermanager_calls))
expected_output = {}
for q, d in six.iteritems(self.glusterfs_volumes_dict):
for q, d in self.glusterfs_volumes_dict.items():
if sharemark[q] not in (FAKE_UUID1, FAKE_UUID2):
expected_output[q] = d

View File

@ -17,7 +17,6 @@ Mock unit tests for the NetApp file share driver interfaces
import mock
import six
from manila.share.drivers.netapp.dataontap.cluster_mode import drv_multi_svm
from manila.share.drivers.netapp.dataontap.cluster_mode import drv_single_svm
@ -59,5 +58,5 @@ class NetAppFileStorageDriverInterfaceTestCase(test.TestCase):
def _get_local_functions(self, obj):
"""Get function names of an object without superclass functions."""
return set([key for key, value in six.iteritems(type(obj).__dict__)
return set([key for key, value in type(obj).__dict__.items()
if callable(value)])

View File

@ -116,7 +116,7 @@ class NetAppDriverFactoryTestCase(test.TestCase):
registry = na_common.NETAPP_UNIFIED_DRIVER_REGISTRY
for family in six.iterkeys(registry):
for mode, full_class_name in six.iteritems(registry[family]):
for mode, full_class_name in registry[family].items():
config = na_fakes.create_configuration()
config.local_conf.set_override('driver_handles_share_servers',

View File

@ -20,7 +20,6 @@ import copy
from oslo_config import cfg
from oslo_serialization import jsonutils
import six
from manila.common import constants
from manila import context
@ -132,7 +131,7 @@ class ShareRpcAPITestCase(test.TestCase):
for arg, expected_arg in zip(self.fake_args, expected_args):
self.assertEqual(expected_arg, arg)
for kwarg, value in six.iteritems(self.fake_kwargs):
for kwarg, value in self.fake_kwargs.items():
self.assertEqual(expected_msg[kwarg], value)
def test_create_share_instance(self):

View File

@ -273,7 +273,7 @@ class ExceptionTest(test.TestCase):
self.assertEqual(exception_type.code, resp.status_int, resp.body)
if hasattr(exception_type, 'headers'):
for (key, value) in six.iteritems(exception_type.headers):
for (key, value) in exception_type.headers.items():
self.assertTrue(key in resp.headers)
self.assertEqual(value, resp.headers[key])

View File

@ -35,7 +35,6 @@ from oslo_service import service
from oslo_utils import excutils
from paste import deploy
import routes.middleware
import six
import webob.dec
import webob.exc
@ -440,7 +439,7 @@ class Debug(Middleware):
resp = req.get_response(self.application)
print(('*' * 40) + ' RESPONSE HEADERS')
for (key, value) in six.iteritems(resp.headers):
for (key, value) in resp.headers.items():
print(key, '=', value)
print()