0

I'm using a class from a module that on first usage asks me for some input in the terminal.

At every new instance the terminal asks for some inputs.

Example:

instance = Class()
instance.run()
## asks for input in the terminal

I thought about the subprocess module but didn't find anything regarding my use case, the best solution should allow me to read what it's asked and input some data at each step.

Thanks in advance

  • Can you share what class and module you are using? Using the subprocess module, you can set a pipe to stdin, which may help. However, depending on the class/module, there may be an easier way with Python. – Cody Schindler Nov 12 '19 at 23:39

2 Answers2

0

Here's an example from a fun guide I found on google just now.

I recommend you search for a few different articles on this topic to learn about the different approaches that might help you in your particular situation. Happy coding!

from __future__ import print_function, unicode_literals
from PyInquirer import prompt
from pprint import pprint
questions = [
    {
        'type': 'input',
        'name': 'first_name',
        'message': 'What\'s your first name',
     }
]
answers = prompt(questions)
pprint(answers)
)
Eddie Knight
  • 102
  • 6
0

Your problem is actually answered pretty well here: Write function result to stdin

I've included an option below, but in general this is a pretty messy situation to do, and I would look for alternate solutions. Even monkeypatching the other library to accept input as variables is likely to be cleaner.

That being said, the translation for your purposes might look like this:

import sys
import StringIO

class Foo(object):
    def run(self):
        self.x = raw_input('Enter x: ')

old_stdin = sys.stdin
instance = Foo()
sys.stdin = StringIO.StringIO('asdlkj')
instance.run()
print
print 'Got: %s' % instance.x

and running:

Enter x: 
Got: asdlkj
Cireo
  • 4,197
  • 1
  • 19
  • 24