From 4ea6e31b361eacf5afc7c831211e5d908c340e1b Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 14 Mar 2018 11:18:23 +0100 Subject: [PATCH] Lib: Use items() when appropriate in dict iteration 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. --- Lib/ldap/cidict.py | 4 ++-- Lib/ldap/modlist.py | 14 +++++++------- Lib/ldap/schema/models.py | 4 ++-- Lib/ldap/schema/subentry.py | 7 +++---- Lib/ldapurl.py | 4 ++-- Lib/ldif.py | 4 ++-- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Lib/ldap/cidict.py b/Lib/ldap/cidict.py index 3ba2feb..4f7e091 100644 --- a/Lib/ldap/cidict.py +++ b/Lib/ldap/cidict.py @@ -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 diff --git a/Lib/ldap/modlist.py b/Lib/ldap/modlist.py index 6f51312..4acf4e9 100644 --- a/Lib/ldap/modlist.py +++ b/Lib/ldap/modlist.py @@ -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() @@ -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] @@ -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() diff --git a/Lib/ldap/schema/models.py b/Lib/ldap/schema/models.py index 9a8cb5b..feb7bff 100644 --- a/Lib/ldap/schema/models.py +++ b/Lib/ldap/schema/models.py @@ -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 diff --git a/Lib/ldap/schema/subentry.py b/Lib/ldap/schema/subentry.py index 6091a75..aa094cb 100644 --- a/Lib/ldap/schema/subentry.py +++ b/Lib/ldap/schema/subentry.py @@ -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) @@ -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: diff --git a/Lib/ldapurl.py b/Lib/ldapurl.py index b3e1171..5159d89 100644 --- a/Lib/ldapurl.py +++ b/Lib/ldapurl.py @@ -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): """ diff --git a/Lib/ldif.py b/Lib/ldif.py index cdcccc0..3fbb2b1 100644 --- a/Lib/ldif.py +++ b/Lib/ldif.py @@ -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):