我有一个python脚本的这段代码部分,我必须查询用户的ldap属性:
try:
ldap_result_id = l.search(baseDN, searchScope, get_searchFilter(adname),
retrieveAttributes)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## you could do whatever you want with the individual entry
## The appending to list is just for illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
for x in result_set:
print x
except ldap.LDAPError, e:
print e
print ldap.LDAPError
我该如何清理它并使其成为可重用的功能(或者方法是更合适的术语)?
确定可以更改的变量并将这些参数传递给函数。将打印和异常处理留在函数之外,除非您可以对异常做一些明智的事情:
def fn(baseDN, searchScope, adname, retrieveAttributes):
ldap_result_id = l.search(baseDN, searchScope, get_searchFilter(adname),
retrieveAttributes)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
## you could do whatever you want with the individual entry
## The appending to list is just for illustration.
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
return result_set
baseDN = ???
searchScope = ???
adname = ???
retrieveAttributes = ???
try:
for x in fn(baseDN, searchScope, adname, retrieveAttributes):
print x
except ldap.LDAPError, e:
print e
print ldap.LDAPError
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句