From 8bafefb8dff63b52d917f5608ced039232cae179 Mon Sep 17 00:00:00 2001 From: xurong00037997 Date: Mon, 6 Feb 2017 17:42:56 +0800 Subject: [PATCH] Replace six.iteritems/itervalues with dict.items()/values() 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: I1d260757c26449317e34367141bdbcfec7831647 --- dragonflow/cli/utils.py | 4 ++-- dragonflow/controller/dnat_app.py | 9 ++++----- dragonflow/controller/l2_app.py | 3 +-- dragonflow/controller/sg_app.py | 5 ++--- dragonflow/controller/topology.py | 3 +-- dragonflow/db/db_store.py | 12 +++++------- dragonflow/db/drivers/redis_db_driver.py | 9 ++++----- dragonflow/db/drivers/redis_mgt.py | 8 ++++---- dragonflow/db/drivers/zookeeper_db_driver.py | 2 +- dragonflow/ovsdb/commands.py | 8 +++----- dragonflow/ovsdb/impl_idl.py | 3 +-- dragonflow/tests/fullstack/test_objects.py | 3 +-- 12 files changed, 29 insertions(+), 40 deletions(-) diff --git a/dragonflow/cli/utils.py b/dragonflow/cli/utils.py index 15ee8dfd4..6ff93c9ff 100644 --- a/dragonflow/cli/utils.py +++ b/dragonflow/cli/utils.py @@ -86,7 +86,7 @@ def keys_and_vals_to_strs(dictionary): return str(k_or_v) else: return k_or_v - return dict((to_str(k), to_str(v)) for k, v in six.iteritems(dictionary)) + return dict((to_str(k), to_str(v)) for k, v in dictionary.items()) def _format_field_name(attr): @@ -135,7 +135,7 @@ def print_dict(dct, dict_property="Property", wrap=0): """ pt = prettytable.PrettyTable([dict_property, 'Value']) pt.align = 'l' - for k, v in six.iteritems(dct): + for k, v in dct.items(): # convert dict to str to check length if isinstance(v, dict): v = six.text_type(keys_and_vals_to_strs(v)) diff --git a/dragonflow/controller/dnat_app.py b/dragonflow/controller/dnat_app.py index 79ff39ffb..7d49ec24a 100644 --- a/dragonflow/controller/dnat_app.py +++ b/dragonflow/controller/dnat_app.py @@ -18,7 +18,6 @@ import collections import netaddr from neutron_lib import constants as n_const from ryu.ofproto import ether -import six from dragonflow import conf as cfg from dragonflow.controller.common import arp_responder @@ -58,7 +57,7 @@ class DNATApp(df_base_app.DFlowApp): or mac == '00:00:00:00:00:00'): return - for key, floatingip in six.iteritems(self.local_floatingips): + for key, floatingip in self.local_floatingips.items(): self._install_dnat_egress_rules(floatingip, mac) self.update_floatingip_status( floatingip, n_const.FLOATINGIP_STATUS_ACTIVE) @@ -87,7 +86,7 @@ class DNATApp(df_base_app.DFlowApp): def _is_first_external_network(self, network_id): if self._get_external_network_count(network_id) == 0: # check whether there are other networks - for key, val in six.iteritems(self.external_networks): + for key, val in self.external_networks.items(): if key != network_id and val > 0: return False return True @@ -96,7 +95,7 @@ class DNATApp(df_base_app.DFlowApp): def _is_last_external_network(self, network_id): if self._get_external_network_count(network_id) == 1: # check whether there are other networks - for key, val in six.iteritems(self.external_networks): + for key, val in self.external_networks.items(): if key != network_id and val > 0: return False return True @@ -308,7 +307,7 @@ class DNATApp(df_base_app.DFlowApp): def remove_local_port(self, lport): port_id = lport.get_id() ips_to_disassociate = [ - fip for fip in six.itervalues(self.local_floatingips) + fip for fip in self.local_floatingips.values() if fip.get_lport_id() == port_id] for floatingip in ips_to_disassociate: self.disassociate_floatingip(floatingip) diff --git a/dragonflow/controller/l2_app.py b/dragonflow/controller/l2_app.py index f4e003752..a3a136fb5 100644 --- a/dragonflow/controller/l2_app.py +++ b/dragonflow/controller/l2_app.py @@ -22,7 +22,6 @@ from oslo_log import log from ryu.lib.mac import haddr_to_bin from ryu.lib.packet import in_proto from ryu.ofproto import ether -import six from dragonflow._i18n import _, _LI, _LE from dragonflow import conf as cfg @@ -75,7 +74,7 @@ class L2App(df_base_app.DFlowApp): :param bridge_mappings: map physical network names to bridge names. ''' - for physical_network, bridge in six.iteritems(bridge_mappings): + for physical_network, bridge in bridge_mappings.items(): LOG.info(_LI("Mapping physical network %(physical_network)s to " "bridge %(bridge)s"), {'physical_network': physical_network, diff --git a/dragonflow/controller/sg_app.py b/dragonflow/controller/sg_app.py index b46ef5cff..1a8102937 100644 --- a/dragonflow/controller/sg_app.py +++ b/dragonflow/controller/sg_app.py @@ -17,7 +17,6 @@ import netaddr from neutron_lib import constants as n_const from oslo_log import log from ryu.ofproto import ether -import six from dragonflow._i18n import _LI, _LW, _LE from dragonflow.controller.common import constants as const @@ -792,7 +791,7 @@ class SGApp(df_base_app.DFlowApp): # of remote group. secrules = self.remote_secgroup_ref.get(secgroup_id) if secrules: - for rule_info in six.itervalues(secrules): + for rule_info in secrules.values(): self._update_security_group_rule_flows_by_addresses( rule_info.get_security_group_id(), rule_info, @@ -822,7 +821,7 @@ class SGApp(df_base_app.DFlowApp): # parameter of remote group. secrules = self.remote_secgroup_ref.get(secgroup_id) if secrules: - for rule_info in six.itervalues(secrules): + for rule_info in secrules.values(): self._update_security_group_rule_flows_by_addresses( rule_info.get_security_group_id(), rule_info, diff --git a/dragonflow/controller/topology.py b/dragonflow/controller/topology.py index 48e8d8900..b006266f3 100644 --- a/dragonflow/controller/topology.py +++ b/dragonflow/controller/topology.py @@ -12,7 +12,6 @@ from oslo_config import cfg from oslo_log import log -import six from dragonflow._i18n import _LI, _LE, _LW from dragonflow.common import constants @@ -301,7 +300,7 @@ class Topology(object): new_ovs_to_lport_mapping = {} add_ovs_to_lport_mapping = {} delete_ovs_to_lport_mapping = self.ovs_to_lport_mapping - for key, ovs_port in six.iteritems(self.ovs_ports): + for key, ovs_port in self.ovs_ports.items(): if ovs_port.get_type() == db_models.OvsPort.TYPE_VM: lport_id = ovs_port.get_iface_id() lport = self._get_lport(lport_id) diff --git a/dragonflow/db/db_store.py b/dragonflow/db/db_store.py index 44417e66d..ad73c4c89 100644 --- a/dragonflow/db/db_store.py +++ b/dragonflow/db/db_store.py @@ -17,8 +17,6 @@ import collections import copy import threading -import six - from dragonflow.db import models @@ -93,7 +91,7 @@ class DbStore(object): def get(self, table_name, key, topic): if topic: return self.tenant_dbs[topic].get(table_name, key) - for tenant_db in six.itervalues(self.tenant_dbs): + for tenant_db in self.tenant_dbs.values(): value = tenant_db.get(table_name, key) if value: return value @@ -102,7 +100,7 @@ class DbStore(object): if topic: return self.tenant_dbs[topic].keys(table_name) result = [] - for tenant_db in six.itervalues(self.tenant_dbs): + for tenant_db in self.tenant_dbs.values(): result.extend(tenant_db.keys(table_name)) return result @@ -110,7 +108,7 @@ class DbStore(object): if topic: return self.tenant_dbs[topic].values(table_name) result = [] - for tenant_db in six.itervalues(self.tenant_dbs): + for tenant_db in self.tenant_dbs.values(): result.extend(tenant_db.values(table_name)) return result @@ -123,7 +121,7 @@ class DbStore(object): if topic: self.tenant_dbs[topic].pop(table_name, key) else: - for tenant_db in six.itervalues(self.tenant_dbs): + for tenant_db in self.tenant_dbs.values(): if tenant_db.pop(table_name, key): break @@ -362,7 +360,7 @@ class DbStore(object): def clear(self, topic=None): if not topic: - for tenant_db in six.itervalues(self.tenant_dbs): + for tenant_db in self.tenant_dbs.values(): tenant_db.clear() else: self.tenant_dbs[topic].clear() diff --git a/dragonflow/db/drivers/redis_db_driver.py b/dragonflow/db/drivers/redis_db_driver.py index 940f81caa..7a7702dbc 100644 --- a/dragonflow/db/drivers/redis_db_driver.py +++ b/dragonflow/db/drivers/redis_db_driver.py @@ -15,7 +15,6 @@ import re from oslo_log import log from redis import client as redis_client from redis import exceptions -import six from dragonflow._i18n import _LE, _LW from dragonflow.db import db_api @@ -59,7 +58,7 @@ class RedisDbDriver(db_api.DbApi): def delete_table(self, table): local_key = self._uuid_to_key(table, '*', '*') - for host, client in six.iteritems(self.clients): + for host, client in self.clients.items(): local_keys = client.keys(local_key) if len(local_keys) > 0: for tmp_key in local_keys: @@ -178,7 +177,7 @@ class RedisDbDriver(db_api.DbApi): local_key = self._uuid_to_key(table, key, '*') self._sync_master_list() try: - for host, client in six.iteritems(self.clients): + for host, client in self.clients.items(): local_keys = client.keys(local_key) if len(local_keys) == 1: return self._execute_cmd("GET", local_keys[0]) @@ -232,7 +231,7 @@ class RedisDbDriver(db_api.DbApi): if not topic: local_key = self._uuid_to_key(table, '*', '*') try: - for host, client in six.iteritems(self.clients): + for host, client in self.clients.items(): local_keys = client.keys(local_key) if len(local_keys) > 0: for tmp_key in local_keys: @@ -267,7 +266,7 @@ class RedisDbDriver(db_api.DbApi): if not topic: local_key = self._uuid_to_key(table, '*', '*') try: - for host, client in six.iteritems(self.clients): + for host, client in self.clients.items(): ip_port = host res.extend(client.keys(local_key)) return [self._strip_table_name_from_key(key) for key in res] diff --git a/dragonflow/db/drivers/redis_mgt.py b/dragonflow/db/drivers/redis_mgt.py index 9fd6afc9f..82e90403f 100644 --- a/dragonflow/db/drivers/redis_mgt.py +++ b/dragonflow/db/drivers/redis_mgt.py @@ -126,7 +126,7 @@ class RedisMgt(object): def get_cluster_topology_by_all_nodes(self): # get redis cluster topology from local nodes cached in initialization new_nodes = {} - for host, info in six.iteritems(self.cluster_nodes): + for host, info in self.cluster_nodes.items(): ip_port = host.split(':') try: node = self._init_node(ip_port[0], ip_port[1]) @@ -218,7 +218,7 @@ class RedisMgt(object): def _parse_to_masterlist(self): master_list = [] - for host, info in six.iteritems(self.cluster_nodes): + for host, info in self.cluster_nodes.items(): if 'master' == info['role']: slots = [] if len(info['slots']) > 0: @@ -269,8 +269,8 @@ class RedisMgt(object): slave_cnt = 0 slot_changed = False - for host, info in six.iteritems(old_nodes): - for new_host, new_info in six.iteritems(new_nodes): + for host, info in old_nodes.items(): + for new_host, new_info in new_nodes.items(): if host == new_host and info['role'] == \ new_info['role']: if info['slots'] != new_info['slots']: diff --git a/dragonflow/db/drivers/zookeeper_db_driver.py b/dragonflow/db/drivers/zookeeper_db_driver.py index 423dcd100..68995537e 100644 --- a/dragonflow/db/drivers/zookeeper_db_driver.py +++ b/dragonflow/db/drivers/zookeeper_db_driver.py @@ -32,7 +32,7 @@ def _parse_hosts(hosts): return hosts.strip() if isinstance(hosts, (dict)): host_ports = [] - for (k, v) in six.iteritems(hosts): + for (k, v) in hosts.items(): host_ports.append("%s:%s" % (k, v)) hosts = host_ports if isinstance(hosts, (list, set, tuple)): diff --git a/dragonflow/ovsdb/commands.py b/dragonflow/ovsdb/commands.py index 1787b183f..2d1bd6ce3 100644 --- a/dragonflow/ovsdb/commands.py +++ b/dragonflow/ovsdb/commands.py @@ -10,8 +10,6 @@ # License for the specific language governing permissions and limitations # under the License. -import six - from neutron.agent.ovsdb.native import commands from neutron.agent.ovsdb.native import idlutils from oslo_config import cfg @@ -138,7 +136,7 @@ class DeleteQos(commands.BaseCommand): conditions = [('external_ids', '=', {'iface-id': self.port_id})] rows_to_delete = [] for table in ['QoS', 'Queue']: - for r in six.itervalues(self.api._tables[table].rows): + for r in self.api._tables[table].rows.values(): if idlutils.row_match(r, conditions): rows_to_delete.append(r) @@ -155,7 +153,7 @@ class UpdateQos(commands.BaseCommand): def run_idl(self, txn): conditions = [('external_ids', '=', {'iface-id': self.port_id})] queue_table = self.api._tables['Queue'] - for r in six.itervalues(queue_table.rows): + for r in queue_table.rows.values(): if idlutils.row_match(r, conditions): dscp = self.qos.get_dscp_marking() dscp = dscp if dscp else [] @@ -169,7 +167,7 @@ class UpdateQos(commands.BaseCommand): setattr(r, 'other_config', other_config) qos_table = self.api._tables['QoS'] - for r in six.itervalues(qos_table.rows): + for r in qos_table.rows.values(): if idlutils.row_match(r, conditions): external_ids = getattr(r, 'external_ids', {}) external_ids['version'] = str(self.qos.get_version()) diff --git a/dragonflow/ovsdb/impl_idl.py b/dragonflow/ovsdb/impl_idl.py index fa8ae66ed..0a37470eb 100644 --- a/dragonflow/ovsdb/impl_idl.py +++ b/dragonflow/ovsdb/impl_idl.py @@ -16,7 +16,6 @@ from neutron.agent.ovsdb import impl_idl from neutron.agent.ovsdb.native import connection from oslo_config import cfg from ovs.db import idl -import six from dragonflow.common import constants from dragonflow.ovsdb import commands @@ -108,7 +107,7 @@ class DFConnection(connection.Connection): """ def update_schema_helper(self, helper): tables = ovsdb_monitor_table_filter_default - for table_name, columns in six.iteritems(tables): + for table_name, columns in tables.items(): if columns == 'all': helper.register_table(table_name) else: diff --git a/dragonflow/tests/fullstack/test_objects.py b/dragonflow/tests/fullstack/test_objects.py index cad49381b..56b783ea9 100644 --- a/dragonflow/tests/fullstack/test_objects.py +++ b/dragonflow/tests/fullstack/test_objects.py @@ -11,7 +11,6 @@ # under the License. import netaddr -import six import time from neutronclient.common import exceptions @@ -287,7 +286,7 @@ class VMTestObj(object): if self.server is None: return None ips = self.nova.servers.ips(self.server) - for id, network in six.iteritems(ips): + for id, network in ips.items(): for ip in network: if int(ip['version']) == 4: return ip['addr']