Skip to content

dictionary key completion #1533

Closed
Closed

Description

@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

Activity

  1. takluyver commented on Mar 29, 2012

    @takluyver
    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.

  2. anushs commented on Aug 29, 2012

    @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!

  3. twmr commented on Nov 2, 2012

    @twmr
    Contributor

    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.

  4. jnothman commented on Apr 26, 2013

    @jnothman
    Contributor

    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!

  5. jnothman commented on Jun 6, 2013

    @jnothman
    Contributor

    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.)

  6. dpsanders commented on Jul 24, 2013

    @dpsanders
    Contributor

    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.

  7. jnothman commented on Mar 8, 2014

    @jnothman
    Contributor

    @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.

  8. jnothman commented on May 8, 2014

    @jnothman
    Contributor

    I assume this can be closed.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions