[IPython-user] completion

Fernando Perez Fernando.Perez at colorado.edu
Tue Feb 22 16:08:21 CST 2005

John Hunter wrote:
> I am using the insight toolkit, which is a C++ library with a python
> wrapper.  I find that for certain instances, class methods are
> available by dir but nothing shows up on tab completion.  My get is
> that there is some getattr magic under the hood that explains this...
> Is there any way to extend ipython's completion namespace so that it
> incorporates the information from dir?  Where does ipython get the
> completion namespace from 
>>>>reader = itk.itkImageFileReaderF2_New()
>>>>reader.<TAB><TAB><TAB>  reveals nothing

Well, in iplib.py, around line 308, we find:

             if "." in text:
                     matches = self.attr_matches(text)
                     if text.endswith('.') and self.omit__names:
                         if self.omit__names == 1:
                             # true if txt is _not_ a __ name, false otherwise:
                             no__name = (lambda txt:
                                         re.match(r'.*\.__.*?__',txt) is None)
                             # true if txt is _not_ a _ name, false otherwise:
                             no__name = (lambda txt:
                                         re.match(r'.*\._.*?',txt) is None)
                         matches = filter(no__name, matches)
                 except NameError:
                     # catches <undefined attributes>.<tab>
                     matches = []

Now, attr_matches comes from FlexCompleter, which is nearly identical to the 
standard python rlcompleter (I had to patch it back in the days of python 2.1, 
now I can probably drop FlexCompleter altogether).  It does use hasattr and a 
few other things.  I'll try to play with it a bit, but I doubt I'll be able to 
fix this before 0.6.12.  I am completely swamped right now, and I want .12 out 
this weekend for the course.  But before that I have a work trip, so 
essentially zero time to work on ipython before the .12 release.

If you figure this out, and can come up with a patch/fix, send it my way. 
Otherwise, I'll see what I can do, but with little in the way of promises.

Note that this happens for simple numeric arrays as well:

In [4]: a=arange(3)

In [5]: a.s<TAB>
a.savespace   a.spacesaver

In [5]: a.shape
Out[5]: (3,)

The shape attribute doesn't get completed, despite obviously being there. 
This is a simple example to use for quick tests, lighter than loading ITK 
(which probably takes a good 1/2 hour :).

Note that I've only seen this problem with extension code, so something in the 
C-based attribute creation is fooling the default completion code.



More information about the IPython-user mailing list