[IPython-user] Tab completion question

Thomas Heller theller@ctypes....
Mon Apr 6 05:14:47 CDT 2009


Andrea Riciputi schrieb:
> Hi,
> how can I get the tab completion in IPython to work in a case- 
> insensitive way? I'd really like to  get:
> 
> Foo.a[TAB][TAB]
> Foo.amethod  Foo.AnotherMethod
> 
> Is it a readline thing or a IPython thing?
> 
> Cheers,
>   Andrea

Don't know how IPython does it, but it seems that you can get
case-insensitive tab completion in readline by changing the
string comparisons in Lib\rlcompleter.py, in the last lines
of the methods attr_matches() and global_matches():

<snip>
    def global_matches(self, text):
        """Compute matches when text is a simple name.

        Return a list of all keywords, built-in functions and names currently
        defined in self.namespace that match.

        """
        import keyword
        matches = []
        n = len(text)
        for list in [keyword.kwlist,
                     __builtin__.__dict__,
                     self.namespace]:
            for word in list:
                if word[:n].lower() == text.lower() and word != "__builtins__":
####                       ^^^^^^^^         ^^^^^^^
                    matches.append(word)
        return matches

    def attr_matches(self, text):
        """Compute matches when text contains a dot.

        Assuming the text is of the form NAME.NAME....[NAME], and is
        evaluatable in self.namespace, it will be evaluated and its attributes
        (as revealed by dir()) are used as possible completions.  (For class
        instances, class members are also considered.)

        WARNING: this can still invoke arbitrary C code, if an object
        with a __getattr__ hook is evaluated.

        """
        import re
        m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text)
        if not m:
            return
        expr, attr = m.group(1, 3)
        object = eval(expr, self.namespace)
        words = dir(object)
        if hasattr(object,'__class__'):
            words.append('__class__')
            words = words + get_class_members(object.__class__)
        matches = []
        n = len(attr)
        for word in words:
            if word[:n].lower() == attr.lower() and word != "__builtins__":
####                    ^^^^^^^^       ^^^^^^^
                matches.append("%s.%s" % (expr, word))
        return matches
</snip>

-- 
Thomas



More information about the IPython-user mailing list