By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

tab compleation input

P: n/a

Here is what i want to do. I have a question in my program, and i want
to do tab completion for the valid answers.

Say i have :
--snip--
validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]

#and i ask

sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
if answer not in valid answers:
print "Wrong!"
--snip--

But what i want is when i enter the answer i can hit tab and it'll
complete one of the validanswers
I know i can do tab complete with readline and 'raw_input('')' but
that's only to execute python commands right?

Eli

Nov 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On a gui on this would be a little bit easier but it's a completley
diffrent realm when doing it in the console. It makes it a little more
difficult when using stdin.readline() because you can only read line by
line. Here is my implmentation.

import sys

validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]
while True:
sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
for valid in validanswers:
if valid.startswith(answer.strip("\t")):
answer = valid
else:
print "Invalid Answer: Please enter a valid answer"
continue
break
print "You have answered, ", answer

I'm at school and wasn't able to test it, but it looks like it should
work.

Eli Criffield wrote:
Here is what i want to do. I have a question in my program, and i want
to do tab completion for the valid answers.

Say i have :
--snip--
validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]

#and i ask

sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
if answer not in valid answers:
print "Wrong!"
--snip--

But what i want is when i enter the answer i can hit tab and it'll
complete one of the validanswers
I know i can do tab complete with readline and 'raw_input('')' but
that's only to execute python commands right?

Eli
Nov 13 '06 #2

P: n/a

Not sure what your trying to do, but it doesn't work. :)
valid doesn't exsist, and if you make vaid =
sys.stdin.readline().rstrip(), then answer doesn't exsist.

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.
>From a bash shell prompt try ls <TAB<TAB>, like that. (readline does
that for bash, but i don't know how to get readline to do anything but
python commands in python).

Eli

da****@gmail.com wrote:
On a gui on this would be a little bit easier but it's a completley
diffrent realm when doing it in the console. It makes it a little more
difficult when using stdin.readline() because you can only read line by
line. Here is my implmentation.

import sys

validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]
while True:
sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
for valid in validanswers:
if valid.startswith(answer.strip("\t")):
answer = valid
else:
print "Invalid Answer: Please enter a valid answer"
continue
break
print "You have answered, ", answer

I'm at school and wasn't able to test it, but it looks like it should
work.

Eli Criffield wrote:
Here is what i want to do. I have a question in my program, and i want
to do tab completion for the valid answers.

Say i have :
--snip--
validanswers = [ 'yes', 'no', 'maybe', 'tuesday', 'never' ]

#and i ask

sys.stdout.write("Answer the Question: ")
answer = sys.stdin.readline().rstrip()
if answer not in valid answers:
print "Wrong!"
--snip--

But what i want is when i enter the answer i can hit tab and it'll
complete one of the validanswers
I know i can do tab complete with readline and 'raw_input('')' but
that's only to execute python commands right?

Eli
Nov 13 '06 #3

P: n/a
ZeD
Eli Criffield wrote:
Here is what i want to do. I have a question in my program, and i want
to do tab completion for the valid answers.
I think you may "play" width curses library: readline() read... while you
input a "newline", but you need to catch single keys (the "TAB" key,
foremost)

--
Under construction
Nov 13 '06 #4

P: n/a
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>

Nov 13 '06 #5

P: n/a

#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>
Nov 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.