2012-08-06 14 views
9

¿Hay alguna manera de hacer que la capacidad de registro de IPython incluya salida y entrada?¿Log de la salida de IPython?

Esto es lo que un archivo de registro se parece actualmente:

#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file 
# 12:02 
# ================================= 
print "test" 

Me gustaría tener una línea más arriba muestran:

#!/usr/bin/env python 
# 2012-08-06.py 
# IPython automatic logging file 
# 12:02 
# ================================= 
print "test" 
# test 

(el # es porque supongo que se necesita para evitar que se rompa la función logplay de IPython)

Supongo que esto es posible usando portátiles IPython, pero al menos en una máquina lo necesito, soy límite ed a ipython 0.10.2.

EDIT: Me gustaría saber cómo hacer esto de forma automática, es decir, dentro del archivo de configuración. Ahora mismo mi config parece

from time import strftime 
import os 
logfilename = strftime('ipython_log_%Y-%m-%d')+".py" 
logfilepath = "%s/%s" % (os.getcwd(),logfilename) 

file_handle = open(logfilepath,'a') 
file_handle.write('########################################################\n') 
out_str = '# Started Logging At: '+ strftime('%Y-%m-%d %H:%M:%S\n') 
file_handle.write(out_str) 
file_handle.write('########################################################\n') 
file_handle.close() 

c.TerminalInteractiveShell.logappend = logfilepath 
c.TerminalInteractiveShell.logstart = True 

pero especificando c.TerminalInteractiveShell.log_output = True parece tener ningún efecto

Respuesta

8

Hay la opción -o para %logstart:

-o: log also IPython's output. In this mode, all commands which 
    generate an Out[NN] prompt are recorded to the logfile, right after 
    their corresponding input line. The output lines are always 
    prepended with a '#[Out]# ' marker, so that the log remains valid 
    Python code. 

Adición: Si se encuentra en una sesión ipython interactivo para los que la anotación ya se ha iniciado, primero debe detener el registro y reinicie:

In [1]: %logstop 

In [2]: %logstart -o 
Activating auto-logging. Current session state plus future input saved. 
Filename  : ./ipython.py 
Mode   : backup 
Output logging : True 
Raw input log : False 
Timestamping : False 
State   : active 

Obsérvese que, después de la reanudación, "Registro de salida" es ahora "True".

+1

La solución parece estar haciendo esto en un script de inicio, como se describe aquí: http://wiki.ipython.org/Cookbook/DatedLog (esta era la solución aportada en http://stackoverflow.com/questions/11836612/where-how-in-what-context-is-ipythons-configuration-file-executed? lq = 1) – keflavich