The readline module defines a number of functions to facilitate completion and reading/writing of history files from the Python interpreter. This module can be used directly or via the rlcompleter module. Settings made using this module affect the behaviour of both the interpreter’s interactive prompt and the prompts offered by the raw_input() and input() 内置函数。
注意
On MacOS X the readline module can be implemented using the libedit library instead of GNU readline.
配置文件对于 libedit 异于 GNU readline。若以编程方式加载配置字符串,可以校验文本 libedit 在 readline.__doc__ 以区分 GNU readline 和 libedit。
The readline 模块定义了下列函数:
Parse and execute single line of a readline init file.
Return the current contents of the line buffer.
Insert text into the command line.
Parse a readline initialization file. The default filename is the last filename used.
Load a readline history file. The default filename is ~/.history .
Save a readline history file. The default filename is ~/.history .
Clear the current history. (Note: this function is not available if the installed version of GNU readline doesn’t support it.)
2.4 版新增。
Return the desired length of the history file. Negative values imply unlimited history file size.
Set the number of lines to save in the history file. write_history_file() uses this value to truncate the history file when saving. Negative values imply unlimited history file size.
Return the number of lines currently in the history. (This is different from get_history_length() , which returns the maximum number of lines that will be written to a history file.)
2.3 版新增。
Return the current contents of history item at index .
2.3 版新增。
Remove history item specified by its position from the history.
2.4 版新增。
Replace history item specified by its position with the given line.
2.4 版新增。
Change what’s displayed on the screen to reflect the current contents of the line buffer.
2.3 版新增。
Set or remove the startup_hook function. If function is specified, it will be used as the new startup_hook function; if omitted or None , any hook function already installed is removed. The startup_hook function is called with no arguments just before readline prints the first prompt.
Set or remove the pre_input_hook function. If function is specified, it will be used as the new pre_input_hook function; if omitted or None , any hook function already installed is removed. The pre_input_hook function is called with no arguments after the first prompt has been printed and just before readline starts reading input characters.
Set or remove the completer function. If function is specified, it will be used as the new completer function; if omitted or None , any completer function already installed is removed. The completer function is called as function(text, state) , for state in 0 , 1 , 2 , ..., until it returns a non-string value. It should return the next possible completion starting with text .
获取补全器函数,或 None 若没有设置补全器函数。
2.3 版新增。
Get the type of completion being attempted.
2.6 版新增。
Get the beginning index of the readline tab-completion scope.
Get the ending index of the readline tab-completion scope.
Set the readline word delimiters for tab-completion.
Get the readline word delimiters for tab-completion.
Set or remove the completion display function. If function is specified, it will be used as the new completion display function; if omitted or None , any completion display function already installed is removed. The completion display function is called as function(substitution, [matches], longest_match_length) once each time matches need to be displayed.
2.6 版新增。
Append a line to the history buffer, as if it was the last line typed.
另请参阅
以下范例演示如何使用 readline 模块的历史读取和写入功能,以自动加载和保存历史文件命名 .pyhist 从用户 Home (主) 目录。以下代码在交互式会话期间,通常会自动执行从用户的 PYTHONSTARTUP 文件。
import os import readline histfile = os.path.join(os.path.expanduser("~"), ".pyhist") try: readline.read_history_file(histfile) except IOError: pass import atexit atexit.register(readline.write_history_file, histfile) del os, histfile
以下范例扩展了 code.InteractiveConsole 类以支持历史的保存/还原。
import code import readline import atexit import os class HistoryConsole(code.InteractiveConsole): def __init__(self, locals=None, filename="<console>", histfile=os.path.expanduser("~/.console-history")): code.InteractiveConsole.__init__(self, locals, filename) self.init_history(histfile) def init_history(self, histfile): readline.parse_and_bind("tab: complete") if hasattr(readline, "read_history_file"): try: readline.read_history_file(histfile) except IOError: pass atexit.register(self.save_history, histfile) def save_history(self, histfile): readline.write_history_file(histfile)