2017-02-21 128 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()