readline
这个 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.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.get_current_history_length()
返回当前在历史记录中的项目数。(这不同于
get_history_length()
,返回将写入历史文件的最大行数。)
启动钩子
完成
以下函数与实现自定义单词完成函数有关。这通常由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_begidx()
- readline.get_endidx()
获取完成范围的开始或结束索引。这些索引是 开始 和 end 传递给的参数
rl_attempted_completion_function
基础库的回调。
例子
下面的示例演示如何使用 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)