簡體   English   中英

Python相當於ignoreboth:erasedups

[英]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的深入研究,篩選出解讀不當和/或棄用的文檔,我拼湊了一個似乎工作正常的解決方案,盡管我確信它不是最優的,原因有很多,即:

  • 每次我在IPython中運行一行時,它都會在history數據庫上運行GROUP BY查詢
  • 它沒有注意清理/協調數據庫表 - 我只修改history ,但忽略output_historysessions

我將以下內容放在$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM