3

I use a function that gets some input from the user interactively and returns some values. I'd like to run the function (without changing it) without having to type in the values myself, e.g. have it read from a list. How can I achieve this?

MCVE:

def add():
    a = int(input('first number: '))
    b = int(input('first number: '))
    return(a+b)

And I want to be able to run something like magic(add(), [2, 3]) to get the output 5.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
kmi
  • 33
  • 2

3 Answers3

8

Assuming you are on Linux or wsl, you can just pipe some input through stdin. I.e.,

$ echo $'1\n3\n' | python scripy.py

should produce 4. If you have a file with the same contents, you can do the same using cat

$ cat file.in | python script.py

only if absolutely necessary:

python can also be used to simulate stdin. sys.stdin is simply an instance of the StringIO class. We can override the default sys.stdin with a StringIO class with contents of our choice.

with StringIO(in_string) as f:
    tmp = sys.stdin
    sys.stdin = f
    add()
    sys.stdin = tmp
xzkxyz
  • 527
  • 3
  • 4
1

One option is to make a test where you mock out input:

from unittest.mock import patch

@patch("builtins.input")
def test_add(mock_input):
    input_values = [2, 3]
    mock_input.side_effect = input_values
    expected_value = sum(input_values)
    actual_value = add()
    # print or log actual value here if you want to see the result
    assert actual_value == expected_value

this is quite a round about way if you are trying to automate the use of some third party function that annoyingly uses input, but if you're just trying to test their function, its not bad.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • This is very close to what I need but it complains on the `@patch` line: `Need a valid target to patch. You supplied: 'input'` – kmi Jul 31 '20 at 00:04
  • 1
    Use “builtins.input”, that will patch what you want. It might be “builtin.input”. I can never remember. – SethMMorton Jul 31 '20 at 00:07
  • Hmmm, try `@patch("builtins.input")` instead? https://stackoverflow.com/a/37449365/1011724 note btw the easiest way to run this test is via pytest – Dan Jul 31 '20 at 00:09
  • 1
    Thanks a bunch. That did the trick. This works perfectly. – kmi Jul 31 '20 at 00:10
-2

If you have your values in a list of any size, then sum will work.

def add(A):
    return sum(A)
JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24