[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - tabcomplete.py:1.6

Fred L. Drake, Jr. fred@zope.com
Wed, 18 Jun 2003 15:52:18 -0400


Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv18746

Modified Files:
	tabcomplete.py 
Log Message:
Simplify implementation to better serve as an example.


=== Zope3/src/zope/schema/tests/tabcomplete.py 1.5 => 1.6 ===
--- Zope3/src/zope/schema/tests/tabcomplete.py:1.5	Wed Jun 18 15:44:32 2003
+++ Zope3/src/zope/schema/tests/tabcomplete.py	Wed Jun 18 15:52:18 2003
@@ -20,9 +20,11 @@
 
 
 class IPrefixQuery(IVocabularyQuery):
+    """Interface for prefix queries."""
     
     def queryForPrefix(prefix):
-        """Return a vocabulary that contains terms beginning with prefix."""
+        """Return a vocabulary that contains terms beginning with
+        prefix."""
 
 
 class Term:
@@ -32,30 +34,21 @@
         self.value = value
 
 
-class TermIterator:
-    def __init__(self, values):
-        self._next = iter(values).next
-
-    def __iter__(self):
-        return self
-
-    def next(self):
-        return Term(self._next())
-
-
 class CompletionVocabulary(object):
     implements(IVocabulary)
 
     def __init__(self, values):
         # In practice, something more dynamic could be used to
         # get the list possible completions.
-        self._values = tuple(values)
+        # We force a _values to be a list so we can use .index().
+        self._values = list(values)
+        self._terms = map(Term, self._values)
 
     def __contains__(self, value):
         return value in self._values
 
     def __iter__(self):
-        return TermIterator(self._values)
+        return iter(self._terms)
 
     def __len__(self):
         return len(self._values)
@@ -65,7 +58,7 @@
 
     def getTerm(self, value):
         if value in self._values:
-            return Term(value)
+            return self._terms[self._values.index(value)]
         raise LookupError(value)