gpt4 book ai didi

Python 相当于 ignoreboth :erasedups

转载 作者:行者123 更新时间:2023-12-04 17:53:32 26 4
gpt4 key购买 nike

我正在通过 Anaconda、Mac Sierra、iTerm 和 $SHELL=bash 运行 iPython (Jupyter) - 如果我错过了任何有用的设置细节,请告诉我.

我喜欢 bash$HISTCONTROL 方面,提到了 here .总结一下这个答案:当遍历历史(也就是点击向上箭头)时,删除重复条目很有帮助,这样您就不会多次滚动相同的命令,这是通过 $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()

关于Python 相当于 ignoreboth :erasedups,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42371085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com