python readline_16.8. readline — GNU readline 接口 — Python 2.7.18 文档
16.8.readline — GNU readline 接口?
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, which
supports completion of Python identifiers at the interactive prompt. 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() built-in functions.
注解
The underlying Readline library API may be implemented by
the libedit library instead of GNU readline.
On MacOS X the readline module detects which library is being used
at run time.
libedit 所用的配置文件與 GNU readline 的不同。 如果你要在程序中載入配置字符串你可以在 readline.__doc__ 中檢測文本 “libedit” 來區分 GNU readline 和 libedit。
Readline keybindings may be configured via an initialization file, typically
.inputrc in your home directory. See Readline Init File
in the GNU Readline manual for information about the format and
allowable constructs of that file, and the capabilities of the
Readline library in general.
16.8.1.初始化文件?
下列函數與初始化文件和用戶配置有關:
readline.parse_and_bind(string)?
執行在 string 參數中提供的初始化行。 此函數會調用底層庫中的 rl_parse_and_bind()。
readline.read_init_file([filename])?
執行一個 readline 初始化文件。 默認文件名為最近所使用的文件名。 此函數會調用底層庫中的 rl_read_init_file()。
16.8.2.行緩沖區?
下列函數會在行緩沖區上操作。
readline.get_line_buffer()?
返回行緩沖區的當前內容 (底層庫中的 rl_line_buffer)。
readline.insert_text(string)?
將文本插入行緩沖區的當前游標位置。 該函數會調用底層庫中的 rl_insert_text(),但會忽略其返回值。
readline.redisplay()?
改變屏幕的顯示以反映行緩沖區的當前內容。 該函數會調用底層庫中的 rl_redisplay()。
16.8.3.歷史文件?
下列函數會在歷史文件上操作:
readline.read_history_file([filename])?
載入一個 readline 歷史文件,并將其添加到歷史列表。 默認文件名為 ~/.history。 此函數會調用底層庫中的 read_history()。
readline.write_history_file([filename])?
將歷史列表保存為 readline 歷史文件,覆蓋任何現有文件。 默認文件名為 ~/.history。 此函數會調用底層庫中的 write_history()。
readline.get_history_length()?
readline.set_history_length(length)?
設置或返回需要保存到歷史文件的行數。 write_history_file() 函數會通過調用底層庫中的 history_truncate_file() 以使用該值來截取歷史文件。 負值意味著不限制歷史文件的大小。
16.8.4.歷史列表?
以下函數會在全局歷史列表上操作:
readline.clear_history()?
清除當前歷史。 此函數會調用底層庫的 clear_history()。 此 Python 函數僅當 Python 編譯包帶有支持此功能的庫版本時才會存在。
2.4 新版功能.
readline.get_current_history_length()?
返回歷史列表的當前項數。 (此函數不同于 get_history_length(),后者是返回將被寫入歷史文件的最大行數。)
2.3 新版功能.
readline.get_history_item(index)?
返回序號為 index 的歷史條目的當前內容。 條目序號從一開始。 此函數會調用底層庫中的 history_get()。
2.3 新版功能.
readline.remove_history_item(pos)?
從歷史列表中移除指定位置上的歷史條目。 條目位置從零開始。 此函數會調用底層庫中的 remove_history()。
2.4 新版功能.
readline.replace_history_item(pos, line)?
將指定位置上的歷史條目替換為 line。 條目位置從零開始。 此函數會調用底層庫中的 replace_history_entry()。
2.4 新版功能.
readline.add_history(line)?
將 line 添加到歷史緩沖區,相當于是最近輸入的一行。 此函數會調用底層庫中的 add_history()。
16.8.5.啟動鉤子?
2.3 新版功能.
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 編譯包帶有支持此功能的庫版本時才會存在。
16.8.6.Completion?
以下函數與自定義單詞補全函數的實現有關。 這通常使用 Tab 鍵進行操作,能夠提示并自動補全正在輸入的單詞。 默認情況下,Readline 設置為由 rlcompleter 來補全交互模式解釋器的 Python 標識符。 如果 readline 模塊要配合自定義的補全函數來使用,則需要設置不同的單詞分隔符。
readline.set_completer([function])?
設置或移除補全函數。 如果指定了 function,它將被用作新的補全函數;如果省略或為 None,任何已安裝的補全函數將被移除。 補全函數的調用形式為 function(text, state),其中 state 為 0, 1, 2, …, 直至其返回一個非字符串值。 它應當返回下一個以 text 開頭的候選補全內容。
已安裝的補全函數將由傳遞給底層庫中 rl_completion_matches() 的 entry_func 回調函數來發起調用。 text 字符串來自于底層庫中 rl_attempted_completion_function 回調函數的第一個形參。
readline.get_completer()?
獲取補全函數,如果沒有設置補全函數則返回 None。
2.3 新版功能.
readline.get_completion_type()?
獲取正在嘗試的補全類型。 此函數會將底層庫中的 rl_completion_type 變量作為一個整數返回。
2.6 新版功能.
readline.get_begidx()?
readline.get_endidx()?
獲取補全域的開始和結束序號。 這些序號就是傳給底層庫中 rl_attempted_completion_function 回調函數的 start 和 end 參數。
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) 的形式被調用。
2.6 新版功能.
16.8.7.示例?
The following example demonstrates how to use the readline module’s
history reading and writing functions to automatically load and save a history
file named .pyhist from the user’s home directory. The code below would
normally be executed automatically during interactive sessions from the user’s
PYTHONSTARTUP file.
import os
import readline
histfile = os.path.join(os.path.expanduser("~"), ".pyhist")
try:
readline.read_history_file(histfile)
# default history len is -1 (infinite), which may grow unruly
readline.set_history_length(1000)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile
import code
import readline
import atexit
import os
class HistoryConsole(code.InteractiveConsole):
def __init__(self, locals=None, filename="",
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.set_history_length(1000)
readline.write_history_file(histfile)
總結
以上是生活随笔為你收集整理的python readline_16.8. readline — GNU readline 接口 — Python 2.7.18 文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中科院联合研发技术!小米米家全效空气净化
- 下一篇: python flask 大文件 下载_