Python等价于ignoreboth:删除重复

2024-09-30 16:20:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行iPython(Jupyter)通过Anaconda,在macsierra上,通过iTerm,使用$SHELL=bash-如果我错过了任何有用的设置细节,请告诉我。你知道吗

我喜欢上面提到的$HISTCONTROL方面。总结一下这个答案:当遍历历史时(即点击向上箭头),删除重复的条目是很有帮助的,这样您就不会多次滚动同一个命令,这是通过$HISTCONTROL=ignoreboth:erasedups完成的。你知道吗

在Python解释器(特别是iPython)中是否有类似的方法?我已经安装了readline,感觉这是一个很好的开始,但是没有什么能解决这个问题,我会认为这是在某个地方建立的。你知道吗


Tags: 答案命令bashipythonjupyter条目anaconda箭头
1条回答
网友
1楼 · 发布于 2024-09-30 16:20:16

通过深入研究IPython,筛选解释不充分和/或不推荐使用的文档,我拼凑出了一个似乎可以很好地工作的解决方案,尽管我确信它不是最佳的,原因有很多,即:

  • 每次我在IPython中运行一行时,它都会对GROUP BY数据库运行一个history查询
  • 它不负责清理/协调数据库表—我只修改history,而忽略output_historysessions

我将以下内容放在dedupe_history.py内的一个文件中(我将其命名为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()

相关问题 更多 >