Skip to content

Commit d07ca5f

Browse files
authored
Remove unnecessary conversions to list() and calls to dict.keys() (sonic-net#1243)
After conversion to Python 3 using the 2to3 tool, all calls to `dict.keys()` and `dict.values()` were converted to lists. In the instances where the returned view objects from these calls are iterated over, there is no need to convert to a list, so I removed those. Also, when iterating over the keys of a dictionary, one need not call `dict.keys()`, one can simply iterate over the dictionary as `for key in dict:`, so I cleaned those up as well.
1 parent 1c45ca1 commit d07ca5f

24 files changed

+92
-92
lines changed

acl_loader/main.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def read_policers_info(self):
182182
# For multi-npu platforms we will read from any one of front asic namespace
183183
# config db as the information should be same across all config db
184184
if self.per_npu_configdb:
185-
namespace_configdb = (list(self.per_npu_configdb.values()))[0]
185+
namespace_configdb = list(self.per_npu_configdb.values())[0]
186186
self.policers_db_info = namespace_configdb.get_table(self.POLICER)
187187
else:
188188
self.policers_db_info = self.configdb.get_table(self.POLICER)
@@ -199,11 +199,11 @@ def read_sessions_info(self):
199199
# For multi-npu platforms we will read from any one of front asic namespace
200200
# config db as the information should be same across all config db
201201
if self.per_npu_configdb:
202-
namespace_configdb = (list(self.per_npu_configdb.values()))[0]
202+
namespace_configdb = list(self.per_npu_configdb.values())[0]
203203
self.sessions_db_info = namespace_configdb.get_table(self.CFG_MIRROR_SESSION_TABLE)
204204
else:
205205
self.sessions_db_info = self.configdb.get_table(self.CFG_MIRROR_SESSION_TABLE)
206-
for key in list(self.sessions_db_info.keys()):
206+
for key in self.sessions_db_info:
207207
if self.per_npu_statedb:
208208
# For multi-npu platforms we will read from all front asic name space
209209
# statedb as the monitor port will be differnt for each asic
@@ -367,7 +367,7 @@ def validate_actions(self, table_name, action_props):
367367
# For multi-npu we will read using anyone statedb connector for front asic namespace.
368368
# Same information should be there in all state DB's
369369
# as it is static information about switch capability
370-
namespace_statedb = (list(self.per_npu_statedb.values()))[0]
370+
namespace_statedb = list(self.per_npu_statedb.values())[0]
371371
capability = namespace_statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE))
372372
else:
373373
capability = self.statedb.get_all(self.statedb.STATE_DB, "{}|switch".format(self.SWITCH_CAPABILITY_TABLE))
@@ -579,17 +579,17 @@ def full_update(self):
579579
be removed and new rules in that table will be installed.
580580
:return:
581581
"""
582-
for key in list(self.rules_db_info.keys()):
582+
for key in self.rules_db_info:
583583
if self.current_table is None or self.current_table == key[0]:
584584
self.configdb.mod_entry(self.ACL_RULE, key, None)
585585
# Program for per front asic namespace also if present
586-
for namespace_configdb in list(self.per_npu_configdb.values()):
586+
for namespace_configdb in self.per_npu_configdb.values():
587587
namespace_configdb.mod_entry(self.ACL_RULE, key, None)
588588

589589

590590
self.configdb.mod_config({self.ACL_RULE: self.rules_info})
591591
# Program for per front asic namespace also if present
592-
for namespace_configdb in list(self.per_npu_configdb.values()):
592+
for namespace_configdb in self.per_npu_configdb.values():
593593
namespace_configdb.mod_config({self.ACL_RULE: self.rules_info})
594594

595595
def incremental_update(self):
@@ -630,15 +630,15 @@ def incremental_update(self):
630630
for key in current_dataplane_rules:
631631
self.configdb.mod_entry(self.ACL_RULE, key, None)
632632
# Program for per-asic namespace also if present
633-
for namespace_configdb in list(self.per_npu_configdb.values()):
633+
for namespace_configdb in self.per_npu_configdb.values():
634634
namespace_configdb.mod_entry(self.ACL_RULE, key, None)
635635

636636

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

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

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

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

670670
def delete(self, table=None, rule=None):
@@ -673,12 +673,12 @@ def delete(self, table=None, rule=None):
673673
:param rule:
674674
:return:
675675
"""
676-
for key in self.rules_db_info.keys():
676+
for key in self.rules_db_info:
677677
if not table or table == key[0]:
678678
if not rule or rule == key[1]:
679679
self.configdb.set_entry(self.ACL_RULE, key, None)
680680
# Program for per-asic namespace corresponding to front asic also if present.
681-
for namespace_configdb in list(self.per_npu_configdb.values()):
681+
for namespace_configdb in self.per_npu_configdb.values():
682682
namespace_configdb.set_entry(self.ACL_RULE, key, None)
683683

684684
def show_table(self, table_name):

config/config_mgmt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def _mergeItems(it1, it2):
545545
pass
546546
return
547547

548-
for it in list(D1.keys()):
548+
for it in D1:
549549
# D2 has the key
550550
if D2.get(it):
551551
_mergeItems(D1[it], D2[it])
@@ -577,7 +577,7 @@ def _searchKeysInConfig(self, In, Out, skeys):
577577
'''
578578
found = False
579579
if isinstance(In, dict):
580-
for key in list(In.keys()):
580+
for key in In:
581581
for skey in skeys:
582582
# pattern is very specific to current primary keys in
583583
# config DB, may need to be updated later.

config/main.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _get_breakout_options(ctx, args, incomplete):
9494

9595
def shutdown_interfaces(ctx, del_intf_dict):
9696
""" shut down all the interfaces before deletion """
97-
for intf in list(del_intf_dict.keys()):
97+
for intf in del_intf_dict:
9898
config_db = ctx.obj['config_db']
9999
if clicommon.get_interface_naming_mode() == "alias":
100100
interface_name = interface_alias_to_name(config_db, intf)
@@ -295,7 +295,7 @@ def interface_alias_to_name(config_db, interface_alias):
295295
if not port_dict:
296296
click.echo("port_dict is None!")
297297
raise click.Abort()
298-
for port_name in list(port_dict.keys()):
298+
for port_name in port_dict:
299299
if interface_alias == port_dict[port_name]['alias']:
300300
return port_name if sub_intf_sep_idx == -1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id
301301

@@ -326,15 +326,15 @@ def interface_name_is_valid(config_db, interface_name):
326326
if not port_dict:
327327
click.echo("port_dict is None!")
328328
raise click.Abort()
329-
for port_name in list(port_dict.keys()):
329+
for port_name in port_dict:
330330
if interface_name == port_name:
331331
return True
332332
if port_channel_dict:
333-
for port_channel_name in list(port_channel_dict.keys()):
333+
for port_channel_name in port_channel_dict:
334334
if interface_name == port_channel_name:
335335
return True
336336
if sub_port_intf_dict:
337-
for sub_port_intf_name in list(sub_port_intf_dict.keys()):
337+
for sub_port_intf_name in sub_port_intf_dict:
338338
if interface_name == sub_port_intf_name:
339339
return True
340340
return False
@@ -357,7 +357,7 @@ def interface_name_to_alias(config_db, interface_name):
357357
if not port_dict:
358358
click.echo("port_dict is None!")
359359
raise click.Abort()
360-
for port_name in list(port_dict.keys()):
360+
for port_name in port_dict:
361361
if interface_name == port_name:
362362
return port_dict[port_name]['alias']
363363

@@ -410,7 +410,7 @@ def get_port_namespace(port):
410410
if clicommon.get_interface_naming_mode() == "alias":
411411
port_dict = config_db.get_table(table_name)
412412
if port_dict:
413-
for port_name in list(port_dict.keys()):
413+
for port_name in port_dict:
414414
if port == port_dict[port_name]['alias']:
415415
return namespace
416416
else:
@@ -427,7 +427,7 @@ def del_interface_bind_to_vrf(config_db, vrf_name):
427427
for table_name in tables:
428428
interface_dict = config_db.get_table(table_name)
429429
if interface_dict:
430-
for interface_name in list(interface_dict.keys()):
430+
for interface_name in interface_dict:
431431
if 'vrf_name' in interface_dict[interface_name] and vrf_name == interface_dict[interface_name]['vrf_name']:
432432
interface_dependent = interface_ipaddr_dependent_on_interface(config_db, interface_name)
433433
for interface_del in interface_dependent:
@@ -459,7 +459,7 @@ def set_interface_naming_mode(mode):
459459
click.echo("port_dict is None!")
460460
raise click.Abort()
461461

462-
for port_name in list(port_dict.keys()):
462+
for port_name in port_dict:
463463
try:
464464
if port_dict[port_name]['alias']:
465465
pass
@@ -639,7 +639,7 @@ def _get_disabled_services_list(config_db):
639639

640640
feature_table = config_db.get_table('FEATURE')
641641
if feature_table is not None:
642-
for feature_name in list(feature_table.keys()):
642+
for feature_name in feature_table:
643643
if not feature_name:
644644
log.log_warning("Feature is None")
645645
continue
@@ -751,15 +751,15 @@ def _restart_services(config_db):
751751

752752
def interface_is_in_vlan(vlan_member_table, interface_name):
753753
""" Check if an interface is in a vlan """
754-
for _, intf in list(vlan_member_table.keys()):
754+
for _, intf in vlan_member_table:
755755
if intf == interface_name:
756756
return True
757757

758758
return False
759759

760760
def interface_is_in_portchannel(portchannel_member_table, interface_name):
761761
""" Check if an interface is part of portchannel """
762-
for _, intf in list(portchannel_member_table.keys()):
762+
for _, intf in portchannel_member_table:
763763
if intf == interface_name:
764764
return True
765765

@@ -2124,17 +2124,17 @@ def startup(ctx, interface_name):
21242124

21252125
log.log_info("'interface startup {}' executing...".format(interface_name))
21262126
port_dict = config_db.get_table('PORT')
2127-
for port_name in list(port_dict.keys()):
2127+
for port_name in port_dict:
21282128
if port_name in intf_fs:
21292129
config_db.mod_entry("PORT", port_name, {"admin_status": "up"})
21302130

21312131
portchannel_list = config_db.get_table("PORTCHANNEL")
2132-
for po_name in list(portchannel_list.keys()):
2132+
for po_name in portchannel_list:
21332133
if po_name in intf_fs:
21342134
config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "up"})
21352135

21362136
subport_list = config_db.get_table("VLAN_SUB_INTERFACE")
2137-
for sp_name in list(subport_list.keys()):
2137+
for sp_name in subport_list:
21382138
if sp_name in intf_fs:
21392139
config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "up"})
21402140

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

21662166
port_dict = config_db.get_table('PORT')
2167-
for port_name in list(port_dict.keys()):
2167+
for port_name in port_dict:
21682168
if port_name in intf_fs:
21692169
config_db.mod_entry("PORT", port_name, {"admin_status": "down"})
21702170

21712171
portchannel_list = config_db.get_table("PORTCHANNEL")
2172-
for po_name in list(portchannel_list.keys()):
2172+
for po_name in portchannel_list:
21732173
if po_name in intf_fs:
21742174
config_db.mod_entry("PORTCHANNEL", po_name, {"admin_status": "down"})
21752175

21762176
subport_list = config_db.get_table("VLAN_SUB_INTERFACE")
2177-
for sp_name in list(subport_list.keys()):
2177+
for sp_name in subport_list:
21782178
if sp_name in intf_fs:
21792179
config_db.mod_entry("VLAN_SUB_INTERFACE", sp_name, {"admin_status": "down"})
21802180

@@ -2299,7 +2299,7 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load
22992299
cm = load_ConfigMgmt(verbose)
23002300

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

config/nat.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def isOverlappingWithAnyDynamicEntry(ipAddress):
107107
if not nat_pool_dict:
108108
return False
109109

110-
for values in list(nat_pool_dict.values()):
110+
for values in nat_pool_dict.values():
111111
global_ip = values["nat_ip"]
112112
ipAddr = global_ip.split('-')
113113
if (len(ipAddr) == 1):
@@ -604,7 +604,7 @@ def remove_static_all(ctx):
604604
for table_name in tables:
605605
table_dict = config_db.get_table(table_name)
606606
if table_dict:
607-
for table_key_name in list(table_dict.keys()):
607+
for table_key_name in table_dict:
608608
config_db.set_entry(table_name, table_key_name, None)
609609

610610
#
@@ -828,7 +828,7 @@ def remove_pools(ctx):
828828
binding_dict = config_db.get_table(binding_table_name)
829829
pool_dict = config_db.get_table(pool_table_name)
830830
if pool_dict:
831-
for pool_key_name in list(pool_dict.keys()):
831+
for pool_key_name in pool_dict:
832832
entryFound = False
833833
for binding_name, binding_values in binding_dict.items():
834834
if binding_values['nat_pool'] == pool_key_name:
@@ -880,7 +880,7 @@ def remove_bindings(ctx):
880880
binding_table_name = 'NAT_BINDINGS'
881881
binding_dict = config_db.get_table(binding_table_name)
882882
if binding_dict:
883-
for binding_key_name in list(binding_dict.keys()):
883+
for binding_key_name in binding_dict:
884884
config_db.set_entry(binding_table_name, binding_key_name, None)
885885

886886
#
@@ -961,7 +961,7 @@ def remove_interfaces(ctx):
961961
for table_name in tables:
962962
table_dict = config_db.get_table(table_name)
963963
if table_dict:
964-
for table_key_name in list(table_dict.keys()):
964+
for table_key_name in table_dict:
965965
if isinstance(table_key_name, str) is False:
966966
continue
967967

fdbutil/filter_fdb_entries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_vlan_cidr_map(filename):
2828

2929
vlan_cidr = defaultdict()
3030
if "VLAN_INTERFACE" in config_db_entries and "VLAN" in config_db_entries:
31-
for vlan_key in list(config_db_entries["VLAN_INTERFACE"].keys()):
31+
for vlan_key in config_db_entries["VLAN_INTERFACE"]:
3232
if '|' not in vlan_key:
3333
continue
3434
vlan, cidr = tuple(vlan_key.split('|'))

fwutil/lib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def __validate_component_map(self, section, pdp_map, pcp_map):
518518
)
519519
)
520520

521-
for key in list(pdp_map.keys()):
521+
for key in pdp_map:
522522
diff_keys = self.__diff_keys(list(pdp_map[key].keys()), list(pcp_map[key].keys()))
523523

524524
if diff_keys:

pfcwd/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ def get_all_ports(db, namespace=None, display=constants.DISPLAY_ALL):
8686
def get_server_facing_ports(db):
8787
candidates = db.get_table('DEVICE_NEIGHBOR')
8888
server_facing_ports = []
89-
for port in list(candidates.keys()):
89+
for port in candidates:
9090
neighbor = db.get_entry(
9191
'DEVICE_NEIGHBOR_METADATA', candidates[port]['name']
9292
)
9393
if neighbor and neighbor['type'].lower() == 'server':
9494
server_facing_ports.append(port)
9595
if not server_facing_ports:
96-
server_facing_ports = [p[1] for p in list(db.get_table('VLAN_MEMBER').keys())]
96+
server_facing_ports = [p[1] for p in db.get_table('VLAN_MEMBER')]
9797
return server_facing_ports
9898

9999

scripts/aclshow

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class AclStat(object):
124124
"""
125125
Get ACL counters from the DB
126126
"""
127-
for table, rule in self.acl_rules.keys():
127+
for table, rule in self.acl_rules:
128128
cnt_props = lowercase_keys(self.db.get_all(self.db.COUNTERS_DB, "COUNTERS:%s:%s" % (table, rule)))
129129
self.acl_counters[table, rule] = cnt_props
130130

@@ -163,7 +163,7 @@ class AclStat(object):
163163

164164
header = ACL_HEADER
165165
aclstat = []
166-
for rule_key in self.acl_rules.keys():
166+
for rule_key in self.acl_rules:
167167
if not display_all and (self.get_counter_value(rule_key, 'packets') == '0' or \
168168
self.get_counter_value(rule_key, 'packets') == 'N/A'):
169169
continue

0 commit comments

Comments
 (0)