Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary conversions to list() and calls to dict.keys() #1243

Merged
merged 4 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions acl_loader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def read_policers_info(self):
# For multi-npu platforms we will read from any one of front asic namespace
# config db as the information should be same across all config db
if self.per_npu_configdb:
namespace_configdb = (list(self.per_npu_configdb.values()))[0]
namespace_configdb = list(self.per_npu_configdb.values())[0]
self.policers_db_info = namespace_configdb.get_table(self.POLICER)
else:
self.policers_db_info = self.configdb.get_table(self.POLICER)
Expand All @@ -199,11 +199,11 @@ def read_sessions_info(self):
# For multi-npu platforms we will read from any one of front asic namespace
# config db as the information should be same across all config db
if self.per_npu_configdb:
namespace_configdb = (list(self.per_npu_configdb.values()))[0]
namespace_configdb = list(self.per_npu_configdb.values())[0]
self.sessions_db_info = namespace_configdb.get_table(self.CFG_MIRROR_SESSION_TABLE)
else:
self.sessions_db_info = self.configdb.get_table(self.CFG_MIRROR_SESSION_TABLE)
for key in list(self.sessions_db_info.keys()):
for key in self.sessions_db_info:
if self.per_npu_statedb:
# For multi-npu platforms we will read from all front asic name space
# statedb as the monitor port will be differnt for each asic
Expand Down Expand Up @@ -367,7 +367,7 @@ def validate_actions(self, table_name, action_props):
# For multi-npu we will read using anyone statedb connector for front asic namespace.
# Same information should be there in all state DB's
# as it is static information about switch capability
namespace_statedb = (list(self.per_npu_statedb.values()))[0]
namespace_statedb = list(self.per_npu_statedb.values())[0]
capability = namespace_statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE))
else:
capability = self.statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE))
Expand Down Expand Up @@ -579,17 +579,17 @@ def full_update(self):
be removed and new rules in that table will be installed.
:return:
"""
for key in list(self.rules_db_info.keys()):
for key in self.rules_db_info:
if self.current_table is None or self.current_table == key[0]:
self.configdb.mod_entry(self.ACL_RULE, key, None)
# Program for per front asic namespace also if present
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_entry(self.ACL_RULE, key, None)


self.configdb.mod_config({self.ACL_RULE: self.rules_info})
# Program for per front asic namespace also if present
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_config({self.ACL_RULE: self.rules_info})

def incremental_update(self):
Expand Down Expand Up @@ -630,15 +630,15 @@ def incremental_update(self):
for key in current_dataplane_rules:
self.configdb.mod_entry(self.ACL_RULE, key, None)
# Program for per-asic namespace also if present
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_entry(self.ACL_RULE, key, None)


# Add all new dataplane rules
for key in new_dataplane_rules:
self.configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key])
# Program for per-asic namespace corresponding to front asic also if present.
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key])

added_controlplane_rules = new_controlplane_rules.difference(current_controlplane_rules)
Expand All @@ -649,22 +649,22 @@ def incremental_update(self):
self.configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key])
# Program for per-asic namespace corresponding to front asic also if present.
# For control plane ACL it's not needed but to keep all db in sync program everywhere
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_entry(self.ACL_RULE, key, self.rules_info[key])

for key in removed_controlplane_rules:
self.configdb.mod_entry(self.ACL_RULE, key, None)
# Program for per-asic namespace corresponding to front asic also if present.
# For control plane ACL it's not needed but to keep all db in sync program everywhere
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.mod_entry(self.ACL_RULE, key, None)

for key in existing_controlplane_rules:
if cmp(self.rules_info[key], self.rules_db_info[key]) != 0:
self.configdb.set_entry(self.ACL_RULE, key, self.rules_info[key])
# Program for per-asic namespace corresponding to front asic also if present.
# For control plane ACL it's not needed but to keep all db in sync program everywhere
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.set_entry(self.ACL_RULE, key, self.rules_info[key])

def delete(self, table=None, rule=None):
Expand All @@ -673,12 +673,12 @@ def delete(self, table=None, rule=None):
:param rule:
:return:
"""
for key in self.rules_db_info.keys():
for key in self.rules_db_info:
if not table or table == key[0]:
if not rule or rule == key[1]:
self.configdb.set_entry(self.ACL_RULE, key, None)
# Program for per-asic namespace corresponding to front asic also if present.
for namespace_configdb in list(self.per_npu_configdb.values()):
for namespace_configdb in self.per_npu_configdb.values():
namespace_configdb.set_entry(self.ACL_RULE, key, None)

def show_table(self, table_name):
Expand Down
4 changes: 2 additions & 2 deletions config/config_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ def _mergeItems(it1, it2):
pass
return

for it in list(D1.keys()):
for it in D1:
# D2 has the key
if D2.get(it):
_mergeItems(D1[it], D2[it])
Expand Down Expand Up @@ -577,7 +577,7 @@ def _searchKeysInConfig(self, In, Out, skeys):
'''
found = False
if isinstance(In, dict):
for key in list(In.keys()):
for key in In:
for skey in skeys:
# pattern is very specific to current primary keys in
# config DB, may need to be updated later.
Expand Down
38 changes: 19 additions & 19 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _get_breakout_options(ctx, args, incomplete):

def shutdown_interfaces(ctx, del_intf_dict):
""" shut down all the interfaces before deletion """
for intf in list(del_intf_dict.keys()):
for intf in del_intf_dict:
config_db = ctx.obj['config_db']
if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(config_db, intf)
Expand Down Expand Up @@ -295,7 +295,7 @@ def interface_alias_to_name(config_db, interface_alias):
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if interface_alias == port_dict[port_name]['alias']:
return port_name if sub_intf_sep_idx == -1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id

Expand Down Expand Up @@ -326,15 +326,15 @@ def interface_name_is_valid(config_db, interface_name):
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if interface_name == port_name:
return True
if port_channel_dict:
for port_channel_name in list(port_channel_dict.keys()):
for port_channel_name in port_channel_dict:
if interface_name == port_channel_name:
return True
if sub_port_intf_dict:
for sub_port_intf_name in list(sub_port_intf_dict.keys()):
for sub_port_intf_name in sub_port_intf_dict:
if interface_name == sub_port_intf_name:
return True
return False
Expand All @@ -357,7 +357,7 @@ def interface_name_to_alias(config_db, interface_name):
if not port_dict:
click.echo("port_dict is None!")
raise click.Abort()
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if interface_name == port_name:
return port_dict[port_name]['alias']

Expand Down Expand Up @@ -410,7 +410,7 @@ def get_port_namespace(port):
if clicommon.get_interface_naming_mode() == "alias":
port_dict = config_db.get_table(table_name)
if port_dict:
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if port == port_dict[port_name]['alias']:
return namespace
else:
Expand All @@ -427,7 +427,7 @@ def del_interface_bind_to_vrf(config_db, vrf_name):
for table_name in tables:
interface_dict = config_db.get_table(table_name)
if interface_dict:
for interface_name in list(interface_dict.keys()):
for interface_name in interface_dict:
if 'vrf_name' in interface_dict[interface_name] and vrf_name == interface_dict[interface_name]['vrf_name']:
interface_dependent = interface_ipaddr_dependent_on_interface(config_db, interface_name)
for interface_del in interface_dependent:
Expand Down Expand Up @@ -459,7 +459,7 @@ def set_interface_naming_mode(mode):
click.echo("port_dict is None!")
raise click.Abort()

for port_name in list(port_dict.keys()):
for port_name in port_dict:
try:
if port_dict[port_name]['alias']:
pass
Expand Down Expand Up @@ -639,7 +639,7 @@ def _get_disabled_services_list(config_db):

feature_table = config_db.get_table('FEATURE')
if feature_table is not None:
for feature_name in list(feature_table.keys()):
for feature_name in feature_table:
if not feature_name:
log.log_warning("Feature is None")
continue
Expand Down Expand Up @@ -751,15 +751,15 @@ def _restart_services(config_db):

def interface_is_in_vlan(vlan_member_table, interface_name):
""" Check if an interface is in a vlan """
for _, intf in list(vlan_member_table.keys()):
for _, intf in vlan_member_table:
if intf == interface_name:
return True

return False

def interface_is_in_portchannel(portchannel_member_table, interface_name):
""" Check if an interface is part of portchannel """
for _, intf in list(portchannel_member_table.keys()):
for _, intf in portchannel_member_table:
if intf == interface_name:
return True

Expand Down Expand Up @@ -2124,17 +2124,17 @@ def startup(ctx, interface_name):

log.log_info("'interface startup {}' executing...".format(interface_name))
port_dict = config_db.get_table('PORT')
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if port_name in intf_fs:
config_db.mod_entry("PORT", port_name, {"admin_status": "up"})

portchannel_list = config_db.get_table("PORTCHANNEL")
for po_name in list(portchannel_list.keys()):
for po_name in portchannel_list:
if po_name in intf_fs:
config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "up"})

subport_list = config_db.get_table("VLAN_SUB_INTERFACE")
for sp_name in list(subport_list.keys()):
for sp_name in subport_list:
if sp_name in intf_fs:
config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "up"})

Expand Down Expand Up @@ -2164,17 +2164,17 @@ def shutdown(ctx, interface_name):
ctx.fail("Interface name is invalid. Please enter a valid interface name!!")

port_dict = config_db.get_table('PORT')
for port_name in list(port_dict.keys()):
for port_name in port_dict:
if port_name in intf_fs:
config_db.mod_entry("PORT", port_name, {"admin_status": "down"})

portchannel_list = config_db.get_table("PORTCHANNEL")
for po_name in list(portchannel_list.keys()):
for po_name in portchannel_list:
if po_name in intf_fs:
config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "down"})

subport_list = config_db.get_table("VLAN_SUB_INTERFACE")
for sp_name in list(subport_list.keys()):
for sp_name in subport_list:
if sp_name in intf_fs:
config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "down"})

Expand Down Expand Up @@ -2299,7 +2299,7 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load
cm = load_ConfigMgmt(verbose)

""" Delete all ports if forced else print dependencies using ConfigMgmt API """
final_delPorts = [intf for intf in list(del_intf_dict.keys())]
final_delPorts = [intf for intf in del_intf_dict]
""" Warn user if tables without yang models exist and have final_delPorts """
breakout_warnUser_extraTables(cm, final_delPorts, confirm=True)

Expand Down
10 changes: 5 additions & 5 deletions config/nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def isOverlappingWithAnyDynamicEntry(ipAddress):
if not nat_pool_dict:
return False

for values in list(nat_pool_dict.values()):
for values in nat_pool_dict.values():
global_ip = values["nat_ip"]
ipAddr = global_ip.split('-')
if (len(ipAddr) == 1):
Expand Down Expand Up @@ -604,7 +604,7 @@ def remove_static_all(ctx):
for table_name in tables:
table_dict = config_db.get_table(table_name)
if table_dict:
for table_key_name in list(table_dict.keys()):
for table_key_name in table_dict:
config_db.set_entry(table_name, table_key_name, None)

#
Expand Down Expand Up @@ -828,7 +828,7 @@ def remove_pools(ctx):
binding_dict = config_db.get_table(binding_table_name)
pool_dict = config_db.get_table(pool_table_name)
if pool_dict:
for pool_key_name in list(pool_dict.keys()):
for pool_key_name in pool_dict:
entryFound = False
for binding_name, binding_values in binding_dict.items():
if binding_values['nat_pool'] == pool_key_name:
Expand Down Expand Up @@ -880,7 +880,7 @@ def remove_bindings(ctx):
binding_table_name = 'NAT_BINDINGS'
binding_dict = config_db.get_table(binding_table_name)
if binding_dict:
for binding_key_name in list(binding_dict.keys()):
for binding_key_name in binding_dict:
config_db.set_entry(binding_table_name, binding_key_name, None)

#
Expand Down Expand Up @@ -961,7 +961,7 @@ def remove_interfaces(ctx):
for table_name in tables:
table_dict = config_db.get_table(table_name)
if table_dict:
for table_key_name in list(table_dict.keys()):
for table_key_name in table_dict:
if isinstance(table_key_name, str) is False:
continue

Expand Down
2 changes: 1 addition & 1 deletion fdbutil/filter_fdb_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_vlan_cidr_map(filename):

vlan_cidr = defaultdict()
if "VLAN_INTERFACE" in config_db_entries and "VLAN" in config_db_entries:
for vlan_key in list(config_db_entries["VLAN_INTERFACE"].keys()):
for vlan_key in config_db_entries["VLAN_INTERFACE"]:
if '|' not in vlan_key:
continue
vlan, cidr = tuple(vlan_key.split('|'))
Expand Down
2 changes: 1 addition & 1 deletion fwutil/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def __validate_component_map(self, section, pdp_map, pcp_map):
)
)

for key in list(pdp_map.keys()):
for key in pdp_map:
diff_keys = self.__diff_keys(list(pdp_map[key].keys()), list(pcp_map[key].keys()))

if diff_keys:
Expand Down
4 changes: 2 additions & 2 deletions pfcwd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def get_all_ports(db, namespace=None, display=constants.DISPLAY_ALL):
def get_server_facing_ports(db):
candidates = db.get_table('DEVICE_NEIGHBOR')
server_facing_ports = []
for port in list(candidates.keys()):
for port in candidates:
neighbor = db.get_entry(
'DEVICE_NEIGHBOR_METADATA', candidates[port]['name']
)
if neighbor and neighbor['type'].lower() == 'server':
server_facing_ports.append(port)
if not server_facing_ports:
server_facing_ports = [p[1] for p in list(db.get_table('VLAN_MEMBER').keys())]
server_facing_ports = [p[1] for p in db.get_table('VLAN_MEMBER')]
return server_facing_ports


Expand Down
4 changes: 2 additions & 2 deletions scripts/aclshow
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class AclStat(object):
"""
Get ACL counters from the DB
"""
for table, rule in self.acl_rules.keys():
for table, rule in self.acl_rules:
cnt_props = lowercase_keys(self.db.get_all(self.db.COUNTERS_DB, "COUNTERS:%s:%s" % (table, rule)))
self.acl_counters[table, rule] = cnt_props

Expand Down Expand Up @@ -163,7 +163,7 @@ class AclStat(object):

header = ACL_HEADER
aclstat = []
for rule_key in self.acl_rules.keys():
for rule_key in self.acl_rules:
if not display_all and (self.get_counter_value(rule_key, 'packets') == '0' or \
self.get_counter_value(rule_key, 'packets') == 'N/A'):
continue
Expand Down
Loading