Replace six.iteritems() with .items()

1.As mentioned in [1], we should avoid usingg
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: I19adc9253ce874205d12a5b5430818cbf94ed29c
This commit is contained in:
ritesh.arya 2017-07-05 11:32:17 +05:30 committed by Ritesh
parent 4dadabd13d
commit 18ad055616
5 changed files with 11 additions and 19 deletions

View File

@ -16,8 +16,6 @@ from adjutant.actions.v1.base import BaseAction, ProjectMixin
from django.conf import settings
from adjutant.actions import openstack_clients, user_store
import six
class NewDefaultNetworkAction(BaseAction, ProjectMixin):
"""
@ -294,7 +292,7 @@ class SetProjectQuotaAction(BaseAction):
# update quota for each openstack service
regions_dict = self.settings.get('regions', {})
for region_name, region_settings in six.iteritems(regions_dict):
for region_name, region_settings in regions_dict.items():
quota_size = region_settings.get('quota_size')
quota_settings = settings.PROJECT_QUOTA_SIZES.get(quota_size, {})
if not quota_settings:
@ -302,7 +300,7 @@ class SetProjectQuotaAction(BaseAction):
"Project quota not defined for size '%s' in region %s." % (
quota_size, region_name))
continue
for service_name, values in six.iteritems(quota_settings):
for service_name, values in quota_settings.items():
updater_class = self._quota_updaters.get(service_name)
if not updater_class:
self.add_note("No quota updater found for %s. Ignoring" %

View File

@ -15,8 +15,6 @@
from django.conf import settings
from django.db import models
import six
from adjutant.actions import user_store
from adjutant.actions.v1.base import (
UserNameAction, UserIdAction, UserMixin, ProjectMixin)
@ -177,7 +175,7 @@ class ResetUserPasswordAction(UserNameAction, UserMixin):
roles = id_manager.get_all_roles(self.user)
user_roles = []
for roles in six.itervalues(roles):
for roles in roles.values():
user_roles.extend(role.name for role in roles)
if set(self.blacklist) & set(user_roles):

View File

@ -15,7 +15,6 @@
import mock
import copy
import six
from django.conf import settings
from django.test.utils import override_settings
@ -110,7 +109,7 @@ class FakeManager(object):
roles = temp_cache['projects'][project.name].roles
users = []
for user_id, user_roles in six.iteritems(roles):
for user_id, user_roles in roles.items():
user = self.get_user(user_id)
user.roles = []
@ -248,7 +247,7 @@ class FakeManager(object):
def update_project(self, project, **kwargs):
project = self._project_from_id(project)
for key, arg in six.iteritems(kwargs):
for key, arg in kwargs.items():
if arg is not None:
setattr(project, key, arg)
return project
@ -319,7 +318,7 @@ class modify_dict_settings(override_settings):
self.operations = args[0]
else:
assert not args
self.operations = list(six.iteritems(kwargs))
self.operations = list(kwargs.items())
super(override_settings, self).__init__()
def save_options(self, test_func):

View File

@ -14,7 +14,6 @@
import hashlib
import json
import six
from datetime import timedelta
from smtplib import SMTPException
@ -162,8 +161,7 @@ def create_notification(task, notes, error=False, engines=True):
class_conf = settings.TASK_SETTINGS.get(
task.task_type, settings.DEFAULT_TASK_SETTINGS)
for note_engine, conf in six.iteritems(
class_conf.get('notifications', {})):
for note_engine, conf in (class_conf.get('notifications', {})).items():
if error:
conf = conf.get('error', {})
else:
@ -215,8 +213,8 @@ def parse_filters(func, *args, **kwargs):
cleaned_filters = {}
try:
filters = json.loads(filters)
for field, operations in six.iteritems(filters):
for operation, value in six.iteritems(operations):
for field, operations in filters.items():
for operation, value in operations.items():
cleaned_filters['%s__%s' % (field, operation)] = value
except (ValueError, AttributeError):
return Response(

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
from copy import deepcopy
@ -27,7 +26,7 @@ def dict_merge(a, b):
if not isinstance(b, dict):
return b
result = deepcopy(a)
for k, v in six.iteritems(b):
for k, v in b.items():
if k in result and isinstance(result[k], dict):
result[k] = dict_merge(result[k], v)
else:
@ -41,7 +40,7 @@ def setup_task_settings(task_defaults, action_defaults, task_settings):
settings for each task_type.
"""
new_task_settings = {}
for task, settings in six.iteritems(task_settings):
for task, settings in task_settings.items():
task_setting = deepcopy(task_defaults)
task_setting['action_settings'] = deepcopy(action_defaults)
new_task_settings[task] = dict_merge(task_setting, settings)