raw_input with a pre-compiled data

Peter Otten __peter__ at web.de
Sun Jun 21 06:51:20 EDT 2009


Luca wrote:

> On Sat, Jun 20, 2009 at 6:38 PM, Chris Rebert<clp2 at rebertia.com> wrote:
>> On Sat, Jun 20, 2009 at 7:17 AM, Luca<lucafbb at gmail.com> wrote:
>>> Hi all.
>>>
>>> I need to use a function like the raw_input to read data from user
>>> command line, but I really like to pre-compile the choice and I'm not
>>> able to do this. There is some other function/module I can use?
>>> I wanna to pre-compile the raw_input input line with the current working
>>> path.
>>
>> What does "pre-compile" mean in this context? It's not clear at all,
>> so I can't even understand your question.
> 
> I'm really sorry to all that replied... In fact I literally traduced a

With "traduced" you stumbled upon another false friend ;)

http://it.wikipedia.org/wiki/Falso_amico

> concept from italian and the term precompiled was not so clear...
> 
> What I mean is this: I wanna that raw_input (or other modules) ask to
> the user the data, but some data inserted is already present, to the
> user can cancel it with the BACKSPACE key.
> 
> Examples below (the "_" is the initial cursor position).
> 
>  >>> raw_input("Insert the directory to work: ")
>  Insert the directory to work: _
> 
> What I need is:
>  >>> XXX_raw_input("Insert the directory to work: ",
>  >>> default="/home/john/")
>  Insert the directory to work: /home/john_
> 
> In the second example a user can cancel the text "/home/john".
> 
> I hope this time is more clear, sorry again.


import readline

def input_default(prompt, default):
    def startup_hook():
        readline.insert_text(default)
    readline.set_startup_hook(startup_hook)
    try:
        return raw_input(prompt)
    finally:
        readline.set_startup_hook(None)

print input_default("directory? ", default="/home/john")

The cmd module may also be worth having a look.

Peter




More information about the Python-list mailing list