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: I9b1f1496375f5b5c6dbc5dadbb57d729680ff3fd
This commit is contained in:
parent
89fe91ce7a
commit
b6e5633b7b
@ -122,7 +122,7 @@ class RequestDeserializer(api_versioning.VersionedResource,
|
||||
|
||||
# step 4 - parse filter parameters
|
||||
filters = []
|
||||
for fname, fval in six.iteritems(params):
|
||||
for fname, fval in params.items():
|
||||
if fname == 'version' and fval == 'latest':
|
||||
query_params['latest'] = True
|
||||
else:
|
||||
@ -386,7 +386,7 @@ class ResponseSerializer(api_versioning.VersionedResource,
|
||||
params.pop('marker', None)
|
||||
|
||||
encode_params = {}
|
||||
for key, value in six.iteritems(params):
|
||||
for key, value in params.items():
|
||||
encode_params[key] = encodeutils.safe_encode(value)
|
||||
query = urlparse.urlencode(encode_params)
|
||||
|
||||
|
@ -316,7 +316,7 @@ def no_4byte_params(f):
|
||||
|
||||
def _check_dict(data_dict):
|
||||
# a dict of dicts has to be checked recursively
|
||||
for key, value in six.iteritems(data_dict):
|
||||
for key, value in data_dict.items():
|
||||
if isinstance(value, dict):
|
||||
_check_dict(value)
|
||||
else:
|
||||
|
@ -17,7 +17,6 @@
|
||||
from oslo_db import exception as db_exception
|
||||
from oslo_log import log as logging
|
||||
from retrying import retry
|
||||
import six
|
||||
|
||||
from glare.db.sqlalchemy import api
|
||||
from glare import locking
|
||||
@ -40,7 +39,7 @@ class ArtifactAPI(object):
|
||||
new_values = {}
|
||||
if 'tags' in values:
|
||||
new_values['tags'] = values.pop('tags') if values['tags'] else []
|
||||
for key, value in six.iteritems(values):
|
||||
for key, value in values.items():
|
||||
if key in api.BASE_ARTIFACT_PROPERTIES:
|
||||
new_values[key] = value
|
||||
else:
|
||||
|
@ -525,7 +525,7 @@ def _do_properties(artifact, new_properties):
|
||||
if prop.name not in new_properties:
|
||||
props_to_update.append(prop)
|
||||
|
||||
for prop_name, prop_value in six.iteritems(new_properties):
|
||||
for prop_name, prop_value in new_properties.items():
|
||||
if prop_value is None:
|
||||
continue
|
||||
if isinstance(prop_value, list):
|
||||
@ -543,7 +543,7 @@ def _do_properties(artifact, new_properties):
|
||||
_create_property(prop_name, list_prop, position=pos)
|
||||
)
|
||||
elif isinstance(prop_value, dict):
|
||||
for dict_key, dict_val in six.iteritems(prop_value):
|
||||
for dict_key, dict_val in prop_value.items():
|
||||
for prop in artifact.properties:
|
||||
if prop.name == prop_name and prop.key_name == dict_key:
|
||||
if getattr(prop, _get_prop_type(dict_val)) != dict_val:
|
||||
@ -582,7 +582,7 @@ def _do_blobs(artifact, new_blobs):
|
||||
if blob.name not in new_blobs:
|
||||
blobs_to_update.append(blob)
|
||||
|
||||
for blob_name, blob_value in six.iteritems(new_blobs):
|
||||
for blob_name, blob_value in new_blobs.items():
|
||||
if blob_value is None:
|
||||
continue
|
||||
if isinstance(blob_value.get('status'), str):
|
||||
@ -597,7 +597,7 @@ def _do_blobs(artifact, new_blobs):
|
||||
_update_blob_values(blob, blob_value)
|
||||
blobs_to_update.append(blob)
|
||||
else:
|
||||
for dict_key, dict_val in six.iteritems(blob_value):
|
||||
for dict_key, dict_val in blob_value.items():
|
||||
for blob in artifact.blobs:
|
||||
if blob.name == blob_name and blob.key_name == dict_key:
|
||||
_update_blob_values(blob, dict_val)
|
||||
|
@ -206,7 +206,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
af.obj_set_defaults(*default_fields)
|
||||
|
||||
# apply values specified by user
|
||||
for name, value in six.iteritems(values):
|
||||
for name, value in values.items():
|
||||
setattr(af, name, value)
|
||||
return af
|
||||
|
||||
@ -350,7 +350,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
cls._validate_change_allowed(values, af)
|
||||
# apply values to the artifact. if all changes applied then update
|
||||
# values in db or raise an exception in other case.
|
||||
for key, value in six.iteritems(values):
|
||||
for key, value in values.items():
|
||||
try:
|
||||
# check updates for links and validate them
|
||||
if cls.is_link(key) and value is not None:
|
||||
@ -592,7 +592,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
|
||||
@classmethod
|
||||
def _delete_blobs(cls, blobs, context, af):
|
||||
for name, blob in six.iteritems(blobs):
|
||||
for name, blob in blobs.items():
|
||||
if cls.is_blob(name):
|
||||
if not blob['external']:
|
||||
try:
|
||||
@ -603,7 +603,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
cls.db_api.update_blob(context, af.id, {name: None})
|
||||
elif cls.is_blob_dict(name):
|
||||
upd_blob = deepcopy(blob)
|
||||
for key, val in six.iteritems(blob):
|
||||
for key, val in blob.items():
|
||||
if not val['external']:
|
||||
try:
|
||||
store_api.delete_blob(val['url'], context=context)
|
||||
@ -625,7 +625,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
|
||||
# marking all blobs as pending delete
|
||||
blobs = {}
|
||||
for name, field in six.iteritems(af.fields):
|
||||
for name, field in af.fields.items():
|
||||
if cls.is_blob(name):
|
||||
b = getattr(af, name)
|
||||
if b:
|
||||
@ -634,7 +634,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
elif cls.is_blob_dict(name):
|
||||
bd = getattr(af, name)
|
||||
if bd:
|
||||
for key, b in six.iteritems(bd):
|
||||
for key, b in bd.items():
|
||||
cls._prepare_blob_delete(b, af, name)
|
||||
blobs[name] = bd
|
||||
LOG.debug("Marked artifact %(artifact)s as deleted and all its blobs "
|
||||
@ -665,7 +665,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
"for activation.") % cls.STATUS.ACTIVE
|
||||
raise exception.BadRequest(msg)
|
||||
|
||||
for name, type_obj in six.iteritems(af.fields):
|
||||
for name, type_obj in af.fields.items():
|
||||
if type_obj.required_on_activate and getattr(af, name) is None:
|
||||
msg = _(
|
||||
"'%s' field value must be set before activation") % name
|
||||
@ -881,7 +881,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
def _obj_changes_to_primitive(self):
|
||||
changes = self.obj_get_changes()
|
||||
res = {}
|
||||
for key, val in six.iteritems(changes):
|
||||
for key, val in changes.items():
|
||||
if val is not None and hasattr(val, 'to_primitive'):
|
||||
res[key] = val.to_primitive()
|
||||
else:
|
||||
@ -977,7 +977,7 @@ class BaseArtifact(base.VersionedObject):
|
||||
def gen_schemas(cls):
|
||||
"""Return json schema representation of the artifact type."""
|
||||
schemas_prop = {}
|
||||
for field_name, field in six.iteritems(cls.fields):
|
||||
for field_name, field in cls.fields.items():
|
||||
schemas_prop[field_name] = cls._schema_field(
|
||||
field, field_name=field_name)
|
||||
schemas = {'properties': schemas_prop,
|
||||
|
@ -92,7 +92,7 @@ class BlobFieldType(fields.FieldType):
|
||||
|
||||
@staticmethod
|
||||
def to_primitive(obj, field, value):
|
||||
prim = {key: val for key, val in six.iteritems(value)
|
||||
prim = {key: val for key, val in value.items()
|
||||
if key != 'id'}
|
||||
|
||||
if not value.get('external'):
|
||||
|
@ -22,7 +22,6 @@ from oslo_config import cfg
|
||||
from oslo_config import types as conf_types
|
||||
from oslo_log import log as logging
|
||||
from oslo_versionedobjects import base as vo_base
|
||||
import six
|
||||
|
||||
from glare.common import exception
|
||||
from glare.i18n import _
|
||||
@ -120,7 +119,7 @@ class ArtifactRegistry(vo_base.VersionedObjectRegistry):
|
||||
:param type_name: name of artifact type
|
||||
:return: artifact class
|
||||
"""
|
||||
for name, af_type in six.iteritems(cls.obj_classes()):
|
||||
for name, af_type in cls.obj_classes().items():
|
||||
if af_type[0].get_type_name() == type_name:
|
||||
return af_type[0]
|
||||
raise exception.TypeNotFound(name=type_name)
|
||||
|
@ -13,8 +13,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import six
|
||||
|
||||
from oslo_log import log as logging
|
||||
from oslo_utils import encodeutils
|
||||
from oslo_versionedobjects import fields
|
||||
@ -287,7 +285,7 @@ class RequiredDictKeys(Validator):
|
||||
{"item": item,
|
||||
"value": ''.join(
|
||||
'{}:{}, '.format(key, val)
|
||||
for key, val in six.iteritems(value))})
|
||||
for key, val in value.items())})
|
||||
|
||||
def to_jsonschema(self):
|
||||
return {'required': list(self.required_items)}
|
||||
@ -337,6 +335,6 @@ class DictElementValidator(ElementValidator):
|
||||
return glare_fields.Dict,
|
||||
|
||||
def validate(self, value):
|
||||
for v in six.itervalues(value):
|
||||
for v in value.values():
|
||||
for validator in self.validators:
|
||||
validator(v)
|
||||
|
@ -14,8 +14,6 @@
|
||||
|
||||
"""This file contains classes that wrap nat"""
|
||||
|
||||
import six
|
||||
|
||||
from oslo_versionedobjects import fields
|
||||
|
||||
from glare.common import exception as exc
|
||||
@ -112,7 +110,7 @@ class Field(object):
|
||||
# setup custom field properties
|
||||
field_props = {prop_name: getattr(self, prop_name)
|
||||
for prop_name in self.field_props}
|
||||
for prop, value in six.iteritems(field_props):
|
||||
for prop, value in field_props.items():
|
||||
setattr(field, prop, value)
|
||||
|
||||
# apply custom validators
|
||||
|
Loading…
Reference in New Issue
Block a user