6

How can I simulate a python interactive session using input from a file and save what would be a transcript? In other words, if I have a file sample.py:

#
# this is a python script
#
def foo(x,y):
   return x+y

a=1
b=2

c=foo(a,b)

c

I want to get sample.py.out that looks like this (python banner omitted):

>>> #
... # this is a python script
... #
... def foo(x,y):
...    return x+y
... 
>>> a=1
>>> b=2
>>> 
>>> c=foo(a,b)
>>> 
>>> c
3
>>> 

I've tried feeding stdin to python, twitter's suggestions were 'bash script' with no details (played with the script command in bash, no joy). I feel it should be easy, and I'm missing something simple. Do I need to write a parser using exec or something?

Python or ipython solutions would be fine. And I might then want to convert to html and syntax highlight this in a web browser, but that's another problem....

10
  • Are you trying to save your interactive python shells's session to a file or emulate the python shell/console? Commented May 22, 2014 at 14:15
  • @JamesMills I think OP wants exactly opposite, a script to be converted to look like from interactive shell. Commented May 22, 2014 at 14:16
  • pythonhosted.org/sphinxcontrib-programoutput <-- This will do something like that. Commented May 22, 2014 at 14:17
  • yeah, slight clarification added. get input from a file, make it look like it was typed. For my example I actually cut n paste into a running python. Commented May 22, 2014 at 14:20
  • @JamesMills are you sure? As far as I can tell, that extension just runs an external command and inserts the complete output (STDOUT) into the docs. I fail to see how that would work to emulate an interactive interpreter reading and evaluating the source line by line. Commented May 22, 2014 at 14:23

1 Answer 1

8

I think code.interact would work:

from __future__ import print_function
import code
import fileinput


def show(input):
    lines = iter(input)

    def readline(prompt):
        try:
            command = next(lines).rstrip('\n')
        except StopIteration:
            raise EOFError()
        print(prompt, command, sep='')
        return command

    code.interact(readfunc=readline)


if __name__=="__main__":
    show(fileinput.input())

(I updated the code to use fileinput so that it reads from either stdin or sys.argv and made it run under python 2 and 3.)

Sign up to request clarification or add additional context in comments.

3 Comments

Does what it says on the tin. I'm having trouble breaking it :)
how do I use this? is it from within the python interpreter or bash? do I enter >>> show(sample.py)
@Tim save the code as interact.py and the input as sample.py, then do python interact.py sample.py. It will work in py3 when you use the new syntax for print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.