Dump interpreter history?

K

Ken D'Ambrosio

Hey, all. A co-worker asked me a question, and I've got no idea how (or
if) it can be done. Bottom line: he'd like to save off the text from an
interpreter session, his thinking being that you've already tried to get
what you want, and now you just need to gussy it up in an editor.

Can this be done?

Thanks!

-Ken
 
J

John Gordon

In said:
Hey, all. A co-worker asked me a question, and I've got no idea how (or
if) it can be done. Bottom line: he'd like to save off the text from an
interpreter session, his thinking being that you've already tried to get
what you want, and now you just need to gussy it up in an editor.

If you're running on a unix system, run the "script" command before
starting the interpreter. Then when you're finished with the interpreter,
type "exit" to end the script session. This will create a file named
"typescript" containing all of the input and output which occurred.
 
T

Tim Chase

Hey, all. A co-worker asked me a question, and I've got no idea how (or
if) it can be done. Bottom line: he'd like to save off the text from an
interpreter session, his thinking being that you've already tried to get
what you want, and now you just need to gussy it up in an editor.

import readline
readline.write_history_file([filename])

Just to clarify (I thought Daniel's answer was so easy it must
have misinterpreted the OP's request), that's a single string as
a filename, not a list containing a filename. I tried

filename = 'output.txt'
import readline
readline.write_history_file([filename])

and got a traceback about the expected parameter type. For
clarity, it should have been

readline.write_history_file(filename)

or

readline.write_history_file('output.txt')

But otherwise, Daniel's given a dead-easy solution.

-tkc
 
A

Aahz

Hey, all. A co-worker asked me a question, and I've got no idea how (or
if) it can be done. Bottom line: he'd like to save off the text from an
interpreter session, his thinking being that you've already tried to get
what you want, and now you just need to gussy it up in an editor.

I've never used it myself, but IIRC ipython does what you want very
nicely.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22
 
R

rusi

script
readline
ipython

all nice solutions...
There's one more (old) one: emacs

ie you can run python inside (under) emacs
That way you can pun thus: your interactions with python are a session
when you choose and a file when you choose (buffer in emacs-speak).

[Frank admission: The emacs python modes are multiple and in a (bit of
a) mess]
 
T

Terry Reedy

I've never used it myself, but IIRC ipython does what you want very
nicely.

Idle will save the contents of the shell window, including opening slash
line and prompts.
 
J

John Gordon

In said:
import readline
readline.write_history_file([filename])
Just to clarify (I thought Daniel's answer was so easy it must
have misinterpreted the OP's request), that's a single string as
a filename, not a list containing a filename. I tried

In most documentation, square brackets indicate that the enclosed item is
optional; the brackets are not meant to be used literally.

Therefore, this text:

readline.write_history_file([filename])

says "You can call write_history_file() with no arguments at all, or
with one argument which is the name of the file to be written."
 
A

Aahz

Idle will save the contents of the shell window, including opening slash
line and prompts.

The problem is that normally you *don't* want the prompts. I believe
IPython handles that.
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top