Jump to content
PierreC

Get the propt value of input()

Recommended Posts

Hello everyone,

 

I have another question: is there a way to get the prompt argument of the python input() function?

For example if the python code is 

input("Please, give me a number")

I would like to get this string when dealing with the OnReceiveData or OnReceiveUniData events.

Share this post


Link to post

You cant't, at least  not easily.

 

The input function writes to stdout the message and then reads from stdin the input. The two operations are separate.  You may be able to guess from what was written to sys.stdout last.

But what you can do is overwrite builtins.input with your own function.

Edited by pyscripter

Share this post


Link to post
On 12/14/2021 at 3:49 PM, pyscripter said:

But what you can do is overwrite builtins.input with your own function.

How to override builtins.input please?

Share this post


Link to post

Run a script like this:

 

import builtins

def myinput(prompt):
    builtins._input_prompt = prompt
    builtins._input(prompt)

builtins._input = builtins.input
builtins.input = myinput

Then the prompt will be stored in builtins._input_prompt

Edited by pyscripter

Share this post


Link to post

Thanks a lot, the text is passed, but the input value is always NoneType.

import builtins

def myinput(prompt):
    builtins._input_prompt = prompt
    builtins._input(prompt)

builtins._input = builtins.input
builtins.input = myinput

nadr = int(input('Enter device address (0 - 239, 255): '))
print(f'Node {nadr} is not bonded')
procedure TForm1.PythonInputOutput1ReceiveUniData(Sender: TObject;
  var Data: string);
var
  s: string;
begin
  InputQuery( 'Query from Python', MainModule.builtins._input_prompt, s);
  Data := AnsiString(s);
end;
Enter device address (0 - 239, 255): 
Traceback (most recent call last):

  File "<string>", line 11, in <module>

TypeError
: 
int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

 

image.png

Share this post


Link to post

I will answer myself, this is functional

import builtins

def myinput(prompt):
    builtins._input_prompt = prompt
    return builtins._input(prompt)

builtins._input = builtins.input
builtins.input = myinput

Thanks again, I'm learning Python and I'm still amazed at what it can do.

  • Thanks 1

Share this post


Link to post

I wrote the code without testing, just to give you the idea.  Good that you solved the problem.

Also instead of MainModule.builtins._input_prompt  you can use BuiltinModule._input_prompt.

 

And the following would do.  No need to convert to AnsiString and back.

procedure TForm1.PythonInputOutput1ReceiveUniData(Sender: TObject;
  var Data: string);
begin
  InputQuery( 'Query from Python', BuiltinModule._input_prompt, Data);
end;

 

Edited by pyscripter
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×