[IPython-dev] Function specific hooks into the ipython tab completion system
rmcgibbo at gmail.com
Thu Nov 29 02:58:14 EST 2012
Tab completion in IPython is one of the things that makes it so useful, especially the context specific tab completion for things like "from ..." where only packages, or obviously the special completion for attributes when the line contains a dot.
I use IPython for interactive data analysis a lot, and one of the most frequent operations is loading up data with something like numpy.loadtxt() or various related functions.
It would be really awesome if we could annotate functions to interact with the tab completion system, perhaps for instance saying that argument 0 to numpy.loadtxt() is supposed to be a filename, so let's give tab-complete suggestions that try to look for directories/files. Some functions only files with specific extensions, so you could filter based on that or whatever.
By hacking on the code for completerlib.py:cd_completer, I sketched out a little demo of what you could do with this: https://gist.github.com/4167151. The architecture is totally wrong, but it lets you get behavior like:
In : ls
datfile.dat dir1/ dir2/ file.gz random_junk test.py
In : directory_as_a_variable = 'sdfsfsd'
In : f = np.loadtxt(<TAB>
datfile.dat dir1/ dir2/ file.gz
In : normal_function(<TAB>
Display all 330 possibilities? (y or n)
In : g = np.loadtxt(di<TAB>
dict dir1/ directory_as_a_variable divmod
dir dir2/ directory_of_my_choosing
Basically hitting the tab completion, when np.loadtxt is on the input line, only shows directories and files that end with a certain extension. If you start to type in the name of an object in your namespace, it'll show up too, but only once you've typed in more than 1 matching character.
The implementation in my gist is pretty lame. The way I've coded it up, the special behavior is based on simply finding the string "np.loadtxt" on the input line, not on the actual function. This means you can't really make the behavior specific to your position in the argument list (i.e. I know that the first arg is a filename, and so should be tab completed like this, but the other ones are not). I suspect the right way to do the implementation is via function decorators to specify the behavior and then adding to IPCompleter instead.
I think I'm up for giving this a shot.
Thoughts? Is this a feature anyone else would find interesting?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the IPython-dev