From 505cb170c348ef37ddc876724e6527614b1c8053 Mon Sep 17 00:00:00 2001 From: Lu lei Date: Sun, 25 Sep 2016 13:48:26 +0800 Subject: [PATCH] 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 --- rally/plugins/openstack/context/api_versions.py | 4 +--- rally/plugins/openstack/context/keystone/users.py | 3 +-- rally/plugins/openstack/context/network/allow_ssh.py | 4 +--- rally/plugins/openstack/context/network/networks.py | 4 +--- rally/plugins/openstack/context/neutron/lbaas.py | 4 +--- rally/plugins/openstack/scenarios/ceilometer/utils.py | 2 +- rally/plugins/openstack/scenarios/nova/utils.py | 3 +-- tests/unit/fakes.py | 2 +- tests/unit/plugins/openstack/cleanup/test_manager.py | 5 ++--- .../plugins/openstack/scenarios/quotas/test_utils.py | 11 +++++------ 10 files changed, 15 insertions(+), 27 deletions(-) diff --git a/rally/plugins/openstack/context/api_versions.py b/rally/plugins/openstack/context/api_versions.py index 623a6655..df545301 100644 --- a/rally/plugins/openstack/context/api_versions.py +++ b/rally/plugins/openstack/context/api_versions.py @@ -12,8 +12,6 @@ import random -import six - from rally.common.i18n import _, _LE from rally import consts from rally import exceptions @@ -193,7 +191,7 @@ class OpenStackAPIVersions(context.Context): self.context["users"])["credential"]) services = clients.keystone.service_catalog.get_endpoints() 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: raise exceptions.ValidationError(_( "There is no service with '%s' type in your environment.") diff --git a/rally/plugins/openstack/context/keystone/users.py b/rally/plugins/openstack/context/keystone/users.py index 71ed8c8c..366c33ad 100644 --- a/rally/plugins/openstack/context/keystone/users.py +++ b/rally/plugins/openstack/context/keystone/users.py @@ -18,7 +18,6 @@ import random import uuid from oslo_config import cfg -import six from rally.common import broker from rally.common.i18n import _ @@ -80,7 +79,7 @@ class UserContextMixin(object): """ scenario_ctx = {} - for key, value in six.iteritems(context_obj): + for key, value in context_obj.items(): if key not in ["users", "tenants"]: scenario_ctx[key] = value diff --git a/rally/plugins/openstack/context/network/allow_ssh.py b/rally/plugins/openstack/context/network/allow_ssh.py index 97aacd88..ba9551d7 100644 --- a/rally/plugins/openstack/context/network/allow_ssh.py +++ b/rally/plugins/openstack/context/network/allow_ssh.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from rally.common.i18n import _ from rally.common import logging from rally.common import utils @@ -68,7 +66,7 @@ def _prepare_open_secgroup(credential, secgroup_name): def rule_match(criteria, existing_rule): 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: if not any(rule_match(new_rule, existing_rule) for existing_rule diff --git a/rally/plugins/openstack/context/network/networks.py b/rally/plugins/openstack/context/network/networks.py index efc770f0..2e85a60f 100644 --- a/rally/plugins/openstack/context/network/networks.py +++ b/rally/plugins/openstack/context/network/networks.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from rally.common.i18n import _ from rally.common import logging from rally.common import utils @@ -103,7 +101,7 @@ class Network(context.Context): net_wrapper = network_wrapper.wrap( osclients.Clients(self.context["admin"]["credential"]), 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", []): with logging.ExceptionLogger( LOG, diff --git a/rally/plugins/openstack/context/neutron/lbaas.py b/rally/plugins/openstack/context/neutron/lbaas.py index bc1d6b34..b2cb1bc0 100644 --- a/rally/plugins/openstack/context/neutron/lbaas.py +++ b/rally/plugins/openstack/context/neutron/lbaas.py @@ -10,8 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from rally.common.i18n import _ from rally.common import logging from rally.common import utils @@ -81,7 +79,7 @@ class Lbaas(context.Context): net_wrapper = network_wrapper.wrap( osclients.Clients(self.context["admin"]["credential"]), 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 pool in network.get("lb_pools", []): with logging.ExceptionLogger( diff --git a/rally/plugins/openstack/scenarios/ceilometer/utils.py b/rally/plugins/openstack/scenarios/ceilometer/utils.py index 6f4f5876..7abab89c 100644 --- a/rally/plugins/openstack/scenarios/ceilometer/utils.py +++ b/rally/plugins/openstack/scenarios/ceilometer/utils.py @@ -60,7 +60,7 @@ class CeilometerScenario(scenario.OpenStackScenario): "source": source, "timestamp": timestamp, } - for k, v in six.iteritems(opt_fields): + for k, v in opt_fields.items(): if v: sample.update({k: v}) len_meta = len(metadata_list) if metadata_list else 0 diff --git a/rally/plugins/openstack/scenarios/nova/utils.py b/rally/plugins/openstack/scenarios/nova/utils.py index 8e18ca30..b3b6de4b 100755 --- a/rally/plugins/openstack/scenarios/nova/utils.py +++ b/rally/plugins/openstack/scenarios/nova/utils.py @@ -16,7 +16,6 @@ import random from oslo_config import cfg -import six from rally import exceptions from rally.plugins.openstack import scenario @@ -753,7 +752,7 @@ class NovaScenario(scenario.OpenStackScenario): break try: 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 value.get("nova-compute", {}).get("available", False)]) return new_host diff --git a/tests/unit/fakes.py b/tests/unit/fakes.py index 7e1bf651..7495e9a5 100644 --- a/tests/unit/fakes.py +++ b/tests/unit/fakes.py @@ -798,7 +798,7 @@ class FakeAlarmManager(FakeManager): def update(self, alarm_id, **fake_alarm_dict_diff): 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) return alarm diff --git a/tests/unit/plugins/openstack/cleanup/test_manager.py b/tests/unit/plugins/openstack/cleanup/test_manager.py index 1573dd83..b168644e 100644 --- a/tests/unit/plugins/openstack/cleanup/test_manager.py +++ b/tests/unit/plugins/openstack/cleanup/test_manager.py @@ -14,7 +14,6 @@ # under the License. import mock -import six from rally.plugins.openstack.cleanup import base 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.reset_mock() - for k, v in six.iteritems(kw): + for k, v in kw.items(): setattr(mock_mgr, k, v) return mock_mgr @@ -342,7 +341,7 @@ class ResourceManagerTestCase(test.TestCase): def _get_res_mock(self, **kw): _mock = mock.MagicMock() - for k, v in six.iteritems(kw): + for k, v in kw.items(): setattr(_mock, k, v) return _mock diff --git a/tests/unit/plugins/openstack/scenarios/quotas/test_utils.py b/tests/unit/plugins/openstack/scenarios/quotas/test_utils.py index 47a88bef..fb05892a 100644 --- a/tests/unit/plugins/openstack/scenarios/quotas/test_utils.py +++ b/tests/unit/plugins/openstack/scenarios/quotas/test_utils.py @@ -14,7 +14,6 @@ # under the License. import mock -import six from rally.plugins.openstack.scenarios.quotas import utils from tests.unit import test @@ -75,7 +74,7 @@ class QuotasScenarioTestCase(test.ScenarioTestCase): max_quota = 1024 scenario = utils.QuotasScenario(self.context) 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.assertLessEqual(v, max_quota) @@ -83,7 +82,7 @@ class QuotasScenarioTestCase(test.ScenarioTestCase): max_quota = 1024 scenario = utils.QuotasScenario(self.context) 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.assertLessEqual(v, max_quota) @@ -91,9 +90,9 @@ class QuotasScenarioTestCase(test.ScenarioTestCase): max_quota = 1024 scenario = utils.QuotasScenario(self.context) quotas = scenario._generate_quota_values(max_quota, "neutron") - for v in six.itervalues(quotas): - for v1 in six.itervalues(v): - for v2 in six.itervalues(v1): + for v in quotas.values(): + for v1 in v.values(): + for v2 in v1.values(): self.assertGreaterEqual(v2, -1) self.assertLessEqual(v2, max_quota)