Python readline 模块:设置 2 个字符分隔符
我正在使用 readline 模块自动补全姓名(名字、姓氏)。
我想设置 2 个字符分隔符,但通过设置 readline.set_completer_delims(', ') 它接受逗号和空格作为分隔符。但我只想要组合。
问题是,现在我输入的名字多次出现,且姓氏不同。 readline 不会在补全中建议所有可能的姓氏,而是认为空格字符是分隔符,并开始重新建议所有名称。
我该如何解决这个问题?
更多信息:我已经在使用自定义完成函数:
# Configure and enable tab completion
def completer(text, state):
"""Contacts completer function for readline module"""
options = [x[2].strip() for x in contacts
if x[2].lower().startswith(text.strip().lower())]
try:
return options[state] + ', '
except IndexError:
return None
readline.set_completer(completer)
问题不在于该函数工作不正确。我对其进行了调试,当完成以空格结尾的单词(如 "simon "
)时,传递给完成者的 text
值是 " "而不是<代码>“西蒙”。
I'm using the readline
module to autocomplete names (firstname lastname).
I want to set a 2 character delimiter, but by setting readline.set_completer_delims(', ')
it accepts both the comma and the whitespace as a delimiter. But I only want the combination.
The problem is, now I enter a first name that exists several times, with different last names. Instead of suggesting all possible last names in the completion, readline thinks that the whitespace character is a delimiter and starts suggesting all names over again.
How can I solve this problem?
Further information: I am already using a custom completion function:
# Configure and enable tab completion
def completer(text, state):
"""Contacts completer function for readline module"""
options = [x[2].strip() for x in contacts
if x[2].lower().startswith(text.strip().lower())]
try:
return options[state] + ', '
except IndexError:
return None
readline.set_completer(completer)
The problem is not that the function works incorrectly. I debugged it, and when completing a word that ends with a whitespace (like "simon "
), the text
value passed to the completer is " "
instead of "simon "
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能必须为此使用自己的函数,
来自 python 文档:
“对于 0、1、2、... 中的状态,完成函数被称为函数(文本,状态),直到它返回一个非-字符串值。它应该返回以文本开头的下一个可能的完成。”
You probably will have to use your own function for this, with
From python doc :
"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."