[英]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.