zeb
zeb

Reputation: 29

Using append() and readline for completion in python

Specifically can I provide append() a Null/None value in Python?

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.

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)"

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

Upvotes: 0

Views: 573

Answers (3)

zeb
zeb

Reputation: 29

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:

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!

Upvotes: 0

linbo
linbo

Reputation: 2431

  1. append require exactly one argument

  2. None object can't invoke append function

Upvotes: 0

alexvassel
alexvassel

Reputation: 10740

tokens variable is a list, so lists method append really takes exactly one argument.

>>> a = []
>>> a
>>> []
>>> a.append(1)
>>> a
>>> [1]
>>> a.append()
>>> TypeError: append() takes exactly one argument (0 given)
>>> a.append(None)
>>> a
>>> [1, None]

Upvotes: 1

Related Questions