You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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!
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.)
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.
@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.
Activity
takluyver commentedon Mar 29, 2012
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 commentedon Aug 29, 2012
Hey,
I am trying to add the above feature. I am confronted with a problem.
Example:
Objects in the namespace -> xa, xb, xc, mydict = {'xy':10, 'xz':20}
Any help/pointers to overcome this?
Thanks!
twmr commentedon Nov 2, 2012
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 commentedon Apr 26, 2013
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 commentedon Jun 6, 2013
I hacked up a solution to this (https://gist.github.com/jnothman/5725776), which given a dict
d = {'abcde': 1}would make suggestions like:It seems to work (add
dict_key_completertoIPCompleter.matchers), but when completing a line with an odd number of quotes in it,readlineadds a closing quote after tab completion, ignoring the fact that my completion already closes the quote (and the bracket). (rl.readlinehas an option calledcompletion_suppress_quote, butreadlinedoesn't, and I don't even know ifpyreadlinewould add it.)dpsanders commentedon Jul 24, 2013
There is a nice solution that already exists which avoids the problem: use attribute access instead of key access.
Configtakes the dictionarydand makes a new oned2which has access via attributes: we can now doAnd, 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#2249Attribute access is (apart from this
.problem) nicer, in my opinion, since it requires 5 fewer key strokes (countingShift), and has less visual noise.jnothman commentedon Mar 8, 2014
@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
Mappingobjects, values may not be stored in memory.jnothman commentedon May 8, 2014
I assume this can be closed.