Tab completion in ipython for list elements
Asked Answered
S

1

8

Here is how tab completion is working for me:

In [84]: a="string"

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

Tab completion for strings is working here:

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   

Tab completion for lists is working here:

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

Tab completion for strings is not working here:

In [87]: b[0].

One possible workaround:

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   

Is it possible to use completion without mentioned workaround? I'm experiencing similar behavior in ipdb, is it possible to fix this behavior also there? I'm using ipythoon v3.1.0 and ipdb v 0.8. Thanks

Stgermain answered 12/7, 2015 at 1:11 Comment(0)
M
5

create ipython profile:

ipython profile create testing

in ipython_config.py un-comment this line

# 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

Load IPython with this profile:

ipython notebook --profile=testing

This gives TAB completion for list members and dictionary keys and values.


One quick alternative is to use dir() method:

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']

Another method is to use Python or IPython interactive console or regular editor in PTVS, which is able to do completions (intellisense) on list elements.

Maria answered 12/7, 2015 at 7:12 Comment(4)
Yes, but this is another kind of workaround. Primary goal is to use tab key. Some configuration like bash's /etc/bash_completion would be fineStgermain
ipython autocompletion is considered for refactoring using jedi: github.com/ipython/ipython/issues/8606Maria
Thank you, this worked :) One note, ipython must be started as ipython --profile=testing. Three questions 1. What did you mean by "(I)Python console" 2. Is it possible to enable this also in ipdb, I suppose changing the default profile might be the way ? 3. How did you figure this out?Stgermain
I've renamed profile_testing to profile_default now when I start ipython completion on lists is working, but when I start python -mipdb script.py it is not, any idea?Stgermain

© 2022 - 2024 — McMap. All rights reserved.