readline
---GNU读线接口¶
这个 readline
模块定义了许多函数,以便于完成和读取/写入来自Python解释器的历史文件。此模块可以直接使用,也可以通过 rlcompleter
模块,它支持在交互提示下完成python标识符。使用此模块进行的设置会影响解释器的交互提示和内置提示的行为。 input()
功能。
readline键绑定可以通过初始化文件配置,通常 .inputrc
在您的主目录中。见 Readline Init File 在GNU readline手册中,获取有关该文件的格式和允许构造以及readline库的一般功能的信息。
注解
底层的readline库API可以由 libedit
库而不是GNU readline。论马科斯 readline
模块检测运行时正在使用哪个库。
的配置文件 libedit
与GNU readline不同。如果以编程方式加载配置字符串,则可以检查中的文本“libedit” readline.__doc__
区分gnu readline和libedit。
如果你使用 编辑线 /`` libedit``readline emulation on macos,位于主目录中的初始化文件命名为 .editrc
. 例如,以下内容 ~/.editrc
将开启 vi 键绑定和制表符完成:
python:bind -v
python:bind ^I rl_complete
初始化文件¶
以下函数与init文件和用户配置相关:
- readline.parse_and_bind(string)¶
执行中提供的初始化行 string 参数。这个调用
rl_parse_and_bind()
在基础库中。
- readline.read_init_file([filename])¶
执行readline初始化文件。默认文件名是使用的最后一个文件名。这个调用
rl_read_init_file()
在基础库中。
行缓冲器¶
以下功能在线路缓冲器上运行:
- readline.get_line_buffer()¶
返回行缓冲区的当前内容 (
rl_line_buffer
在底层库中)。
- readline.insert_text(string)¶
在光标位置将文本插入行缓冲区。这个调用
rl_insert_text()
在基础库中,但忽略返回值。
- readline.redisplay()¶
更改屏幕上显示的内容以反映行缓冲区的当前内容。这个调用
rl_redisplay()
在基础库中。
历史文件¶
以下函数对历史文件进行操作:
- readline.read_history_file([filename])¶
加载一个readline历史文件,并将其附加到历史列表中。默认文件名为
~/.history
. 这个调用read_history()
在基础库中。
- readline.write_history_file([filename])¶
将历史记录列表保存到可读行历史文件中,覆盖任何现有文件。默认文件名为
~/.history
. 这个调用write_history()
在基础库中。
- readline.append_history_file(nelements[, filename])¶
附加最后一个 N元素 文件的历史记录项。默认文件名为
~/.history
. 文件必须已经存在。这个调用append_history()
在基础库中。只有为支持该函数的库版本编译了python时,该函数才存在。3.5 新版功能.
- readline.get_history_length()¶
- readline.set_history_length(length)¶
设置或返回要保存在历史文件中的所需行数。这个
write_history_file()
函数使用此值通过调用history_truncate_file()
在基础库中。负值表示历史文件大小不受限制。
历史列表¶
以下函数在全局历史记录列表上操作:
- readline.clear_history()¶
清除当前历史记录。这个调用
clear_history()
在基础库中。只有为支持该函数的库版本编译了python函数时,该函数才存在。
- readline.get_current_history_length()¶
返回当前在历史记录中的项目数。(这不同于
get_history_length()
,返回将写入历史文件的最大行数。)
- readline.get_history_item(index)¶
返回历史记录项的当前内容 index . 项目索引是基于一个的。这个调用
history_get()
在基础库中。
- readline.remove_history_item(pos)¶
从历史记录中删除由其位置指定的历史记录项。位置以零为基础。这个调用
remove_history()
在基础库中。
- readline.replace_history_item(pos, line)¶
将其位置指定的历史记录项替换为 line . 位置以零为基础。这个调用
replace_history_entry()
在基础库中。
- readline.add_history(line)¶
追加 line 到历史缓冲区,就好像它是最后一行键入的一样。这个调用
add_history()
在基础库中。
- readline.set_auto_history(enabled)¶
启用或禁用自动调用
add_history()
通过readline读取输入时。这个 启用 参数应该是一个布尔值,如果为真,则启用自动历史记录;如果为假,则禁用自动历史记录。3.6 新版功能.
CPython implementation detail: Auto history is enabled by default, and changes to this do not persist across multiple sessions.
启动钩子¶
- readline.set_startup_hook([function])¶
设置或删除
rl_startup_hook
基础库的回调。如果 function 如果指定,它将用作新的挂钩功能;如果省略或None
,已安装的任何功能都将被删除。在readline打印第一个提示之前,调用钩子时没有参数。
- readline.set_pre_input_hook([function])¶
设置或删除
rl_pre_input_hook
基础库的回调。如果 function 如果指定,它将用作新的挂钩功能;如果省略或None
,已安装的任何功能都将被删除。在打印第一个提示之后,在readline开始读取输入字符之前,调用钩子时不带参数。只有为支持该函数的库版本编译了python时,该函数才存在。
完成¶
以下函数与实现自定义单词完成函数有关。这通常由tab键操作,可以建议并自动完成正在键入的单词。默认情况下,readline设置为 rlcompleter
完成交互式解释器的python标识符。如果 readline
模块将与自定义完成符一起使用,应设置一组不同的单词分隔符。
- readline.set_completer([function])¶
设置或删除完成功能。如果 function 如果指定,它将用作新的完成函数;如果省略或
None
,任何已安装的完成程序功能都将被删除。completer函数被调用为function(text, state)
,为了 state 在里面0
,1
,2
,…,直到它返回一个非字符串值。它应该返回下一个可能的完成时间,从 text .已安装的完成程序函数由 entry_func 回调传递给
rl_completion_matches()
在基础库中。这个 text 字符串从第一个参数到rl_attempted_completion_function
基础库的回调。
- readline.get_completer()¶
获取完成函数,或
None
如果没有设置完成功能。
- readline.get_completion_type()¶
获取正在尝试完成的类型。这将返回
rl_completion_type
基础库中的变量为整数。
- readline.get_begidx()¶
- readline.get_endidx()¶
获取完成范围的开始或结束索引。这些索引是 开始 和 end 传递给的参数
rl_attempted_completion_function
基础库的回调。
- readline.set_completer_delims(string)¶
- readline.get_completer_delims()¶
设置或获取要完成的单词分隔符。这些决定了要考虑完成的单词的开头(完成范围)。这些函数访问
rl_completer_word_break_characters
基础库中的变量。
- readline.set_completion_display_matches_hook([function])¶
设置或删除完成显示功能。如果 function 如果指定,它将用作新的完成显示功能;如果省略或
None
,任何已安装的完成显示功能都将被删除。这将设置或清除rl_completion_display_matches_hook
基础库中的回调。完成显示函数被调用为function(substitution, [matches], longest_match_length)
每次需要显示一次匹配项。
例子¶
下面的示例演示如何使用 readline
模块的历史读取和写入功能,用于自动加载和保存名为 .python_history
从用户的主目录。以下代码通常在用户的交互会话期间自动执行。 PYTHONSTARTUP
文件。地址:
import atexit
import os
import readline
histfile = os.path.join(os.path.expanduser("~"), ".python_history")
try:
readline.read_history_file(histfile)
# default history len is -1 (infinite), which may grow unruly
readline.set_history_length(1000)
except FileNotFoundError:
pass
atexit.register(readline.write_history_file, histfile)
当运行python时,此代码实际上是自动运行的 interactive mode (见 读线配置 )
下面的示例实现了相同的目标,但只通过附加新的历史记录来支持并发的交互式会话。地址:
import atexit
import os
import readline
histfile = os.path.join(os.path.expanduser("~"), ".python_history")
try:
readline.read_history_file(histfile)
h_len = readline.get_current_history_length()
except FileNotFoundError:
open(histfile, 'wb').close()
h_len = 0
def save(prev_h_len, histfile):
new_h_len = readline.get_current_history_length()
readline.set_history_length(1000)
readline.append_history_file(new_h_len - prev_h_len, histfile)
atexit.register(save, h_len, histfile)
以下示例扩展了 code.InteractiveConsole
类以支持历史记录保存/还原。::
import atexit
import code
import os
import readline
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 FileNotFoundError:
pass
atexit.register(self.save_history, histfile)
def save_history(self, histfile):
readline.set_history_length(1000)
readline.write_history_file(histfile)