473,394 Members | 1,750 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,394 software developers and data experts.

How to use gnu readline library in program?

I'm trying to figure out how to use the gnu readline library so
that when my program is prompting the user for input there is
line editing and history support.

I've read and re-read the documentation for the "readline"
module in the standard library and I still can't figure out how
to use the module or even if the module is intended to do what
I want. The example code all seems to be about on how to
modify the behavior of an interactive Python interpreter
session so you have things like auto-completion of Python
identifiers.

What I want to do is replace sys.stdin.readline() with
something that will provide the user with line editing and
history recall. In other languages, one uses the Gnu readline
library to do that, but my reading of the Python library
documentation is that's not what the Python readline module is
for. Am I wrong?

--
Grant Edwards grante Yow! On the road, ZIPPY
at is a pinhead without a
visi.com purpose, but never without
a POINT.
Jul 1 '08 #1
2 4398
Grant Edwards wrote:
I'm trying to figure out how to use the gnu readline library so
that when my program is prompting the user for input there is
line editing and history support.

I've read and re-read the documentation for the "readline"
module in the standard library and I still can't figure out how
to use the module or even if the module is intended to do what
I want. The example code all seems to be about on how to
modify the behavior of an interactive Python interpreter
session so you have things like auto-completion of Python
identifiers.

What I want to do is replace sys.stdin.readline() with
something that will provide the user with line editing and
history recall. In other languages, one uses the Gnu readline
library to do that, but my reading of the Python library
documentation is that's not what the Python readline module is
for. Am I wrong?
Here's a simple example:

import readline

for s in "alpha beta gamma".split():
readline.add_history(s)

candidates = "red yellow pink blue black".split()

def completer(word, index):
matches = [c for c in candidates if c.startswith(word)]
try:
return matches[index] + " "
except IndexError:
pass
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")

while 1:
print raw_input("$ ")

You may also consider using the cmd module.

Peter
Jul 1 '08 #2
>What I want to do is replace sys.stdin.readline() with
>something that will provide the user with line editing and
history recall. In other languages, one uses the Gnu readline
library to do that, but my reading of the Python library
documentation is that's not what the Python readline module is
for. Am I wrong?

Here's a simple example:

import readline

for s in "alpha beta gamma".split():
readline.add_history(s)

candidates = "red yellow pink blue black".split()

def completer(word, index):
matches = [c for c in candidates if c.startswith(word)]
try:
return matches[index] + " "
except IndexError:
pass
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")

while 1:
print raw_input("$ ")
Ah, thanks. It was far too simple. What I was looking for was
simply:

import readline

then replace sys.stdin.readline() with raw_input()
You may also consider using the cmd module.
I'll have to keep the cmd module in mind for other applications
(where the user is entering commands). Rather than commands,
the user of this application is entering data that changes
little (if any) from one line to the next, so raw_input() is
exactly what I needed.

--
Grant Edwards grante Yow! I'm young ... I'm
at HEALTHY ... I can HIKE
visi.com THRU CAPT GROGAN'S LUMBAR
REGIONS!
Jul 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: michele.simionato | last post by:
This a case where the documentation is lacking. The standard library documentation (http://www.python.org/dev/doc/devel/lib/module-rlcompleter.html) gives this example try: import readline...
3
by: Michael Teter | last post by:
Howdy. I looked around for an answer to this, but I was unable to find one that seemed to match my situation. (Incidentally, now I cannot get archives.postgresql.org to respond to my search...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
6
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large...
1
by: Joachim | last post by:
Is there a way to set a timeout for the Console.ReadLine method?
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
25
by: lnatz | last post by:
Hi, I am writing a shell(project). I would like to use readline, ncurses and/or history to search history. I want to write a function that would recognize the KEY_UP and KEY_DOWN,and search the...
2
by: SriBhargav | last post by:
Hi, I've a question on setting timeout on console.readline() I would like the user to input something through Console.readline() in 5 secs. If there is no input in that time, I would like to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.