8

I understand I can run a script in IPython via run test.py and debug from there.

But how do I pipe an output into test.py? For example, normally I could run in the command line like grep "ABC" input.txt | ./test.py, but how do I do the same thing in IPython?

Thanks!

CuriousMind
  • 15,168
  • 20
  • 82
  • 120
  • Not familiar with IPython. Any reason you can't use sys.stdin or the fileinput module? – Kenan Banks May 19 '12 at 02:29
  • @Triptych I want to debug with that particular input, generated by grep. – CuriousMind May 19 '12 at 02:31
  • Try using the [fileinput module](http://docs.python.org/library/fileinput.html) and see how it goes. But to really answer this question: yes, you can do it but it depends on how the python is written. – Keith May 19 '12 at 02:35
  • Do you mean you want to simulate user input to ipython? – Keith May 19 '12 at 02:35

1 Answers1

7

Inside the Python script you should be reading from sys.stdin:

import sys

INPUT = sys.stdin

def do_something_with_data(line):
    # Do your magic here
    ...
    return result

def main():
    for line in INPUT:
        print 'Result:', do_something_with_data(line)

if __name__ == '__main__':
    main()

Inside the iterative interpreter you can use the subprocess module mock sys.stdin.

In[0]: from test.py import *
In[1]: INPUT = subprocess.Popen(['grep', 'ABC', 'input.txt'], \
                               stdout=subprocess.PIPE).stdout
In[2]: main()

You can also pipe the output to a file and just read from the file. For practical purposes stdin is just another file.

In[0]: ! grep "ABC" input.txt > output.txt
In[1]: INPUT = open('output.txt')
In[2]: main()
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153