[英]Python equivalent of ignoreboth:erasedups
我正在通過Anaconda運行iPython(Jupyter),在Mac Sierra上通過iTerm,使用$SHELL=bash
- 如果我錯過了任何有用的設置細節,請告訴我。
我喜歡這里提到的bash
的$HISTCONTROL
方面。 總結一下這個答案:當遍歷歷史記錄(也就是點擊向上箭頭)時,刪除重復的條目是有幫助的,這樣你就不會多次$HISTCONTROL=ignoreboth:erasedups
同一個命令,而這是用$HISTCONTROL=ignoreboth:erasedups
。
在Python解釋器(或iPython,特別是)中是否有相應的內容? 我已經安裝了readline
並且感覺這是一個很好的起點,但沒有任何東西突然解決問題,我會認為這是在某個地方構建的。
通過對IPython的深入研究,篩選出解讀不當和/或棄用的文檔,我拼湊了一個似乎工作正常的解決方案,盡管我確信它不是最優的,原因有很多,即:
history
數據庫上運行GROUP BY
查詢 history
,但忽略output_history
和sessions
表 我將以下內容放在$HOME/.ipython/profile_default/startup
中的文件中(我將其命名為dedupe_history.py
,但名稱無關緊要):
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.