【问题标题】:Tab completion in ipython for list elements列表元素的 ipython 中的制表符完成
【发布时间】:2015-09-30 12:56:24
【问题描述】:

这是标签完成对我的工作方式:

In [84]: a="string"

In [85]: b = ["str", "ing"]

字符串的制表符补全在这里工作:

In [86]: a.
a.capitalize  a.decode      a.expandtabs  a.index       a.isdigit     a.istitle     a.ljust       a.partition   a.rindex      a.rsplit      a.splitlines  a.swapcase    a.upper       
a.center      a.encode      a.find        a.isalnum     a.islower     a.isupper     a.lower       a.replace     a.rjust       a.rstrip      a.startswith  a.title       a.zfill       
a.count       a.endswith    a.format      a.isalpha     a.isspace     a.join        a.lstrip      a.rfind       a.rpartition  a.split       a.strip       a.translate   

列表的制表符补全在这里工作:

In [86]: b.
b.append   b.count    b.extend   b.index    b.insert   b.pop      b.remove   b.reverse  b.sort     

字符串的制表符补全在这里不起作用:

In [87]: b[0].

一种可能的解决方法:

In [88]: c = b[0]

In [89]: c.
c.capitalize  c.decode      c.expandtabs  c.index       c.isdigit     c.istitle     c.ljust       c.partition   c.rindex      c.rsplit      c.splitlines  c.swapcase    c.upper       
c.center      c.encode      c.find        c.isalnum     c.islower     c.isupper     c.lower       c.replace     c.rjust       c.rstrip      c.startswith  c.title       c.zfill       
c.count       c.endswith    c.format      c.isalpha     c.isspace     c.join        c.lstrip      c.rfind       c.rpartition  c.split       c.strip       c.translate   

是否可以在没有提及解决方法的情况下使用完成?我在 ipdb 中遇到了类似的行为,是否也可以在那里修复这种行为?我正在使用 ipythoon v3.1.0 和 ipdb v 0.8。谢谢

【问题讨论】:

    标签: python autocomplete ipython jupyter ipdb


    【解决方案1】:

    创建 ipython 配置文件:

    ipython profile create testing
    

    ipython_config.py 取消注释这一行

    # Activate greedy completion
    # 
    # This will enable completion on elements of lists, results of function calls,
    # etc., but can be unsafe because the code is actually evaluated on TAB.
    c.IPCompleter.greedy = True
    

    使用此配置文件加载 IPython:

    ipython notebook --profile=testing
    

    这为列表成员和字典键和值提供了 TAB 完成。


    一种快速的替代方法是使用 dir() 方法:

    dir(b[0])
    
    #returns:
    
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    

    另一种方法是在 PTVS 中使用 Python 或 IPython 交互式控制台或常规编辑器,它能够对列表元素进行补全(智能感知)。

    【讨论】:

    • 是的,但这是另一种解决方法。主要目标是使用制表键。像 bash 的 /etc/bash_completion 这样的配置就可以了
    • ipython 自动补全被考虑使用绝地重构:github.com/ipython/ipython/issues/8606
    • 谢谢你,这工作:) 注意,ipython 必须以ipython --profile=testing 启动。三个问题 1. “(I)Python 控制台”是什么意思 2. 是否可以在 ipdb 中启用此功能,我想更改默认配置文件可能是这样? 3. 你是怎么想到的?
    • 我已将profile_testing 重命名为profile_default,当我开始ipython 时,列表上的完成是有效的,但是当我开始python -mipdb script.py 时它不是,有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2014-01-10
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多