473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

tab compleation input


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
5 1895
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

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
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
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

#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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gotcha | last post by:
I have a little code to add multiple items to a shopping cart based page. This code works perfect, but it adds all of the info to the same input fields every time it loops. I need it to change...
3
by: david | last post by:
HI! Im trying to make "HTML form" into automatic. 1. If I get 18 numbers like: A B C D E F . . . . 2. How can I put those 18 numbers automatically into 6 numbers format like: A B C D E F
2
by: SophistiCat | last post by:
Hi, I am working on a computational program that has to read a number of parameters (~50) from an input file. The program contains a single class hierarchy with about a dozen member-classes or...
2
by: Cranky | last post by:
Ok, here is my scenario: I need to input numbers using my handheld IPAQ. I figured out how to create an online numeric keypad for inputting numbers into an input field, what I need to know is how...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
3
by: cbradio | last post by:
Hi, I am having trouble developing a form in a restricted environment. My sample code is found below my message (sorry I don't have a URL). Basically, without a doctype, the form displays properly...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
1
by: tcertain | last post by:
I am totally duh at javascript although I have 2 books trying to learn it. I am trying to add values to a form and have a calculate total at end. this is my form script. I have hours at end of...
1
by: printline | last post by:
Hello All I'm quite new to xml vs. PHP, so i hope someone can help with an issue i have been struggeling with. I have an html form, that when submitted, it should create an xml file, and save...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.