Skip to content

Commit

Permalink
py3: Use modern idioms with built-in types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pyldap contributors authored and Petr Viktorin committed Nov 24, 2017
1 parent 17365cc commit 17b4000
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Demo/Lib/ldap/async/deltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions Lib/ldap/modlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = []
Expand Down
7 changes: 4 additions & 3 deletions Lib/ldap/schema/subentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions Lib/ldif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:]
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Tests/t_cidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 17b4000

Please sign in to comment.