Skip to content

Commit

Permalink
Lib: Use items() when appropriate in dict iteration
Browse files Browse the repository at this point in the history
When both key and value are needed,

    for key, value in dictionary.items():
        ...

is used instead of:

    for key in dictionary.keys():
        value = dictionary[key]
        ...

Thanks to Jon Dufresne for finding keys() usages.
  • Loading branch information
Petr Viktorin authored and Petr Viktorin committed Mar 14, 2018
1 parent 28528ea commit 4ea6e31
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Lib/ldap/cidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __delitem__(self,key):
del self.data[lower_key]

def update(self,dict):
for key in dict.keys():
self[key] = dict[key]
for key, value in dict.items():
self[key] = value

def has_key(self,key):
return key in self
Expand Down
14 changes: 7 additions & 7 deletions Lib/ldap/modlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def addModlist(entry,ignore_attr_types=None):
"""Build modify list for call of method LDAPObject.add()"""
ignore_attr_types = {v.lower() for v in ignore_attr_types or []}
modlist = []
for attrtype in entry.keys():
for attrtype, value in entry.items():
if attrtype.lower() in ignore_attr_types:
# This attribute type is ignored
continue
# Eliminate empty attr value strings in list
attrvaluelist = [item for item in entry[attrtype] if item is not None]
attrvaluelist = [item for item in value if item is not None]
if attrvaluelist:
modlist.append((attrtype,entry[attrtype]))
modlist.append((attrtype, value))
return modlist # addModlist()


Expand Down Expand Up @@ -52,13 +52,13 @@ def modifyModlist(
attrtype_lower_map = {}
for a in old_entry.keys():
attrtype_lower_map[a.lower()]=a
for attrtype in new_entry.keys():
for attrtype, value in new_entry.items():
attrtype_lower = attrtype.lower()
if attrtype_lower in ignore_attr_types:
# This attribute type is ignored
continue
# Filter away null-strings
new_value = [item for item in new_entry[attrtype] if item is not None]
new_value = [item for item in value if item is not None]
if attrtype_lower in attrtype_lower_map:
old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[])
old_value = [item for item in old_value if item is not None]
Expand Down Expand Up @@ -88,10 +88,10 @@ def modifyModlist(
if not ignore_oldexistent:
# Remove all attributes of old_entry which are not present
# in new_entry at all
for a in attrtype_lower_map.keys():
for a, val in attrtype_lower_map.items():
if a in ignore_attr_types:
# This attribute type is ignored
continue
attrtype = attrtype_lower_map[a]
attrtype = val
modlist.append((ldap.MOD_DELETE,attrtype,None))
return modlist # modifyModlist()
4 changes: 2 additions & 2 deletions Lib/ldap/schema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ def _at2key(self,nameoroid):
return t

def update(self,dict):
for key in dict.keys():
self[key] = dict[key]
for key, value in dict.values():
self[key] = value

def __contains__(self,nameoroid):
return self._at2key(nameoroid) in self.data
Expand Down
7 changes: 3 additions & 4 deletions Lib/ldap/schema/subentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def ldap_entry(self):
entry = {}
# Collect the schema elements and store them in
# entry's attributes
for se_class in self.sed.keys():
for se in self.sed[se_class].values():
for se_class, elements in self.sed.items():
for se in elements.values():
se_str = str(se)
try:
entry[SCHEMA_ATTR_MAPPING[se_class]].append(se_str)
Expand All @@ -153,8 +153,7 @@ def listall(self,schema_element_class,schema_element_filters=None):
avail_se = self.sed[schema_element_class]
if schema_element_filters:
result = []
for se_key in avail_se.keys():
se = avail_se[se_key]
for se_key, se in avail_se.items():
for fk,fv in schema_element_filters:
try:
if getattr(se,fk) in fv:
Expand Down
4 changes: 2 additions & 2 deletions Lib/ldapurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@ def applyDefaults(self,defaults):
Dictionary containing a mapping from class attributes
to default values
"""
for k in defaults.keys():
for k, value in defaults.items():
if getattr(self,k) is None:
setattr(self,k,defaults[k])
setattr(self, k, value)

def initializeUrl(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions Lib/ldif.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def _unparseEntryRecord(self,entry):
entry
dictionary holding an entry
"""
for attr_type in sorted(entry.keys()):
for attr_value in entry[attr_type]:
for attr_type, values in sorted(entry.items()):
for attr_value in values:
self._unparseAttrTypeandValue(attr_type,attr_value)

def _unparseChangeRecord(self,modlist):
Expand Down

0 comments on commit 4ea6e31

Please sign in to comment.