interactave, readline, and insert_text

G

garett

Hello, I have been trying to make a function that will print a line of
text to the edit line of the interactive interpreter so that I can modify
the line and then hit enter. I have been using readline.insert_text().
However, the line does not show up in the edit line. After googleing
around for this I found a post that suggested:
def test():
readline.insert_text("asdf")
x = readline.get_line_buffer()
print x

the resulting output is:
test()asdf

So, the text does get inserted into the line buffer but does not display
in the edit line of the interactive interpreter.

I have also tried readline.redisplay() to the the contents of the line
buffer to display in the edit line of the interpreter. Does anybody have
any suggestions as to a technique to put text into the edit line of the
interactive interpreter from a function so it can be further edited?
Thanks a lot. -Garett
 
C

Christopher T King

I have also tried readline.redisplay() to the the contents of the line
buffer to display in the edit line of the interpreter. Does anybody have
any suggestions as to a technique to put text into the edit line of the
interactive interpreter from a function so it can be further edited?

The closest thing I can find is to use readline.add_history(). This won't
make your text pop up on the command line, but it will appear at the end
of the history buffer, ready to use.

On an unrelated note, holy crap: I did not know of the existence of
rlcompleter. Let all to whom this module is unknown type the following at
a Unix Python prompt:

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')

and rejoice! :) It just goes to show how many neat things Python does that
you can go years without ever discovering them.
 
F

Fernando Perez

Christopher said:
On an unrelated note, holy crap: I did not know of the existence of
rlcompleter. Let all to whom this module is unknown type the following at
a Unix Python prompt:

import readline
import rlcompleter
readline.parse_and_bind('tab: complete')

and rejoice! :) It just goes to show how many neat things Python does that
you can go years without ever discovering them.

<shameless plug ahead>

If you like rlcompleter, you might try out ipython, which provides that, and
another ~14000 lines of code oriented at making the interactive experience much
better than what it is in the default python shell (including a profile with
support for Numeric and Gnuplot, and a coming one for matplotlib).

http://ipython.scipy.org

Best,

f.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top