From 17b4000d7d0eaab602098ac3aa5d858373d659af Mon Sep 17 00:00:00 2001 From: pyldap contributors Date: Thu, 23 Nov 2017 21:30:42 +0100 Subject: [PATCH] py3: Use modern idioms with built-in types d.has_key(x) -> x in d list(x); x.sort() -> sorted(x) filter(lambda...) -> list comprehension d.keys() -> list(d.keys()) (when keys are modified) x == None -> x is None --- Demo/Lib/ldap/async/deltree.py | 2 +- Lib/ldap/modlist.py | 6 +++--- Lib/ldap/schema/subentry.py | 7 ++++--- Lib/ldif.py | 6 +++--- Tests/t_cidict.py | 6 ++---- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Demo/Lib/ldap/async/deltree.py b/Demo/Lib/ldap/async/deltree.py index 9e23f1c..68d3643 100644 --- a/Demo/Lib/ldap/async/deltree.py +++ b/Demo/Lib/ldap/async/deltree.py @@ -30,7 +30,7 @@ def startSearch(self,searchRoot,searchScope): ) def _processSingleResult(self,resultType,resultItem): - if self._entryResultTypes.has_key(resultType): + if resultType in self._entryResultTypes: # Don't process search references dn,entry = resultItem hasSubordinates = entry.get( diff --git a/Lib/ldap/modlist.py b/Lib/ldap/modlist.py index 6f4bf1e..a853500 100644 --- a/Lib/ldap/modlist.py +++ b/Lib/ldap/modlist.py @@ -18,7 +18,7 @@ def addModlist(entry,ignore_attr_types=None): # This attribute type is ignored continue # Eliminate empty attr value strings in list - attrvaluelist = filter(lambda x:x!=None,entry[attrtype]) + attrvaluelist = [item for item in entry[attrtype] if item is not None] if attrvaluelist: modlist.append((attrtype,entry[attrtype])) return modlist # addModlist() @@ -58,10 +58,10 @@ def modifyModlist( # This attribute type is ignored continue # Filter away null-strings - new_value = filter(lambda x:x!=None,new_entry[attrtype]) + new_value = [item for item in new_entry[attrtype] if item is not None] if attrtype_lower in attrtype_lower_map: old_value = old_entry.get(attrtype_lower_map[attrtype_lower],[]) - old_value = filter(lambda x:x!=None,old_value) + old_value = [item for item in old_value if item is not None] del attrtype_lower_map[attrtype_lower] else: old_value = [] diff --git a/Lib/ldap/schema/subentry.py b/Lib/ldap/schema/subentry.py index 3612a38..fbc33c5 100644 --- a/Lib/ldap/schema/subentry.py +++ b/Lib/ldap/schema/subentry.py @@ -294,7 +294,8 @@ def get_structural_oc(self,oc_list): if oc_se and oc_se.kind==0: struct_ocs[oc_se.oid] = None result = None - struct_oc_list = struct_ocs.keys() + # Build a copy of the oid list, to be cleaned as we go. + struct_oc_list = list(struct_ocs) while struct_oc_list: oid = struct_oc_list.pop() for child_oid in oc_tree[oid]: @@ -417,14 +418,14 @@ def attribute_types( # Remove all mandantory attribute types from # optional attribute type list - for a in r_may.keys(): + for a in list(r_may.keys()): 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 l.keys(): + for a in list(l.keys()): for afk,afv in attr_type_filter: try: schema_attr_type = self.sed[AttributeType][a] diff --git a/Lib/ldif.py b/Lib/ldif.py index 714d01c..318f46c 100644 --- a/Lib/ldif.py +++ b/Lib/ldif.py @@ -125,7 +125,7 @@ def _needs_base64_encoding(self,attr_type,attr_value): returns 1 if attr_value has to be base-64 encoded because of special chars or because attr_type is in self._base64_attrs """ - return self._base64_attrs.has_key(attr_type.lower()) or \ + return attr_type.lower() in self._base64_attrs or \ not safe_string_re.search(attr_value) is None def _unparseAttrTypeandValue(self,attr_type,attr_value): @@ -351,7 +351,7 @@ def _next_key_and_value(self): attr_value = None if self._process_url_schemes: u = urlparse.urlparse(url) - if self._process_url_schemes.has_key(u[0]): + if u[0] in self._process_url_schemes: attr_value = urllib.urlopen(url).read() else: attr_value = unfolded_line[colon_pos+1:] @@ -369,7 +369,7 @@ def _consume_empty_lines(self): # Consume empty lines try: k,v = next_key_and_value() - while k==v==None: + while k is None and v is None: k,v = next_key_and_value() except EOFError: k,v = None,None diff --git a/Tests/t_cidict.py b/Tests/t_cidict.py index c8812f2..dea802d 100644 --- a/Tests/t_cidict.py +++ b/Tests/t_cidict.py @@ -32,11 +32,9 @@ def test_cidict(self): cix["xYZ"] = 987 self.assertEqual(cix["XyZ"], 987) self.assertEqual(cix.get("xyz", None), 987) - cix_keys = cix.keys() - cix_keys.sort() + cix_keys = sorted(cix.keys()) self.assertEqual(cix_keys, ['AbCDeF','xYZ']) - cix_items = cix.items() - cix_items.sort() + cix_items = sorted(cix.items()) self.assertEqual(cix_items, [('AbCDeF',123), ('xYZ',987)]) del cix["abcdEF"] self.assertEqual("abcdef" in cix, False)