attribute error

M

M.N.A.Smadi

HI;

I am having the following error. I am using someone else's code and
all they are doing is pass an argv to a function then

def execute_action(manager, argv):
method_name = argv.pop(0).lower()


and am getting this strange error.
AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

but i always thought that something like this will be standard stuff.
Any ideas?

thanks
moe smadi
 
M

Mike Meyer

M.N.A.Smadi said:
HI;

I am having the following error. I am using someone else's code and
all they are doing is pass an argv to a function then

def execute_action(manager, argv):
method_name = argv.pop(0).lower()


and am getting this strange error.
AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

but i always thought that something like this will be standard stuff.
Any ideas?

Yes - show us the rest of the code. execute_action is never called in
the snippets you posted, and it's pretty clear that it's being invoked
with the wrong thing as an argument. Can you provide a minimal working
(well, executable) code sample that generates the error message you
are getting?

Oh yeah - post the full traceback as well.

<mike
 
S

Steven D'Aprano

HI;

I am having the following error. I am using someone else's code and
all they are doing is pass an argv to a function then

def execute_action(manager, argv):
method_name = argv.pop(0).lower()


and am getting this strange error.
AttributeError: 'str' object has no attribute 'pop'

Since you haven't told us what you have passed as argv, I will look into
my crystal ball and use my psychic powers...

Things are unclear... no, wait, I see a glimmer of light... are you using
a single string, something like "this is a list of arguments"? The
function is expecting a list of strings ["this", "is", "a", "list", "of",
"arguments"].

You may find the split() method useful for breaking up a single string
into a list of strings.
 
M

M.N.A.Smadi

HI;

I am having the following error:

AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

The code being executed is:
if command not in commands:
raise ArgumentsError('invalid arguments.')



if command == 'usage':
return usage(argv[0], sys.stdout)

manager = Manager.Manager(*Config.Config().get_connection())

if command == 'actions':
show_actions()

if command == 'help':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

show_actions(argv[2])

elif command == 'action':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

try:
execute_action(manager, argv[2:])
except TypeError, e:
print "Bad arguments specified. Help for %s:" % (argv[2],)
show_actions(argv[2])

elif command == 'command':
execute_action('command', argv[2])

def execute_action(manager, argv):
method_name = argv.pop(0).lower()



but i always thought that something like this will be standard stuff.
Any ideas?
 
M

Mike Meyer

HI;

I am having the following error:

AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

The code being executed is:
if command not in commands:
raise ArgumentsError('invalid arguments.')



if command == 'usage':
return usage(argv[0], sys.stdout)

manager = Manager.Manager(*Config.Config().get_connection())

if command == 'actions':
show_actions()

if command == 'help':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

show_actions(argv[2])

elif command == 'action':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

try:
execute_action(manager, argv[2:])
except TypeError, e:
print "Bad arguments specified. Help for %s:" % (argv[2],)
show_actions(argv[2])

elif command == 'command':
execute_action('command', argv[2])

This should be execute_action('command', argv[2:]), with the ':' added.

<mike
 
M

M.N.A.Smadi

This has nothing to do with how the argument is passed. It is prob
something wrong with str.pop in my python because when i run python and type
import os
import string
x = '1 2 3'
x.pop()

i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'


Mike said:
M.N.A.Smadi <[email protected]> typed: said:
HI;

I am having the following error:

AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

The code being executed is:
if command not in commands:
raise ArgumentsError('invalid arguments.')



if command == 'usage':
return usage(argv[0], sys.stdout)

manager = Manager.Manager(*Config.Config().get_connection())

if command == 'actions':
show_actions()

if command == 'help':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

show_actions(argv[2])

elif command == 'action':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

try:
execute_action(manager, argv[2:])
except TypeError, e:
print "Bad arguments specified. Help for %s:" % (argv[2],)
show_actions(argv[2])

elif command == 'command':
execute_action('command', argv[2])

This should be execute_action('command', argv[2:]), with the ':' added.

<mike


def execute_action(manager, argv):
method_name = argv.pop(0).lower()



but i always thought that something like this will be standard stuff.
Any ideas?


Mike Meyer wrote:
 
M

Mike Meyer

This has nothing to do with how the argument is passed. It is prob
something wrong with str.pop in my python because when i run python and type
import os
import string
x = '1 2 3'
x.pop()

i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'

The only thing wrong with str.pop is that you're trying to invoke
it. The interpreter is telling you that string doesn't *have* a pop
method. The interpreter is right. Strings are immutable, so "pop"
doesn't make any sense for them.

<mike
Mike said:
M.N.A.Smadi <[email protected]> typed: said:
HI;

I am having the following error:

AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect
from Asterisk import Manager, BaseException, Config
import Asterisk.Util

The code being executed is:
if command not in commands:
raise ArgumentsError('invalid arguments.')



if command == 'usage':
return usage(argv[0], sys.stdout)

manager = Manager.Manager(*Config.Config().get_connection())

if command == 'actions':
show_actions()

if command == 'help':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

show_actions(argv[2])

elif command == 'action':
if len(argv) < 3:
raise ArgumentsError('please specify an action.')

try:
execute_action(manager, argv[2:])
except TypeError, e:
print "Bad arguments specified. Help for %s:" % (argv[2],)
show_actions(argv[2])

elif command == 'command':
execute_action('command', argv[2])

This should be execute_action('command', argv[2:]), with the ':' added.

<mike


def execute_action(manager, argv):
method_name = argv.pop(0).lower()



but i always thought that something like this will be standard stuff.
Any ideas?


Mike Meyer wrote:








HI;

I am having the following error. I am using someone else's code and
all they are doing is pass an argv to a function then

def execute_action(manager, argv):
method_name = argv.pop(0).lower()


and am getting this strange error.
AttributeError: 'str' object has no attribute 'pop'

am using Python 2.3.4 and am importing the following libraries:

import sys, os, inspect


from Asterisk import Manager, BaseException, Config


import Asterisk.Util

but i always thought that something like this will be standard stuff.
Any ideas?




Yes - show us the rest of the code. execute_action is never called in
the snippets you posted, and it's pretty clear that it's being invoked
with the wrong thing as an argument. Can you provide a minimal working
(well, executable) code sample that generates the error message you
are getting?

Oh yeah - post the full traceback as well.

<mike
 
S

Steve Holden

Mike said:
This has nothing to do with how the argument is passed. It is prob
something wrong with str.pop in my python because when i run python and type
import os
import string
x = '1 2 3'
x.pop()

i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'


The only thing wrong with str.pop is that you're trying to invoke
it. The interpreter is telling you that string doesn't *have* a pop
method. The interpreter is right. Strings are immutable, so "pop"
doesn't make any sense for them.

<mike
[...]

Just to hammer the point home:

Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File said:
>>> x = [1, 2, 3]
>>> x.pop() 3
>>> x.pop() 2
>>> x [1]
>>>

So if you genuinely have a string containing the values, split it onto a
list first using something like

x = x.split()

regards
Steve
 
S

Steve Holden

Mike said:
This has nothing to do with how the argument is passed. It is prob
something wrong with str.pop in my python because when i run python and type
import os
import string
x = '1 2 3'
x.pop()

i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'


The only thing wrong with str.pop is that you're trying to invoke
it. The interpreter is telling you that string doesn't *have* a pop
method. The interpreter is right. Strings are immutable, so "pop"
doesn't make any sense for them.

<mike
[...]

Just to hammer the point home:

Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File said:
>>> x = [1, 2, 3]
>>> x.pop() 3
>>> x.pop() 2
>>> x [1]
>>>

So if you genuinely have a string containing the values, split it onto a
list first using something like

x = x.split()

regards
Steve
 
S

Steven D'Aprano

This has nothing to do with how the argument is passed. It is prob
something wrong with str.pop in my python because when i run python and type
import os
import string
x = '1 2 3'
x.pop()

i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'


That's because strings don't have a pop method, just like the error says.
Did you read the error? Error messages frequently tell you what the error
is.

Lists have a pop method. You can't pop from a string. If you try
to pop from a string, you will get an error. You tried to pop from a
string, and it gave an error. The error told you what you did wrong:
you tried to pop from a string. Why are you surprised?

Go back to your code. Look at the variable that you are trying to pop
from, and notice that it is a string, just like the error message says.
Now search back through your code until you find the place where you
assign a string to that variable. Don't assign a string to it.

Problem fixed.

Like I said the first time I answered your question, I think your problem
is that you are passing in a single argument string like:

"this is a list of commands"

instead of a real list like:

["this", "is", "a", "list", "of", "commands"]

As I also said the first time I answered this, you may find the split()
string method useful for creating that list. (Hint: if x is a string, you
call x.split() and get back a list.)

For future reference: any time you think you have found a bug in Python,
the chances are about 999,999 in a million that you've found a bug in your
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top