tab compleation input
Eli Criffield
elicriffield at gmail.com
Mon Nov 13 18:16:45 EST 2006
#Thanks for the link from Fredrik Lundh
#from http://effbot.org/librarybook/readline.htm
#You do it like this,
class Completer:
def __init__(self, words):
self.words = words
self.prefix = None
def complete(self, prefix, index):
if prefix != self.prefix:
self.matching_words = [
w for w in self.words if w.startswith(prefix)
]
self.prefix = prefix
try:
return self.matching_words[index]
except IndexError:
return None
import readline
# a set of more or less interesting words
validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]
completer = Completer(validanswers)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
# try it out!
while 1:
answer = raw_input("Answer the Question: ")
if answer not in validanswers:
print "Wrong!"
else:
print "Your answer is",answer
Fredrik Lundh wrote:
> Eli Criffield wrote:
>
> > Not sure what your trying to do, but it doesn't work. :)
> > valid doesn't exsist
>
> it's assigned to by the for-in loop, and is only used inside it.
>
> > Either way, it doesn't take the tab tell after you hit enter, Not
> > really what i was getting at. What i want is like a bash shell prompt.
>
> you can use the "readline" module with a custom completer class. see
> the second example on this page:
>
> http://effbot.org/librarybook/readline.htm
>
> </F>
More information about the Python-list
mailing list