XD blog

blog page

completion


2014-08-05 Custom completion for IPython

Autocompletion is a nice feature quite useful when typing very long names. It is implemented by modules such as IPython, bpython or DreamPie. I'm quite a big fan of a the first one, the notebooks to be precise. However, it did not seem quite easy to add my custom autocompletion. I tried some search queries but I was only able to find Integrating your objects with IPython or IPEP 11: Tab Completion System Refactor.

So I followed the idea mentioned on the first page. I created kind of fake classes. They do not do anything but populating the dictionary __dict__ so that the information appears when tab is pressed. So I just did that for files except I had to replace some characters, normal for a filename but unexpected for a variable name. Based on that, I wrote some code (here) to populate a class with members very close to file names:

from pyquickhelper import AutoCompletionFile
d = AutoCompletionFile(".")

As a result, when pressing tab, file names will appear:

I replaced "." by "_" but it is quite easy to use. By adding ._, you get the real path.

os.listdir(d.sphinxdoc.source._)

Xavier Dupré