[英]python complete, file vs object
我最近在 python interperter 中發現了自動完成功能: https : //docs.python.org/2.7/tutorial/interactive.html 。 這對於加快我在交互式解釋器中進行的測試非常有用。 有兩件事完全沒有用,都是有用的。
如果我只是將C+f: complete
放在我的 .inputrc 中(或使用沒有 rlcompleter 的 readline),當我按下 Ctl+f 時,我會在我啟動解釋器的目錄中完成文件。 當我加載模塊readline
和rlcompleter
並將readline.parse_and_bind('Cn: complete')
到 .pystartup 文件時,它會將 Ctl+n 和 Ctl+f 轉換為自動完成 python 對象。
我想兩者都做,但不知道如何防止rlcompleter
覆蓋標准完成。 有沒有辦法啟動readline
兩個實例,一個readline
,一個不使用rlcompleter
?
這是我的 .pystartup 文件
import atexit
import os
import readline
import rlcompleter #removing this does file completion.
readline.parse_and_bind('C-n: complete')
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
這個怎么運作:
當您導入rlcompleter
,它會安裝rlcompleter.Completer().complete
作為readline
的完成程序: readline.set_completer(Completer().complete)
。
如果沒有rlcompleter
,則completer
為None
,因此底層 GNU readline lib 默認使用rl_filename_completion_function
。
綁定鍵和完成邏輯是由 GNU readline lib 實現的,所以在 Python 中start up two instances of readline
沒有什么可做start up two instances of readline
......
我沒有找到在 Python 中調用默認rl_filename_completion_function
的方法(在 C 擴展中是可能的),所以我猜你必須在 Python 中復制rl_filename_completion_function
的邏輯。
這意味着您應該繼承Completer
並構建您的自定義complete
。 但是您仍然無法將這兩個邏輯拆分為Cn
和Cf
:(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.