<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hey,<div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>By hacking on the code for completerlib.py:cd_completer, I sketched out a little demo of what you could do with this: <a href="https://gist.github.com/4167151">https://gist.github.com/4167151</a>. The architecture is totally wrong, but it lets you get behavior like:</div><div><br></div><div>```</div><div><div><div>In [1]: ls</div><div>datfile.dat  dir1/        dir2/        file.gz      random_junk  test.py</div><div><br></div><div>In [2]: directory_as_a_variable = 'sdfsfsd'</div><div><br></div><div>In [3]: f = np.loadtxt(<TAB></div><div>datfile.dat  dir1/        dir2/        file.gz  </div></div></div><div><br></div><div><div>In [4]: normal_function(<TAB></div><div>Display all 330 possibilities? (y or n)</div></div><div><br></div><div><div>In [5]: g = np.loadtxt(di<TAB></div><div>dict                      dir1/                     directory_as_a_variable   divmod                    </div><div>dir                       dir2/                     directory_of_my_choosing  </div></div><div>```</div><div><br></div><div>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.</div><div><br></div><div>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<font class="Apple-style-span" color="#445588" face="Consolas, 'Liberation Mono', Courier, monospace"><span class="Apple-style-span" style="line-height: 16px; white-space: pre;"><b> </b></span></font>instead.</div><div><br></div><div>I think I'm up for giving this a shot.</div><div><br></div><div>Thoughts? Is this a feature anyone else would find interesting?</div><div><br></div><div>-Robert</div><div><br></div><div><br></div></body></html>