Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python的IgnoreDups等价物:erasedups_Python_Readline - Fatal编程技术网

Python的IgnoreDups等价物:erasedups

Python的IgnoreDups等价物:erasedups,python,readline,Python,Readline,我正在通过Anaconda、Mac Sierra、iTerm和$SHELL=bash运行iPython(Jupyter)-如果我错过了任何有用的设置细节,请告诉我 我喜欢上面提到的bash的$HISTCONTROL特性。总结一下答案:当遍历历史记录时(也就是点击向上箭头),删除重复的条目是很有帮助的,这样您就不会多次滚动同一个命令,这是通过$HISTCONTROL=ignoreboth:erasedups实现的 Python解释器(特别是iPython)中是否有类似的东西?我已经安装了readl

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

我喜欢上面提到的
bash
$HISTCONTROL
特性。总结一下答案:当遍历历史记录时(也就是点击向上箭头),删除重复的条目是很有帮助的,这样您就不会多次滚动同一个命令,这是通过
$HISTCONTROL=ignoreboth:erasedups
实现的


Python解释器(特别是iPython)中是否有类似的东西?我已经安装了
readline
,感觉这是一个很好的开始,但是没有什么能明显地解决这个问题,我本以为这是内置的。

通过深入研究IPython,筛选解释不当和/或不推荐的文档,我拼凑了一个似乎很好用的解决方案,但我确信它不是最优的,原因有很多,即:

  • 每次我在IPython中运行一行代码时,它都会在
    历史
    数据库上运行
    GROUP BY
    查询
  • 它不负责清理/协调数据库表-我只修改
    历史记录
    ,而忽略
    输出历史记录
    会话
我将以下内容放在
$HOME/.ipython/profile\u default/startup
中的一个文件中(我将其命名为
dedupe\u 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()

我一直想要这个功能。ipython应该以本机的方式来实现这一点。您是否提出了功能请求?我没有,但请随意接管:)