代码之家  ›  专栏  ›  技术社区  ›  Boris Gorelik

在ipython历史中搜索

  •  22
  • Boris Gorelik  · 技术社区  · 15 年前

    ipython %his 命令输出用户最近输入的命令。是否可以在这些命令中搜索?像这样:

    [c for c in %history if c.startswith('plot')]
    

    编辑 我不是在寻找重新运行命令的方法,而是在历史记录列表中找到它。当然,有时我希望在定位命令后重新运行它,无论是逐字还是经过修改。

    编辑 用搜索 ctr-r 然后打字 plot 给出以“plot”开头的最新命令。它不会列出以它开头的所有命令。也不能在命令的中间或结尾搜索

    解决方案

    在这里扩展前奏曲和赋格曲的解决方案,我正在寻找:

    [l for l in  _ih if l.startswith('plot')]
    

    这里, if 条件可以用正则表达式替换

    7 回复  |  直到 6 年前
        1
  •  7
  •   Gary Kerr    13 年前

    与第一个答案类似,您可以执行以下操作:

    ''.join(_ih).split('\n')
    

    但是,在遍历命令历史记录项时,可以执行以下操作。因此,您可以从中创建列表理解。

    for item in _ih:
        print item
    

    这一点记录在文档的以下部分中: http://ipython.org/ipython-doc/dev/interactive/reference.html#input-caching-system

        2
  •  41
  •   hobs axoplasm    7 年前

    更好的是: %hist -g |pattern| 记录你过去的历史 |pattern| . 您还可以将搜索限制在当前会话或特定的行范围内。见 %hist?

    所以对于@borisgorelik的问题,你必须这样做

    %hist -g plot
    

    不幸的是你做不到

    %hist -g ^plot
    

    也不

    %hist -g "^plot"
    
        3
  •  10
  •   fmark    15 年前

    如果要在历史记录中重新运行命令,请尝试 Ctrl-r 然后是搜索字符串。

        4
  •  10
  •   hobs axoplasm    9 年前

    我通常会发现自己想在所有之前和现在的课程中搜索整个ipython历史。为此我使用:

    from IPython.core.history import HistoryAccessor
    hista = HistoryAccessor()
    z1 = hista.search('*numpy*corr*')
    z1.fetchall()
    

    (不要同时运行两者,否则会损坏/删除历史记录)

    ip = get_ipython()
    sqlite_cursor = ip.history_manager.search('*numpy*corr*')
    sqlite_cursor.fetchall()
    

    搜索字符串不是正则表达式。ipython历史记录管理器使用sqlite的 glob * 搜索语法。

        5
  •  1
  •   Alexander Artemenko    15 年前

    你可以这样做:

    ''.join(_ip.IP.shell.input_hist).split('\n')
    

    ''.join(_ip.IP.shell.input_hist_raw).split('\n')
    

    以防止magick扩展。

        6
  •  0
  •   kait    7 年前
    from IPython.core.history import HistoryAccessor
    
    
    def search_hist(pattern,
                    print_matches=True,
                    return_matches=True,
                    wildcard=True):
    
        if wildcard:
            pattern = '*' + pattern + '*'
        matches = HistoryAccessor().search(pattern).fetchall()
    
        if not print_matches:
            return matches
    
        for i in matches:
            print('#' * 60)
            print(i[-1])
    
        if return_matches:
            return matches
    
        7
  •  0
  •   Juan Miguel Díaz Pérez    6 年前
    %history [-n] [-o] [-p] [-t] [-f FILENAME] [-g [PATTERN [PATTERN ...]]]
             [-l [LIMIT]] [-u]
             [range [range ...]]
    

    -g <[PATTERN [PATTERN …]]>
    treat the arg as a glob pattern to search for in (full) history. This includes the saved history (almost all commands ever written). The pattern may contain ‘?’ to match one unknown character and ‘*’ to match any number of unknown characters. Use ‘%hist -g’ to show full saved history (may be very long).
    

    示例(在我的历史中):

    In [23]: hist -g cliente*aza
    655/58: cliente.test.alguna.update({"orden" : 1, "nuevo" : "azafran"})
    655/59: cliente.test.alguna.update({"orden" : 1} , {$set : "nuevo" : "azafran"})
    655/60: cliente.test.alguna.update({"orden" : 1} , {$set : {"nuevo" : "azafran"}})
    

    示例(在我的历史中):

    In [24]: hist -g ?lie*aza
    655/58: cliente.test.alguna.update({"orden" : 1, "nuevo" : "azafran"})
    655/59: cliente.test.alguna.update({"orden" : 1} , {$set : "nuevo" : "azafran"})
    655/60: cliente.test.alguna.update({"orden" : 1} , {$set : {"nuevo" : "azafran"}})