如何记录IPython的输出,而不是在每次加载时都有丑陋的7行logging-info?

sen*_*nte 7 python installation logging ipython dotfiles

我的目标是调用ipython,同时还将所有输入/输出记录到IPython,并看到如下内容:

stu@stumac ~  $ ipython

In [1]: exit
stu@stumac ~  $
Run Code Online (Sandbox Code Playgroud)

如果我设置,可以轻松删除横幅

c.TerminalIPythonApp.display_banner = False

在我的 ~/.ipython/profile-default/ipython_config.py档案中.

但是,如何在记录事物的同时获得初创公司的清洁?

在全新安装中,如果我在没有参数的情况下启动IPython,我会看到:

sente@og ~ $ ipython
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $
Run Code Online (Sandbox Code Playgroud)

如果我logfile=logfile.txt在调用IPython时传递参数,我会看到:

sente@og ~ $ ipython --logfile=logfile.txt
Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
Python 2.7.3 (default, Jun 20 2013, 12:50:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: exit
sente@og ~ $
Run Code Online (Sandbox Code Playgroud)

如何在不向终端添加额外混乱的情况下使用日志记录:

Activating auto-logging. Current session state plus future input saved.
Filename       : logfile.txt
Mode           : backup
Output logging : False
Raw input log  : False
Timestamping   : False
State          : active
Run Code Online (Sandbox Code Playgroud)

在其他机器上,我将IPython配置为通过.ipython/profile_default/startup/01-log-everything.py包含以下行来自动记录事物:

from time import strftime
import os.path

ip = get_ipython()

ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)

ip.run_line_magic('logstart', '-o %s append' % filename)
Run Code Online (Sandbox Code Playgroud)

这导致了与我添加时相同的混乱 --logfile=logfile.txt

任何有关如何正确执行此操作的帮助将不胜感激.我可以,如果没有别的重定向sys.stdout,配置日志,然后重置sys.stdout但我希望有一个不那么hackish解决方案.

sen*_*nte 4

查看IPython源码后,即:

似乎真正实现我想要的目标(在启动时回显任何内容的完整日志记录sys.stdout)的唯一方法是在初始化日志记录时重定向或静音。

我创建了一个启动文件:~/.ipython/profile_default/startup/01-log-everything.py执行此操作,代码是:

from time import strftime
import os.path
import contextlib
import cStringIO
import sys


# create a context which we can use for any block which we can use for any
# block which we do not want to print stdout
# -- taken from http://stackoverflow.com/a/2829036/217652

@contextlib.contextmanager
def nostdout():
    save_stdout = sys.stdout
    sys.stdout = cStringIO.StringIO()
    yield
    sys.stdout = save_stdout



ip = get_ipython()

ldir = ip.profile_dir.log_dir
fname = strftime('%Y-%m-%d-%H-%M-%S') + ".py"
filename = os.path.join(ldir, fname)


# stdout is now muted
with nostdout():
    ip.run_line_magic('logstart', '-o %s append' % filename)
Run Code Online (Sandbox Code Playgroud)