[Da-tools-commits] ./da-tools/userdir-ldap-cgi-python r17: Adding a cuter and faster version of the sortResults Method.

Robin Wittler real at geek-at-work.org
Mon Jan 26 22:13:43 UTC 2009


------------------------------------------------------------
revno: 17
committer: Robin Wittler <real at geek-at-work.org>
branch nick: userdir-ldap-cgi-python
timestamp: Mon 2009-01-26 23:13:43 +0100
message:
  Adding a cuter and faster version of the sortResults Method.
modified:
  ud_ldap_ng/controllers/udldapng.py
  ud_ldap_ng/lib/libldap.py
-------------- next part --------------
=== modified file 'ud_ldap_ng/controllers/udldapng.py'
--- a/ud_ldap_ng/controllers/udldapng.py	2009-01-26 20:20:36 +0000
+++ b/ud_ldap_ng/controllers/udldapng.py	2009-01-26 22:13:43 +0000
@@ -20,15 +20,16 @@
 	def index(self):
 		con = AnonymousDebianLDAP()
 		c.attributes = ['hostname', 'architecture', 'purpose', 'sponsor', 'status']
-		c.sortorders = {'asc': False, 'dsc': True}
+		c.sortorders = {u'asc': False, u'dsc': True}
 		c.result = con.getDebianHosts(attrlist=c.attributes, adjust=True, default_value='')
 		c.sortorder = request.params.get('sortorder', None)
 		if not c.sortorder or c.sortorder not in c.sortorders:
 			sortorder = c.sortorders.get('asc')
+			c.sortorder = u'asc'
 		else:
 			sortorder = c.sortorders.get(c.sortorder)
 		c.sortby = request.params.get('sortby', None)
 		if not c.sortby or c.sortby not in c.attributes:
-			c.sortby = 'hostname'
+			c.sortby = u'hostname'
 		c.results = c.result.sortResults(c.sortby, reverse=sortorder)
 		return render('/machines.html')

=== modified file 'ud_ldap_ng/lib/libldap.py'
--- a/ud_ldap_ng/lib/libldap.py	2009-01-25 19:50:15 +0000
+++ b/ud_ldap_ng/lib/libldap.py	2009-01-26 22:13:43 +0000
@@ -132,7 +132,7 @@
 		return ret_list
 	
 	@classmethod
-	def sortResults(cls, keyword, reverse=False):
+	def sortResults(cls, keyword, reverse=False, default_value=''):
 		'''Sort the result dicts and returns it as a iterator.
 
 		@param keyword			The keyword to sort
@@ -141,27 +141,13 @@
 		@type reverse			bool
 		@return					iterator
 		'''
-		#TODO: This is all a bit ugly, please show me a better way to sort many dicts by one key. ;)
 		if not type(keyword) == str and not type(keyword) == unicode:
 			raise TypeError('keyword must be type str or unicode str')
 		if not keyword in cls.getAllAttrFromResults(cls.__results):
 			raise KeyError('keyword exist not in results')
 		if not type(reverse) == bool:
 			raise TypeError('reverse must be type bool')
-		ret_list = list()
-		for result in cls.__results:
-			if not keyword in result:
-				if not cls.adjusted:
-					ret_list.append(('zzzzzzzzzz', result))
-				else:
-					raise KeyError('keyword is not in adjusted result: %s' %(result))
-			else:
-				ret_list.append((result.get(keyword), result))
-		ret_list = sorted(ret_list, reverse=reverse)
-		for key, result in [(k,r) for k,r in ret_list]:
-			ret_list.remove((key, result))
-			ret_list.append(result)
-		return iter(ret_list)
+		return iter(sorted(cls.__results, key=lambda x: x.get(keyword, default_value), reverse=reverse))
 
 	@classmethod
 	def getAllAttrFromRawResults(cls, results):
@@ -347,8 +333,8 @@
 			raise TypeError('attrlist must be type list or None')
 		if attrlist:
 			for nr, value in enumerate(attrlist):
-				if not type(value) == str:
-					raise TypeError('value %s in attrlist must be type str' %(nr))
+				if not type(value) == str and not type(value) == unicode:
+					raise TypeError('value %s in attrlist must be type str or unicode string.' %(nr))
 		if not type(attrsonly) == int:
 			raise TypeError('attrsonly must be type int')
 		return self.con.search_s(dn, scope, filterstr=filterstr, attrlist=attrlist, attrsonly=attrsonly)
@@ -373,8 +359,8 @@
 		'''
 		if not getattr(self, 'con', None):
 			raise Exception('You must call the "connect" Method before.')
-		if not type(host) == str:
-			raise TypeError('host must be type str')
+		if not type(host) == str and not type(host) == unicode:
+			raise TypeError('host must be type str or unicode string')
 		dn = 'host=%s,ou=hosts,dc=debian,dc=org' %(host)
 		scope = ldap.SCOPE_SUBTREE
 		return DebianLDAPResult(self.ldapSearch(dn, scope, attrlist=attrlist))



More information about the Da-tools-commits mailing list