completer

Module: completer

Inheritance diagram for IPython.completer:

Word completion for IPython.

This module is a fork of the rlcompleter module in the Python standard library. The original enhancements made to rlcompleter have been sent upstream and were accepted as of Python 2.3, but we need a lot more functionality specific to IPython, so this module will continue to live as an IPython-specific utility.

Original rlcompleter documentation:

This requires the latest extension to the readline module (the completes keywords, built-ins and globals in __main__; when completing NAME.NAME..., it evaluates (!) the expression up to the last dot and completes its attributes.

It’s very cool to do “import string” type “string.”, hit the completion key (twice), and see the list of names defined by the string module!

Tip: to use the tab key as the completion key, call

readline.parse_and_bind(“tab: complete”)

Notes:

  • Exceptions raised by the completer function are ignored (and

generally cause the completion to fail). This is a feature – since readline sets the tty device in raw (or cbreak) mode, printing a traceback wouldn’t work well without some complicated hoopla to save, reset and restore the tty state.

  • The evaluation of the NAME.NAME... form may cause arbitrary

application defined code to be executed if an object with a __getattr__ hook is found. Since it is the responsibility of the application (or the user) to enable this feature, I consider this an acceptable risk. More complicated expressions (e.g. function calls or indexing operations) are not evaluated.

  • GNU readline is also used by the built-in functions input() and

raw_input(), and thus these also benefit/suffer from the completer features. Clearly an interactive application can benefit by specifying its own completer function and using raw_input() for all its input.

  • When the original stdin is not a tty device, GNU readline is never

used, and this module (and the readline module) are silently inactive.

Classes

Completer

class IPython.completer.Completer(namespace=None, global_namespace=None)
__init__(namespace=None, global_namespace=None)

Create a new completer for the command line.

Completer([namespace,global_namespace]) -> completer instance.

If unspecified, the default namespace where completions are performed is __main__ (technically, __main__.__dict__). Namespaces should be given as dictionaries.

An optional second namespace can be given. This allows the completer to handle cases where both the local and global scopes need to be distinguished.

Completer instances should be used as the completion mechanism of readline via the set_completer() call:

readline.set_completer(Completer(my_namespace).complete)

attr_matches(text)

Compute matches when text contains a dot.

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

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

complete(text, state)

Return the next possible completion for ‘text’.

This is called successively with state == 0, 1, 2, ... until it returns None. The completion should begin with ‘text’.

global_matches(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 or self.global_namespace that match.

IPCompleter

class IPython.completer.IPCompleter(shell, namespace=None, global_namespace=None, omit__names=0, alias_table=None)

Bases: IPython.completer.Completer

Extension of the completer class with IPython-specific features

__init__(shell, namespace=None, global_namespace=None, omit__names=0, alias_table=None)

IPCompleter() -> completer

Return a completer object suitable for use by the readline library via readline.set_completer().

Inputs:

  • shell: a pointer to the ipython shell itself. This is needed

because this completer knows about magic functions, and those can only be accessed via the ipython instance.

  • namespace: an optional dict where completions are performed.
  • global_namespace: secondary optional dict for completions, to

handle cases (such as IPython embedded inside functions) where both Python scopes are visible.

  • The optional omit__names parameter sets the completer to omit the

‘magic’ names (__magicname__) for python objects unless the text to be completed explicitly starts with one or more underscores.

  • If alias_table is supplied, it should be a dictionary of aliases

to complete.

alias_matches(text)
Match internal system aliases
all_completions(text)
Return all possible completions for the benefit of emacs.
complete(text, state, line_buffer=None)

Return the next possible completion for ‘text’.

This is called successively with state == 0, 1, 2, ... until it returns None. The completion should begin with ‘text’.

Keywords:
  • line_buffer: string

If not given, the completer attempts to obtain the current line buffer via readline. This keyword allows clients which are requesting for text completions in non-readline contexts to inform the completer of the entire text.

dispatch_custom_completer(text)
file_matches(text)

Match filenames, expanding ~USER type strings.

Most of the seemingly convoluted logic in this completer is an attempt to handle filenames with spaces in them. And yet it’s not quite perfect, because Python’s readline doesn’t expose all of the GNU readline details needed for this to be done correctly.

For a filename with a space in it, the printed completions will be only the parts after what’s already been typed (instead of the full completions, as is normally done). I don’t think with the current (as of Python 2.3) Python readline it’s possible to do better.

python_func_kw_matches(text)
Match named parameters (kwargs) of the last open function
python_matches(text)
Match attributes or global python names