Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter to raw_input automatically

Tags:

python

Just as an example, it might seem illogical. I have a get_name function as below, and wanted to write a automated script to call this function and enter to the raw_input automatically.

def get_name ():
    name = raw_input("Please enter your name : ")
    print "Hi " + name

The automated script as below, what command should I add to enter my value automatically?

def run ():
    get_name ()
    // what should I add here?
like image 634
user3431399 Avatar asked Jul 06 '15 06:07

user3431399


People also ask

How do you input automatically in Python?

The input() function automatically converts the user input into string. We need to explicitly convert the input using the type casting. raw_input() - The raw_input function is used in Python's older version like Python 2.

How do you press Enter to continue in Python?

In Python 3 use input(): input("Press Enter to continue...") In Python 2 use raw_input(): raw_input("Press Enter to continue...")

What is the difference between input () and raw_input ()?

There are two functions that can be used to read data or input from the user in python: raw_input() and input(). The results can be stored into a variable. raw_input() – It reads the input or command and returns a string. input() – Reads the input and returns a python type like list, tuple, int, etc.

What does raw_input () do in Python?

The raw_input() function which is available in python 2 is used to take the input entered by the user. The raw_input() function explicitly converts the entered data into a string and returns the value. The raw_input() can be used only in python 2. x version.


4 Answers

For testing you could call your script from the command line with IO redirection - see subprocess in the manuals but for a quick solution you could change your code like this, note that this does not test raw_input but lets you simply test the surrounding code:

def get_name (name=''):
    """ Code to get the name for testing supply name int the call """
    if len(name) == 0:
        name = raw_input("Please enter your name : ")
    print "Hi " + name

def run ():
    get_name ("Fred")
like image 22
Steve Barnes Avatar answered Nov 11 '22 17:11

Steve Barnes


You can redirect your stdin to a file , and then raw_input() would read from that file.

Example -

def run():
    import sys
    f1 = sys.stdin
    f = open('input.txt','r')
    sys.stdin = f
    get_name()
    f.close()
    sys.stdin = f1

Please note, after you do - f = open('input.txt','r') and sys.stdin = f , raw_input() would read from the <filename> file.

Once you are done with the get_name() call, close the file and restore the stdin using sys.stdin = sys.__stdin__ , if you want to restore it back to console input, otherise you can restore it to f1 , which would restore it to the state it was before test started.

Please note, you should be careful when redirecting inputs like this.

like image 29
Anand S Kumar Avatar answered Nov 11 '22 17:11

Anand S Kumar


You can also substitute stdin with StringIO (aka memory file) instead of real file. This way the entered text will be in your testing code instead of separate text file.

based on Anand S Kumar's (+1):

def run():
    import sys
    import StringIO
    f1 = sys.stdin
    f = StringIO.StringIO('entered text') # <-- HERE
    sys.stdin = f
    get_name()
    f.close()
    sys.stdin = f1

Also, for more sophisticated testing of interactive commandline functions/tools you may want to check the pyexpect package.

like image 91
Michał Šrajer Avatar answered Nov 11 '22 17:11

Michał Šrajer


Another option is to make the input function a parameter, defaulting to raw_input:

def get_name(infunc=raw_input):
    name = infunc("Please enter your name : ")
    print "Hi " + name

Then for testing purposes you can pass in a function that does whatever you need:

get_name(lambda prompt: "John Smith")
like image 3
jonrsharpe Avatar answered Nov 11 '22 17:11

jonrsharpe