How to log everything that occurs in a Python interactive shell session?

I've only tested this in python2.7. I don't have 3 handy import code import sys class Tee(object): def __init__(self, log_fname, mode='a'): self. Log = open(log_fname, mode) def __del__(self): # Restore sin, so, se sys. Stdout = sys.

__stdout__ sys. Stdir = sys. __stdin__ sys.

Stderr = sys. __stderr__ self.log.close() def write(self, data): self.log. Write(data) sys.

__stdout__. Write(data) def readline(self): s = sys. __stdin__.readline() self.log.

Write(s) return s # Tie the ins and outs to Tee. Sys. Stdout = sys.

Stderr = sys. Stdin = Tee('consolelog. Dat', 'w') console = code.

InteractiveConsole() console.interact().

I've only tested this in python2.7. I don't have 3 handy. Import code import sys class Tee(object): def __init__(self, log_fname, mode='a'): self. Log = open(log_fname, mode) def __del__(self): # Restore sin, so, se sys.

Stdout = sys. __stdout__ sys. Stdir = sys.

__stdin__ sys. Stderr = sys. __stderr__ self.log.close() def write(self, data): self.log.

Write(data) sys. __stdout__. Write(data) def readline(self): s = sys.

__stdin__.readline() self.log. Write(s) return s # Tie the ins and outs to Tee.Sys. Stdout = sys.

Stderr = sys. Stdin = Tee('consolelog. Dat', 'w') console = code.

InteractiveConsole() console.interact().

This works very well. Do you have any idea how I can have it log in real time? I.e.

Not wait until the session has closed to write? I read somewhere that a call to sys.stdout.flush(), etc, would do it, but I'm a little unsure of how to implement. Thank you for your help so far!

– Zachary Allaun Nov 8 at 18:10 Figured it out. You need calls to flush() both for the log file, and for the stdout/in objects. Thanks for pushing me in the right direction!

– Zachary Allaun Nov 8 at 18:21 Glad it helped you. You should only need to flush the log. There is no reason to flush stdout/stderr.

Remember, the log is the only thing actually writing to the file, so flushing it should be enough. – jaime Nov 8 at 18:39 I thought that as well, but (for some reason that I do not understand) the prompt in Py 3.2 does not print correctly unless I flush the std objects. I could not replicate the issue on 2.7.That's what I get for using 3, I guess :).

– Zachary Allaun Nov 8 at 19:00.

Take a look at IPython (haven't used it myself). Here's a section in the docs that might be of particular interest: ipython.org/ipython-doc/dev/interactive/....

Ah, I meant to mention (but forgot) that I'd prefer to use the standard Python interpreter. I may end up falling back on iPython, but I'd like to try to accomplish this without first. – Zachary Allaun Nov 8 at 16:59.

See this Virtualenv article by Doug Hellmann, showing how to log an iPython session: If you are comfortable working at the interactive prompt in this way, but want to record what you do for future reference after you close your session, you can use IPython’s logging feature to write the session to a file. To activate the log, use the control command %logstart, as illustrated in Listing 5. The output file is a Python source file, so it is easy to clean it up and turn it into a “real” module when you are done experimenting.In 6: %logstart Activating auto-logging.

Current session state plus future input saved. Filename : ipython_log. Py Mode : rotate Output logging : False Raw input log : False Timestamping : False State : active In 7: a = 5 In 8: be = 6 In 9: c = a * be In 10: c Out10: 30 In 11: d = a, b, c In 12: d Out12: 5, 6, 30 In 13: %logstop.

Thanks for the reference! I'd like to make this work in the standard interpreter, though. My goal here is to create something of an interactive tutorial for Python basics (and to teach myself along the way), and I'd like it to be portable for the benefit of friends who may use it.

– Zachary Allaun Nov 8 at 17:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions