dictionary key completion #1533

Closed
gasagna opened this Issue Mar 29, 2012 · 8 comments

7 participants

@gasagna

Hi,

Would it be possible to implement a feature like completion for dictionary keys? Something like:

mydict = {'amen':42, 'amenable':32, 0:'left'}
mydict[' (press tab)
'amen' 'amenable' 0

Thanks for the wonderful tool!

Davide

@takluyver
IPython member

Yes-ish, although it might only be able to work in certain situations. Have a look at IPython.core.completer and IPython.core.completerlib.

@anushs

Hey,
I am trying to add the above feature. I am confronted with a problem.

  • The logic conflicts with the tab-completion to populate lists and hence IPython.core.completer recognizes '[' as a delimiter.

Example:

  • Objects in the namespace -> xa, xb, xc, mydict = {'xy':10, 'xz':20}

    mydict[x
    ---> xa xb xc

Any help/pointers to overcome this?

Thanks!

@thisch

Why don't you just look for the opening bracket followed by either ' or " in your completion logic. Thus, if you want to complete keys which are strings then you have to type mydict["TAB. This simplifies your completion logic because you don't have to ignore objects in your namespace.

@jnothman

I think this may only apply to literal string keys, in which case @thisch is right it should apply after [" or [' (assuming there's no intention to support .get(', etc.). It would be nice if this also supported the keys of numpy records!

@jnothman

I hacked up a solution to this (https://gist.github.com/jnothman/5725776), which given a dict d = {'abcde': 1} would make suggestions like:

d[ -> 'abcde']
d[' -> abcde']
d["a -> abcde"]

It seems to work (add dict_key_completer to IPCompleter.matchers), but when completing a line with an odd number of quotes in it, readline adds a closing quote after tab completion, ignoring the fact that my completion already closes the quote (and the bracket). (rl.readline has an option called completion_suppress_quote, but readline doesn't, and I don't even know if pyreadline would add it.)

@dpsanders

There is a nice solution that already exists which avoids the problem: use attribute access instead of key access.

from IPython.config import Config

d = {"H": 1, "He": 2}
d2 = Config(d)

Config takes the dictionary d and makes a new one d2 which has access via attributes: we can now do

print d.H
d.He = 3

And, magically, completion works!:

d.H<TAB>

The only problem occurs when the keys themselves contain ..
This is the case, for example, in matplotlib.rcParams; see matplotlib/matplotlib#2249

Attribute access is (apart from this . problem) nicer, in my opinion, since it requires 5 fewer key strokes (counting Shift), and has less visual noise.

@jnothman

@dpsanders, apart from its inconvenience, your solution works only for keys that are valid identifiers. It also assumes that a copy of the dict is cheap, while for some Mapping objects, values may not be stored in memory.

@jnothman

I assume this can be closed.

@minrk minrk modified the milestone: 3.0, wishlist May 8, 2014
@minrk minrk closed this May 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment