Skip to content

Commit

Permalink
Prefer iterating dict instead of calling dict.keys()
Browse files Browse the repository at this point in the history
Calling dict.keys() is unnecessary. iter(dict) is equivalent to
dict.keys(). Inspired by Lennart Regebro's talk "Prehistoric Patterns in
Python" from PyCon 2017.

https://www.youtube.com/watch?v=V5-JH23Vk0I
  • Loading branch information
Jon Dufresne authored and Petr Viktorin committed Sep 20, 2019
1 parent 9346fa5 commit f4059c4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Lib/ldap/modlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def modifyModlist(
case_ignore_attr_types = {v.lower() for v in case_ignore_attr_types or []}
modlist = []
attrtype_lower_map = {}
for a in old_entry.keys():
for a in old_entry:
attrtype_lower_map[a.lower()]=a
for attrtype, value in new_entry.items():
attrtype_lower = attrtype.lower()
Expand Down
12 changes: 6 additions & 6 deletions Lib/ldap/schema/subentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
SCHEMA_CLASS_MAPPING[o.schema_attribute] = o
SCHEMA_ATTR_MAPPING[o] = o.schema_attribute

SCHEMA_ATTRS = SCHEMA_CLASS_MAPPING.keys()
SCHEMA_ATTRS = list(SCHEMA_CLASS_MAPPING)


class SubschemaError(ValueError):
Expand Down Expand Up @@ -122,15 +122,15 @@ def __init__(self,sub_schema_sub_entry,check_uniqueness=1):
self.sed[se_class][se_id] = se_instance

if hasattr(se_instance,'names'):
for name in ldap.cidict.cidict({}.fromkeys(se_instance.names)).keys():
for name in ldap.cidict.cidict({}.fromkeys(se_instance.names)):
if check_uniqueness and name in self.name2oid[se_class]:
self.non_unique_names[se_class][se_id] = None
raise NameNotUnique(attr_value)
else:
self.name2oid[se_class][name] = se_id

# Turn dict into list maybe more handy for applications
self.non_unique_oids = self.non_unique_oids.keys()
self.non_unique_oids = list(self.non_unique_oids)

return # subSchema.__init__()

Expand Down Expand Up @@ -168,7 +168,7 @@ def listall(self,schema_element_class,schema_element_filters=None):
except AttributeError:
pass
else:
result = avail_se.keys()
result = list(avail_se)
return result


Expand Down Expand Up @@ -422,14 +422,14 @@ def attribute_types(

# Remove all mandantory attribute types from
# optional attribute type list
for a in list(r_may.keys()):
for a in list(r_may):
if a in r_must:
del r_may[a]

# Apply attr_type_filter to results
if attr_type_filter:
for l in [r_must,r_may]:
for a in list(l.keys()):
for a in list(l):
for afk,afv in attr_type_filter:
try:
schema_attr_type = self.sed[AttributeType][a]
Expand Down
2 changes: 1 addition & 1 deletion Tests/t_ldap_syncrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def syncrepl_present(self, uuids, refreshDeletes=False):

elif (uuids is None) and (refreshDeletes is False):
deleted_uuids = []
for uuid in self.uuid_dn.keys():
for uuid in self.uuid_dn:
if uuid not in self.present:
deleted_uuids.append(uuid)

Expand Down

0 comments on commit f4059c4

Please sign in to comment.