Replace six iteration methods with standard ones

1.As mentioned in [1], we should avoid using six.iterXXX
  to achieve iterators. We can use dict.XXX instead, as it
  will return iterators in PY3 as well.
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: Idc13a78f86a63015eb5141c3fe05cde19eb3f7c3
Co-Authored-By: Hanxi Liu <hanxi.liu@easystack.cn>
This commit is contained in:
Lu lei 2016-09-25 13:48:26 +08:00 committed by Hanxi Liu
parent 07fe83bef1
commit 505cb170c3
10 changed files with 15 additions and 27 deletions

View File

@ -12,8 +12,6 @@
import random import random
import six
from rally.common.i18n import _, _LE from rally.common.i18n import _, _LE
from rally import consts from rally import consts
from rally import exceptions from rally import exceptions
@ -193,7 +191,7 @@ class OpenStackAPIVersions(context.Context):
self.context["users"])["credential"]) self.context["users"])["credential"])
services = clients.keystone.service_catalog.get_endpoints() services = clients.keystone.service_catalog.get_endpoints()
services_from_admin = None services_from_admin = None
for client_name, conf in six.iteritems(self.config): for client_name, conf in self.config.items():
if "service_type" in conf and conf["service_type"] not in services: if "service_type" in conf and conf["service_type"] not in services:
raise exceptions.ValidationError(_( raise exceptions.ValidationError(_(
"There is no service with '%s' type in your environment.") "There is no service with '%s' type in your environment.")

View File

@ -18,7 +18,6 @@ import random
import uuid import uuid
from oslo_config import cfg from oslo_config import cfg
import six
from rally.common import broker from rally.common import broker
from rally.common.i18n import _ from rally.common.i18n import _
@ -80,7 +79,7 @@ class UserContextMixin(object):
""" """
scenario_ctx = {} scenario_ctx = {}
for key, value in six.iteritems(context_obj): for key, value in context_obj.items():
if key not in ["users", "tenants"]: if key not in ["users", "tenants"]:
scenario_ctx[key] = value scenario_ctx[key] = value

View File

@ -13,8 +13,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 rally.common.i18n import _ from rally.common.i18n import _
from rally.common import logging from rally.common import logging
from rally.common import utils from rally.common import utils
@ -68,7 +66,7 @@ def _prepare_open_secgroup(credential, secgroup_name):
def rule_match(criteria, existing_rule): def rule_match(criteria, existing_rule):
return all(existing_rule[key] == value return all(existing_rule[key] == value
for key, value in six.iteritems(criteria)) for key, value in criteria.items())
for new_rule in rules_to_add: for new_rule in rules_to_add:
if not any(rule_match(new_rule, existing_rule) for existing_rule if not any(rule_match(new_rule, existing_rule) for existing_rule

View File

@ -13,8 +13,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 rally.common.i18n import _ from rally.common.i18n import _
from rally.common import logging from rally.common import logging
from rally.common import utils from rally.common import utils
@ -103,7 +101,7 @@ class Network(context.Context):
net_wrapper = network_wrapper.wrap( net_wrapper = network_wrapper.wrap(
osclients.Clients(self.context["admin"]["credential"]), osclients.Clients(self.context["admin"]["credential"]),
self, config=self.config) self, config=self.config)
for tenant_id, tenant_ctx in six.iteritems(self.context["tenants"]): for tenant_id, tenant_ctx in self.context["tenants"].items():
for network in tenant_ctx.get("networks", []): for network in tenant_ctx.get("networks", []):
with logging.ExceptionLogger( with logging.ExceptionLogger(
LOG, LOG,

View File

@ -10,8 +10,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 rally.common.i18n import _ from rally.common.i18n import _
from rally.common import logging from rally.common import logging
from rally.common import utils from rally.common import utils
@ -81,7 +79,7 @@ class Lbaas(context.Context):
net_wrapper = network_wrapper.wrap( net_wrapper = network_wrapper.wrap(
osclients.Clients(self.context["admin"]["credential"]), osclients.Clients(self.context["admin"]["credential"]),
self, config=self.config) self, config=self.config)
for tenant_id, tenant_ctx in six.iteritems(self.context["tenants"]): for tenant_id, tenant_ctx in self.context["tenants"].items():
for network in tenant_ctx.get("networks", []): for network in tenant_ctx.get("networks", []):
for pool in network.get("lb_pools", []): for pool in network.get("lb_pools", []):
with logging.ExceptionLogger( with logging.ExceptionLogger(

View File

@ -60,7 +60,7 @@ class CeilometerScenario(scenario.OpenStackScenario):
"source": source, "source": source,
"timestamp": timestamp, "timestamp": timestamp,
} }
for k, v in six.iteritems(opt_fields): for k, v in opt_fields.items():
if v: if v:
sample.update({k: v}) sample.update({k: v})
len_meta = len(metadata_list) if metadata_list else 0 len_meta = len(metadata_list) if metadata_list else 0

View File

@ -16,7 +16,6 @@
import random import random
from oslo_config import cfg from oslo_config import cfg
import six
from rally import exceptions from rally import exceptions
from rally.plugins.openstack import scenario from rally.plugins.openstack import scenario
@ -753,7 +752,7 @@ class NovaScenario(scenario.OpenStackScenario):
break break
try: try:
new_host = random.choice( new_host = random.choice(
[key for key, value in six.iteritems(az.hosts) [key for key, value in az.hosts.items()
if key != host and if key != host and
value.get("nova-compute", {}).get("available", False)]) value.get("nova-compute", {}).get("available", False)])
return new_host return new_host

View File

@ -798,7 +798,7 @@ class FakeAlarmManager(FakeManager):
def update(self, alarm_id, **fake_alarm_dict_diff): def update(self, alarm_id, **fake_alarm_dict_diff):
alarm = self.get(alarm_id)[0] alarm = self.get(alarm_id)[0]
for attr, value in six.iteritems(fake_alarm_dict_diff): for attr, value in fake_alarm_dict_diff.items():
setattr(alarm, attr, value) setattr(alarm, attr, value)
return alarm return alarm

View File

@ -14,7 +14,6 @@
# under the License. # under the License.
import mock import mock
import six
from rally.plugins.openstack.cleanup import base from rally.plugins.openstack.cleanup import base
from rally.plugins.openstack.cleanup import manager from rally.plugins.openstack.cleanup import manager
@ -167,7 +166,7 @@ class SeekAndDestroyTestCase(test.TestCase):
mock_mgr().list.side_effect = list_side_effect mock_mgr().list.side_effect = list_side_effect
mock_mgr.reset_mock() mock_mgr.reset_mock()
for k, v in six.iteritems(kw): for k, v in kw.items():
setattr(mock_mgr, k, v) setattr(mock_mgr, k, v)
return mock_mgr return mock_mgr
@ -342,7 +341,7 @@ class ResourceManagerTestCase(test.TestCase):
def _get_res_mock(self, **kw): def _get_res_mock(self, **kw):
_mock = mock.MagicMock() _mock = mock.MagicMock()
for k, v in six.iteritems(kw): for k, v in kw.items():
setattr(_mock, k, v) setattr(_mock, k, v)
return _mock return _mock

View File

@ -14,7 +14,6 @@
# under the License. # under the License.
import mock import mock
import six
from rally.plugins.openstack.scenarios.quotas import utils from rally.plugins.openstack.scenarios.quotas import utils
from tests.unit import test from tests.unit import test
@ -75,7 +74,7 @@ class QuotasScenarioTestCase(test.ScenarioTestCase):
max_quota = 1024 max_quota = 1024
scenario = utils.QuotasScenario(self.context) scenario = utils.QuotasScenario(self.context)
quotas = scenario._generate_quota_values(max_quota, "nova") quotas = scenario._generate_quota_values(max_quota, "nova")
for k, v in six.iteritems(quotas): for k, v in quotas.items():
self.assertGreaterEqual(v, -1) self.assertGreaterEqual(v, -1)
self.assertLessEqual(v, max_quota) self.assertLessEqual(v, max_quota)
@ -83,7 +82,7 @@ class QuotasScenarioTestCase(test.ScenarioTestCase):
max_quota = 1024 max_quota = 1024
scenario = utils.QuotasScenario(self.context) scenario = utils.QuotasScenario(self.context)
quotas = scenario._generate_quota_values(max_quota, "cinder") quotas = scenario._generate_quota_values(max_quota, "cinder")
for k, v in six.iteritems(quotas): for k, v in quotas.items():
self.assertGreaterEqual(v, -1) self.assertGreaterEqual(v, -1)
self.assertLessEqual(v, max_quota) self.assertLessEqual(v, max_quota)
@ -91,9 +90,9 @@ class QuotasScenarioTestCase(test.ScenarioTestCase):
max_quota = 1024 max_quota = 1024
scenario = utils.QuotasScenario(self.context) scenario = utils.QuotasScenario(self.context)
quotas = scenario._generate_quota_values(max_quota, "neutron") quotas = scenario._generate_quota_values(max_quota, "neutron")
for v in six.itervalues(quotas): for v in quotas.values():
for v1 in six.itervalues(v): for v1 in v.values():
for v2 in six.itervalues(v1): for v2 in v1.values():
self.assertGreaterEqual(v2, -1) self.assertGreaterEqual(v2, -1)
self.assertLessEqual(v2, max_quota) self.assertLessEqual(v2, max_quota)