如何避免在ipython历史记录中存储命令?

And*_*rew 12 python history ipython

在bash中,我可以通过在其前面放置一个空格来阻止命令被保存到bash历史记录中.我不能在ipython中使用这个方法.我如何在ipython中做相同的操作?

Eva*_* Hu 7

我想我终于搞清楚了.这种方法并没有真正"阻止"ipython记录历史记录,但实际上删除了历史记录.但我认为这仍然是实现这一目标的好方法.

ipython历史记录存储在sqlite数据库文件中 $(ipython locate)/profile_default/history.sqlite.

数据库表历史记录有四列:session,line,sourcesource_raw.该会话列给出了session_id当前会话,它可以在IPython的方法获得: get_ipython().history_manager.hisdb.get_last_session_id()ipython.该线柱仅仅是线民.

所以我要做的是我要在环境中删除数据库中的这条记录ipython:

使用1)历史数据库对象可以得到get_ipython().history_manager.dbipython

hismgr = get_ipython().history_manager   
Run Code Online (Sandbox Code Playgroud)

2)获取会话ID:

session_id = hismgr.get_last_session_id()
Run Code Online (Sandbox Code Playgroud)

3)删除历史记录行line_id(假设这里是111)和session_id

hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(111, session_id))
Run Code Online (Sandbox Code Playgroud)

4)InOut数组列表也像历史一样,但它存储在内存中,因此可以像变量一样进行扫描或重写.

In[111]=Out[111]=''
Run Code Online (Sandbox Code Playgroud)

要"防止将命令保存到ipython历史记录",意味着删除数据库中的行历史记录,并在ipython环境中重写InOut数组.我们可以将这四条线合并为一条,为所有人做一次工作.如果我们要删除第128行,这是一个示例:

In [127]: history -l 3 -n
 124: history -l 3 -n
 125: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(123, session_id))
 126: history -l 3 -n

In [128]: passwd='123456'

In [129]: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(128, session_id));In[128]=Out[128]='';
Out[129]: <sqlite3.Cursor at 0x7f8dce3dae30>

In [130]: history -l 3 -n
 126: history -l 3 -n
 127: history -l 3 -n
 129: hismgr = get_ipython().history_manager; session_id = hismgr.get_last_session_id();hismgr.db.execute('DELETE FROM history WHERE line={0} and session={1}'.format(128, session_id));In[128]=Out[128]='';
Run Code Online (Sandbox Code Playgroud)

历史记录第128行消失了.

这是我经历过的一些文件

IPython.core.history.HistoryManager

蟒蛇-sqlite3的


iva*_*eev 5

过时的注释:这是为编写的IPython 4.0.1从v5开始,IPython不再使用readline


没有库存方式,可以添加或解决。

IPython中有两种历史记录:

  • readline历史。它可用于特殊的按键/组合键(上/下,Ctrl-R等)。仅将输入的行(用<Enter>)存储在控制台上(或也存储pyreadline在剪贴板中)。IPython依赖于Python readlineAPI的本地实现,该实现没有“不添加”功能,通常会将每一行添加到历史记录中。
    IPython提供了一种缓解这种情况的工具IPython.core.interactiveshell.ReadlineNoRecord,即上下文管理器,它可以对历史进行快照,然后将其还原。从开始ipython 4.0.1,它仅用于%run魔术,以避免将脚本交互式输入添加到历史记录中。

  • IPython的历史。它已保存在数据库和自动变量中。它包含完整的输入(对于多行输入,分别获取readline每个<Enter>'ed线)和输出。保存是在HistoryManager.store_inputs()(对于输入)和HistoryManager.store_output()(对于输出)中实现的,从中调用InteractiveShell.run_cell并由其store_history参数控制。后者反过来由TerminalInteractiveShell.interactwith 调用store_history=True

有两种方法可以解决任一层的问题:

  • 避免首先添加输入。这不能通过在命令前面加上魔术来完成,而只能通过作为单独命令运行并切换标志的魔术来完成。如您所见,这是因为在魔术命令得到控制时,当前输入已被存储。

    • readline:公共API中没有相关条目,因此它是特定于实现的。例如pyreadline,用完成添加pyreadline.modes.basemode.BaseMode.add_history()。模式对象可通过访问get_ipython().readline.rl.mode
    • 装饰run_celladd_history使用包装器检查相应对象中的标志,并使用设置/切换它们的自定义魔术命令即可解决问题
  • 执行后立即自动从历史记录中删除证据输入/输出。这可以通过前缀魔术来完成。

    • IPython:HistoryManager没有任何设施可以删除条目(既不能从数据库中也不能从变量中)。las,HistoryManager必须手动修改数据库或替换库存。还要注意,该类具有可选的缓存HistoryManager.db_cache_size(默认情况下禁用)。
    • readline:remove_history_item(index)在API中。您需要知道输入中的行数。

或者,如果仅要求输入密码,请考虑不在屏幕上回显密码的其他方式(因此不要将其作为控制台历史记录的一部分):

  • getpass.getpass()
  • 将其存储在其他位置(例如您只能读取的配置文件)