2017-02-21 130 views
1

我通过Anaconda在Mac Sierra上运行iPython(Jupyter),通过iTerm,使用$SHELL=bash - 如果我错过了任何有用的设置细节,请让我知道。Python等价于ignoreboth:erasedups

我喜欢bash$HISTCONTROL方面,提到here。总结这个答案:当遍历历史记录(又名向上箭头)时,删除重复条目会很有帮助,因此您不会多次滚动浏览同一个命令,并且这可以通过$HISTCONTROL=ignoreboth:erasedups完成。

在Python解释器(或iPython,特别是)内部是否有任何等价物?我安装了readline,觉得这是一个很好的开始,但没有什么能跳出来明显地解决问题,我会认为这是建立在某个地方。

回答

0

通过深入研究IPython,筛选解释不当和/或已弃用的文档,我将一个似乎工作正常的解决方案拼凑在一起,尽管我确定它不是最佳的,原因有很多,即:

  • 它运行history数据库上GROUP BY查询每次运行在IPython中一行
  • 不小心清理/协调数据库表的时间 - 我只修改history,却忽略output_historysessions表格

我把下面的一个文件(我把它命名为dedupe_history.py,但名字是无关紧要的)内部$HOME/.ipython/profile_default/startup

import IPython 
import IPython.core.history as H 
## spews a UserWarning about locate_profile() ... seems safe to ignore 
HISTORY = H.HistoryAccessor() 


def dedupe_history(): 
    query = ("DELETE FROM history WHERE rowid NOT IN " 
     "(SELECT MAX(rowid) FROM history GROUP BY source)") 
    db = HISTORY.db 
    db.execute(query) 
    db.commit() 


def set_pre_run_cell_event(): 
    IPython.get_ipython().events.register("pre_run_cell", dedupe_history) 

## dedupe history at start of new session - maybe that's sufficient, YMMV 
dedupe_history() 
## run dedupe history every time you run a command 
set_pre_run_cell_event()