通过深入研究IPython,筛选解释不当和/或已弃用的文档,我将一个似乎工作正常的解决方案拼凑在一起,尽管我确定它不是最佳的,原因有很多,即:
- 它运行
history
数据库上GROUP BY
查询每次运行在IPython中一行
- 不小心清理/协调数据库表的时间 - 我只修改
history
,却忽略output_history
和sessions
表格
我把下面的一个文件(我把它命名为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()