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
This commit is contained in:
xurong00037997 2017-02-06 17:42:56 +08:00
parent c93d1c6d31
commit 8bafefb8df
12 changed files with 29 additions and 40 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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,

View File

@ -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,

View File

@ -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)

View File

@ -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()

View File

@ -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]

View File

@ -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']:

View File

@ -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)):

View File

@ -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())

View File

@ -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:

View File

@ -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']