rlcompleter — Função de autocomplemento para GNU readline

Código-fonte: Lib/rlcompleter.py


O módulo rlcompleter define uma função de autocompletamento adequada para ser passada para set_completer() no módulo readline.

Quando este módulo é importado em uma plataforma Unix com o módulo readline disponível, uma instância da classe Completer é criada automaticamente e seu método complete() é definido como o autocompletamento do readline. O método fornece o autocompletamento de identificadores e palavras-chaves válidas do Python.

Exemplo:

>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> readline. <TAB PRESSED>
readline.__doc__          readline.get_line_buffer(  readline.read_init_file(
readline.__file__         readline.insert_text(      readline.set_completer(
readline.__name__         readline.parse_and_bind(
>>> readline.

The rlcompleter module is designed for use with Python’s interactive mode. Unless Python is run with the -S option, the module is automatically imported and configured (see Configuração Readline).

Em plataformas sem readline, a classe Completer definida por este módulo ainda pode ser usada para propósitos personalizados.

class rlcompleter.Completer

Os objetos Completer têm o seguinte método:

complete(text, state)

Return the next possible completion for text.

When called by the readline module, this method is called successively with state == 0, 1, 2, ... until the method returns None.

Se chamado para text que não inclui um caractere de ponto ('.'), ele será completado a partir dos nomes atualmente definidos em __main__, builtins e palavras reservadas (conforme definido pelo módulo keyword).

If called for a dotted name, it will try to evaluate anything without obvious side-effects (functions will not be evaluated, but it can generate calls to __getattr__()) up to the last part, and find matches for the rest via the dir() function. Any exception raised during the evaluation of the expression is caught, silenced and None is returned.