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: Ida473d702a89e067c350c5cfde6c1982fb466577
This commit is contained in:
loooosy 2017-04-15 15:09:15 +08:00
parent 629fd57ac5
commit 373d7f0721
9 changed files with 35 additions and 40 deletions

View File

@ -59,7 +59,7 @@ class GluonException(Exception):
# kwargs doesn't match a variable in the message
# log the issue and the kwargs
LOG.exception('Exception in string format operation')
for name, value in six.iteritems(kwargs):
for name, value in kwargs.items():
LOG.error("%(name)s: %(value)s" %
{'name': name, 'value': value})
try:

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import six
import sqlalchemy.orm.exc
from gluon.common import exception
@ -106,7 +105,7 @@ class Connection(api.Connection):
def _add_filters(self, query, filters):
if filters is None:
filters = {}
for (key, value) in six.iteritems(filters):
for (key, value) in filters.items():
query = query.filter_by(**{key: value})
return query

View File

@ -78,14 +78,14 @@ class APIGenerator(object):
self.child = {}
if not self.data:
raise Exception('Cannot create API from empty model.')
for table_name, table_data in six.iteritems(self.data['api_objects']):
for table_name, table_data in self.data['api_objects'].items():
try:
# For every entry build a (sub_)api_controller
# an APIObject, an APIObject and an APIListObject
# and a RealObject is created
api_object_fields = {}
for attribute, attr_value in \
six.iteritems(table_data['attributes']):
table_data['attributes'].items():
api_type = self.translate_model_to_api_type(
attr_value.get('type'),
attr_value.get('values'),
@ -117,7 +117,7 @@ class APIGenerator(object):
# Now add all childs since the roots are there now
# And init the controller since all childs are there now
for table_name, table_data in six.iteritems(self.data['api_objects']):
for table_name, table_data in self.data['api_objects'].items():
controller = self.controllers[table_name]
if 'parent' in table_data['api']:
parent = table_data['api'].get('parent')
@ -134,7 +134,7 @@ class APIGenerator(object):
self.subcontrollers[table_name] = new_subcontroller_class
self.child[parent] = table_name
setattr(parent_controller, sub_name, new_subcontroller_class())
for table_name, table_data in six.iteritems(self.data['api_objects']):
for table_name, table_data in self.data['api_objects'].items():
api_name = table_data['api']['plural_name']
controller_instance = self.controllers[table_name]()
if table_name in self.child:

View File

@ -17,7 +17,6 @@
from __future__ import print_function
import re
import six
import sys
import sqlalchemy as sa
@ -56,14 +55,13 @@ class DataBaseModelProcessor(object):
return ret_str.lower().replace("-", "_")
# Make a model class that we've never thought of before
for table_name, table_data in six.iteritems(self.data['api_objects']):
for table_name, table_data in self.data['api_objects'].items():
self.get_primary_key(table_data)
for table_name, table_data in six.iteritems(self.data['api_objects']):
for table_name, table_data in self.data['api_objects'].items():
try:
attrs = {}
for col_name, col_desc in six.iteritems(
table_data['attributes']):
for col_name, col_desc in table_data['attributes'].items():
try:
options = {}
@ -164,7 +162,7 @@ class DataBaseModelProcessor(object):
@classmethod
def get_primary_key(cls, table_data):
primary = []
for k, v in six.iteritems(table_data['attributes']):
for k, v in table_data['attributes'].items():
if 'primary' in v:
primary = k
break

View File

@ -14,7 +14,6 @@
# under the License.
import os
import six
import sys
import click
@ -188,7 +187,7 @@ def make_create_func(api_model, tablename):
del kwargs["host"]
del kwargs["port"]
data = {}
for key, val in six.iteritems(kwargs):
for key, val in kwargs.items():
if val is not None:
data[key] = val
result = do_post(url, data)
@ -205,7 +204,7 @@ def make_update_func(api_model, tablename, primary_key):
del kwargs["port"]
del kwargs[primary_key]
data = {}
for key, val in six.iteritems(kwargs):
for key, val in kwargs.items():
if val is not None:
data[key] = val
result = do_put(url, data)
@ -225,7 +224,7 @@ def make_delete_func(api_model, tablename, primary_key):
def get_primary_key(table_data):
primary = []
for k, v in six.iteritems(table_data['attributes']):
for k, v in table_data['attributes'].items():
if 'primary' in v:
primary = k
break
@ -262,12 +261,12 @@ def proc_model(cli, package_name="unknown",
portdefault=0):
# print("loading model")
model = load_model(package_name, model_dir, api_model)
for table_name, table_data in six.iteritems(model['api_objects']):
for table_name, table_data in model['api_objects'].items():
get_primary_key(table_data)
for table_name, table_data in six.iteritems(model['api_objects']):
for table_name, table_data in model['api_objects'].items():
try:
attrs = {}
for col_name, col_desc in six.iteritems(table_data['attributes']):
for col_name, col_desc in table_data['attributes'].items():
try:
# Step 1: deal with object xrefs
if col_desc['type'] in model['api_objects']:
@ -325,7 +324,7 @@ def proc_model(cli, package_name="unknown",
default=hostdefault, help=hosthelp)(create)
create = click.option("--port", envvar=portenv,
default=portdefault, help=porthelp)(create)
for col_name, col_desc in six.iteritems(table_data['attributes']):
for col_name, col_desc in table_data['attributes'].items():
kwargs = {}
option_name = "--" + col_name
kwargs["default"] = None
@ -344,7 +343,7 @@ def proc_model(cli, package_name="unknown",
default=hostdefault, help=hosthelp)(update)
update = click.option("--port", envvar=portenv,
default=portdefault, help=porthelp)(update)
for col_name, col_desc in six.iteritems(table_data['attributes']):
for col_name, col_desc in table_data['attributes'].items():
if col_name == attrs['_primary_key']:
continue
kwargs = {}

View File

@ -51,13 +51,13 @@ def validate_attributes(obj_name, obj, model):
formats = ['date-time', 'json', 'ipv4', 'ipv6', 'mac', 'uri', 'email']
int_formats = ['int32', 'int64']
for attr_name, attr_val in six.iteritems(obj.get('attributes')):
for attr_name, attr_val in obj.get('attributes').items():
if 'type' not in attr_val:
raise_obj_error(obj_name,
'A type property is not specified for '
'attribute: %s, ',
(attr_name))
for prop_name, prop_val in six.iteritems(attr_val):
for prop_name, prop_val in attr_val.items():
if prop_name in props:
if prop_name == 'type':
if prop_val not in types and \
@ -165,7 +165,7 @@ def verify_model(model):
baseport_found = False
baseinterface_found = False
baseservice_found = False
for obj_name, obj_val in six.iteritems(model['api_objects']):
for obj_name, obj_val in model['api_objects'].items():
if obj_val.get('extends') == 'BasePort':
if baseport_found:
raise_format_error(
@ -205,23 +205,23 @@ def extend_object(obj, obj_dict):
obj['policies'] = dict()
if 'attributes' in ext_obj:
for attr_name, attr_val in \
six.iteritems(ext_obj.get('attributes')):
ext_obj.get('attributes').items():
if attr_name not in obj['attributes']:
obj['attributes'].__setitem__(attr_name, attr_val)
else:
obj['attributes'][attr_name].update(attr_val)
for attr_name, attr_val in six.iteritems(orig_attrs):
for attr_name, attr_val in orig_attrs.items():
if attr_name not in obj['attributes']:
obj['attributes'].__setitem__(attr_name, attr_val)
else:
obj['attributes'][attr_name].update(attr_val)
if 'policies' in ext_obj:
for rule_name, rule_val in six.iteritems(ext_obj.get('policies')):
for rule_name, rule_val in ext_obj.get('policies').items():
if rule_name not in obj['policies']:
obj['policies'].__setitem__(rule_name, rule_val)
else:
obj['policies'][rule_name].update(rule_val)
for rule_name, rule_val in six.iteritems(orig_policies):
for rule_name, rule_val in orig_policies.items():
if rule_name not in obj['policies']:
obj['policies'].__setitem__(rule_name, rule_val)
else:
@ -231,7 +231,7 @@ def extend_object(obj, obj_dict):
def proc_object_extensions(dicta, dictb):
moved_list = list()
for obj_name, obj_val in six.iteritems(dicta):
for obj_name, obj_val in dicta.items():
if obj_val.get('extends') in dictb:
dictb[obj_name] = extend_object(obj_val, dictb)
moved_list.append(obj_name)
@ -243,14 +243,14 @@ def proc_object_extensions(dicta, dictb):
def extend_base_objects(model):
# First we move non-extended objects to new list
for obj_name, obj_val in six.iteritems(model.get('base_objects')):
for obj_name, obj_val in model.get('base_objects').items():
if 'extends' in obj_val:
if obj_val.get('extends') not in model.get('base_objects'):
raise_format_error('extends references unkown object: %s',
(obj_val.get('extends')))
new_dict = dict()
moved_list = list()
for obj_name, obj_val in six.iteritems(model.get('base_objects')):
for obj_name, obj_val in model.get('base_objects').items():
if 'extends' not in obj_val:
moved_list.append(obj_name)
new_dict.__setitem__(obj_name, obj_val)
@ -263,7 +263,7 @@ def extend_base_objects(model):
def extend_api_objects(model):
new_dict = dict()
for obj_name, obj_val in six.iteritems(model.get('api_objects')):
for obj_name, obj_val in model.get('api_objects').items():
if 'extends' in obj_val:
if obj_val.get('extends') not in model.get('base_objects'):
raise_obj_error(obj_name,
@ -297,7 +297,7 @@ def append_model(model, yaml_dict):
model['api_objects'] = dict()
if 'base_objects' not in model:
model['base_objects'] = dict()
for obj_name, obj_val in six.iteritems(yaml_dict.get('objects')):
for obj_name, obj_val in yaml_dict.get('objects').items():
if 'api' in obj_val:
if 'plural_name' not in obj_val['api']:
obj_val['api']['plural_name'] = \

View File

@ -106,7 +106,7 @@ def _should_validate_sub_attributes(attribute, sub_attr):
validate = attribute.get('validate')
return (validate and isinstance(sub_attr, collections.Iterable) and
any([k.startswith('type:dict') and
v for (k, v) in six.iteritems(validate)]))
v for (k, v) in validate.items()]))
def _build_subattr_match_rule(attr_name, attr, action, target):

View File

@ -119,7 +119,7 @@ def start_sync_thread(**kwargs):
"""Start the SyncThread. This should be called in the main function."""
if not SyncData.sync_thread_running:
for key, value in six.iteritems(kwargs):
for key, value in kwargs.items():
if key == "etcd_host":
SyncData.etcd_host = value
elif key == "etcd_port":

View File

@ -13,7 +13,6 @@
# under the License.
import mock
import six
from gluon.particleGenerator.DataBaseModelGenerator import\
DataBaseModelProcessor as DBMProcessor
@ -187,8 +186,8 @@ class DataBaseModelGeneratorTestCase(partgen_base.ParticleGeneratorTestCase):
# The class should have all the attributes from GluonInternalPort table
GluonInternalPort_class_attrs = dir(GluonInternalPort_class)
for col_name, col_desc in \
six.iteritems(self.model['api_objects']
['GluonInternalPort']['attributes']):
(self.model['api_objects']
['GluonInternalPort']['attributes']).items():
self.assertIn(col_name, GluonInternalPort_class_attrs)
# ** for the foo model **
@ -209,7 +208,7 @@ class DataBaseModelGeneratorTestCase(partgen_base.ParticleGeneratorTestCase):
# The class should have all the attributes from GluonInternalPort table
foo_class_attrs = dir(foo_class)
for col_name, col_desc in \
six.iteritems(self.model['api_objects']['foo']['attributes']):
(self.model['api_objects']['foo']['attributes']).items():
self.assertIn(col_name, foo_class_attrs)
"""