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 withstate == 0, 1, 2, ...
until the method returnsNone
.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ódulokeyword
).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 thedir()
function. Any exception raised during the evaluation of the expression is caught, silenced andNone
is returned.