![]() | Ned Batchelder : Blog | Code | Text | Site Caged python » Home : Blog : July 2011 |
Caged pythonSunday 10 July 2011 For a presentation, I wanted to produce samples of Python interactive sessions. I could have opened a terminal window and typed my input, and copied the resulting session and pasted it into a text file, but that's not repeatable, and is labor intensive and error-prone. I looked for ways people had done this in the past, and didn't find the thing that I'm sure is out there, but it's fun to do it yourself anyway. The code module in the standard library provides most of the heavy lifting, but there's a little input and output grabbing and tweaking to be done. Here's what I ended up with: """A python prompt in a cage, for producing prompt sessions.""" Running it produces: $ python cagedprompt.py There's a few small ways the output differs from a real interactive session: the initial banner is shorter, and a blank line in the input will produce a true blank line in the output. These make the output nicer to use for presentations. Now I can use the prompt_session function to get the textual output of a Python prompt fed with a particular input. Nice.
tagged:
python» 6 reactions | |
Comments
Not sure if this helps, but if you are on a Linux command line, you can also use the script command. This basically logs all information from stdout and stderr to a file.
Thanks @farzadb82, I hadn't heard of the script command. But that still leaves me entering input by hand, and redoing it by hand if the input needs to be different.
How about using Crunchy? [ Or perhaps Reinteract (?) - I have only seen a demo of it.]
I believe player piano may qualify as the "thing you're sure is out there": http://blog.wearpants.org/playerpiano-amaze-your-friends/
It's actually designed so you can bang away at the keyboard as if you were actually typing at the interactive prompt, but I expect you could extract the recorded doctests in other ways easily enough.
The trouble with Python is that it's often far simpler and more fun to invent your own solution than it is to use someone else's.
See also - http://discorporate.us/projects/sliderepl/sliderepl.py. It's very useful for demo'ing code while minimizing typos in front of the audience.
Add a comment: