简体   繁体   English

使用append()和readline在python中完成

[英]Using append() and readline for completion in python

Specifically can I provide append() a Null/None value in Python? 具体来说,我可以在Python中提供append()Null / None值吗?

I am trying to add auto complete functionality to a command line application, so am using readline to obtain anything the user may have typed at the raw_input prompt. 我试图将自动完成功能添加到命令行应用程序,因此要使用readline获取用户可能在raw_input提示符下键入的任何内容。

I'm getting an issue when I try to tab (with no value entered into the console) and get this message: "append() takes exactly one argument (0 given)" 当我尝试使用Tab键(未在控制台中输入任何值)并收到此消息时,我遇到了一个问题:“ append()恰好带有一个参数(给定0)”

Here is the code: 这是代码:

tokens = readline.get_line_buffer().split()
if not tokens or readline.get_line_buffer()[-1] == ' ':
    tokens.append()

I'm using the example provided here because of the traverse function where the depth of the tree isn't an issue: https://www.ironalbatross.net/wiki/index.php5?title=Python_Readline_Completions#Complex_problem_.28Regular_Grammar.29 我使用此处提供的示例是因为遍历功能不影响树的深度: https : //www.ironalbatross.net/wiki/index.php5?title=Python_Readline_Completions#Complex_problem_.28Regular_Grammar.29

tokens variable is a list , so lists method append really takes exactly one argument. tokens变量是一个list ,因此list方法append确实只需要一个参数。

>>> a = []
>>> a
>>> []
>>> a.append(1)
>>> a
>>> [1]
>>> a.append()
>>> TypeError: append() takes exactly one argument (0 given)
>>> a.append(None)
>>> a
>>> [1, None]
  1. append require exactly one argument 追加仅需要一个参数

  2. None object can't invoke append function 没有对象不能调用附加函数

OK I managed to fix it... wasn't sure what value to provide append() when there was no value returned by readline so did this and it worked: 好的,我设法解决了这个问题……不确定readline没有返回值时提供什么值append(),这样做是可行的:

def complete(self,text,state):
try:
    tokens = readline.get_line_buffer().split()
    if not tokens or readline.get_line_buffer()[-1] == ' ':
        tokens.append(text)

Thanks guys! 多谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM