[kupfer: 10/51] grouping: Change GroupingLeaf's __getitem__ to return only one value



commit f6487e4849f83cc2f78cc2dbb9227d0ce79d06eb
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Jan 7 02:00:18 2010 +0100

    grouping: Change GroupingLeaf's __getitem__ to return only one value
    
    Since this is the most common usage. GroupingLeaf.all(key) returns all
    values.

 kupfer/obj/grouping.py |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
---
diff --git a/kupfer/obj/grouping.py b/kupfer/obj/grouping.py
index 08e9958..6a87563 100644
--- a/kupfer/obj/grouping.py
+++ b/kupfer/obj/grouping.py
@@ -50,8 +50,15 @@ class GroupingLeaf (Leaf):
 		return any(key in leaf.object for leaf in self.links)
 
 	def __getitem__(self, key):
+		"Get first (canonical) value for key"
+		try:
+			return iter(self.all(key)).next()
+		except StopIteration:
+			raise KeyError("%s has no slot %s" % (self, key))
+
+	def all(self, key):
 		"Return iterator of all values for @key"
-		return [leaf.object[key] for leaf in self.links if key in leaf.object]
+		return (leaf.object[key] for leaf in self.links if key in leaf.object)
 
 class GroupingSource (Source):
 	grouping_keys = [EMAIL_KEY, NAME_KEY, JID_KEY]
@@ -100,7 +107,7 @@ class GroupingSource (Source):
 				continue
 			for leaf in list(leaves):
 				for slot2 in self.grouping_keys:
-					for value2 in leaf[slot2]:
+					for value2 in leaf.all(slot2):
 						merge_groups((slot, value), (slot2, value2))
 		self.output_debug("MERGED ALL", time.time() - st)
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]