Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ipython autocompletion for list or dict of objects

I would like to have autocompletion in IPython (Jupyter qtconsole or console) for the following case:

I create a class

class MyClass(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b

and put several objects of this class into a list or dict

my_list = []
my_list.append(MyClass(2,3))
my_list.append(MyClass(9,2))
my_list.append(MyClass(8,4))

Now if I do

my_list[0].TAB

autocompletion is not working.

I would like to see a list of my class attributes and methods. Am I missing something or is this just not support in IPython?

Appreciate your help ...

like image 673
fred Avatar asked Nov 03 '15 11:11

fred


People also ask

What is the IPython code autocompletion feature?

The IPython Code Autocompletion feature (and other Magics) helps developers implement the code logic and navigate through the libraries/APIs in the same experience as the traditional IDEs. There is some new development project that tries to merge the benefits/features of the traditional IDEs and Jupyter Notebook like Netflix's Polynote.

How can I customise key completions for objects in IPython?

You can also customise key completions for your objects, e.g. pressing tab after obj ["a. To do so, define a method _ipython_key_completions_ (), which returns a list of objects which are possible keys in a subscript expression obj [key]. IPython can display richer representations of objects.

Is it possible to create your own Dict in Python?

Custom (ie. not built-in) objects in Python by default have a magic __dict__ attribute that holds all per-instance attributes of the object. But there’s no reason why we can’t supply our own dict instead!

How do I get a list of possible keys in IPython?

To do so, define a method _ipython_key_completions_ (), which returns a list of objects which are possible keys in a subscript expression obj [key]. IPython can display richer representations of objects.


1 Answers

You can execute that in a cell of your Jupyter Notebook:

%config IPCompleter.greedy=True

Which gives (in an ipython/jupyter console, but same in a notebook)

In [10]: my_list[0].<TAB>
my_list[0].a  my_list[0].b  

To have it permanently, just edit your file ipython_config.py so it looks like this (Commented lines are already present and unmodified, around lines 506-514):

#------------------------------------------------------------------------------
# Completer configuration
#------------------------------------------------------------------------------

# 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.Completer.greedy = True # <-- uncomment this line and set it to True

If you don't have ipython_config.py in ~/.ipython/profile_default/ you can create one with:

ipython profile create
like image 171
jrjc Avatar answered Sep 20 '22 13:09

jrjc