TypeError: unsubscriptable object.

B

Balaji

Hello Everybody...

I have a problem..

This is the code...
--------------------------------------
class Stack:

def __init__(self,expr):
self.stackP=[]
self.stackF=1
self.a = {}
# self.stackManagement(expr)
if not self.a.has_key('rhs'):
self.a['rhs'] = 0
# self.a['rhs'] += self.popP()*self.popF()

def pushP(self,obj):
self.stackP.append(obj)

def pushF(self,obj):
stackF=stackF*obj
return stackF

def popP(self):
rval=0
rval=self.stackP.pop()
return rval

def popF(self,obj):
stackF/=obj
return stackF

def peekP(self):
rval=0
rval= self.stackP[-1]
return rval
def peekF(self):
rval=0
rval= self.stackF[-1]
return rval
def stackManagement(self,expr):

#Determine the next symbol
if expr.__class__==E:
self.pushP(expr.operator)
self.stackManagement(expr.left)
self.stackManagement(expr.right)
else:
self.pushP(expr)
if len(self.stackP) > 1:
s = self.peekP()
prev = self.stackP[-2]
if prev in ('*','+','/','-'):
if prev in ('*','/'):
self.pushF(self.peekF()*s)
else:#stackP[-3] must be an operator.
top=self.popP()
mid =self.popP()
op=self.popP()
if op == '*':
if top.__class__==V:
self.a += self.popF(self.stackF)

self.stackManagement(0)
else:
self.popF(self.stackF)
self.stackManagement(mid*top)
if op == '+':
if top.__class__==V:
self.a+= self.peekF()

self.stackManagement(0)
else:
self.stackManagement(mid+top)
if op == '-':
if top.__class__==V:
self.a -= self.peekF()

self.stackManagement(0)
else:
self.stackManagement(mid-top)
if op == '/':
if top.__class__==V:
self.a /= self.popF(self.stackF)/(mid**2)

self.stackManagement(0)
else:
self.popF(self.stackF)
self.stackManagement(top/mid)
else:
self.a['rhs'] += self.popP(self.stackP)*self.popF(self.stackF)

and I'm giveing the following calls...
---------------------------

from expression import *
from generation import*

(x,y,z) = (V(),V(),V()) # making instances of Variable class

e1=x+y+z

s=Stack(e1)

s.stackManagement(e1)
----------------------------------------------
I'm getting the following error TypeError: unsubscriptable object...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "generation.py", line 175, in stackManagement
self.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
File "generation.py", line 199, in stackManagement
self.a+= self.peekF()
File "generation.py", line 168, in peekF
rval= self.stackF[-1]
-------------------------------------
I 'm at my wit ends..

Can anyone help...

Thx in advance
 
T

Terry Reedy

Balaji said:
class Stack:

def __init__(self,expr):

Note: tabs get eaten by some newsreaders.
self.stackP=[]
self.stackF=1 ....
def peekF(self):
rval = 0 # useless line
rval= self.stackF[-1]
return rval
# return self.stackF[-1] would be easier to read

See the potential problem? (If not, add
'print type(self.stackF)' to the top of this function.)
def stackManagement(self,expr): ....
self.a+= self.peekF()

which now seems quite possible.
....
s.stackManagement(e1)

which means you tried to subscript an unsubscriptable object
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "generation.py", line 175, in stackManagement
self.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
File "generation.py", line 199, in stackManagement
self.a+= self.peekF()
File "generation.py", line 168, in peekF
rval= self.stackF[-1]

in the line immediately above.

Terry J. Reedy
 
M

Mel Wilson

Hello Everybody...

I have a problem.. [ ... ]
----------------------------------------------
I'm getting the following error TypeError: unsubscriptable object...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "generation.py", line 175, in stackManagement
self.stackManagement(expr.left)
File "generation.py", line 176, in stackManagement
self.stackManagement(expr.right)
File "generation.py", line 199, in stackManagement
self.a+= self.peekF()
File "generation.py", line 168, in peekF
rval= self.stackF[-1]

Something has come unstuck. I would try wrapping
the failing statement:

try:
rval= self.stackF[-1]
except TypeError:
print "TypeError, self.stackF:", repr(self.stackF)
sys.exit (-1)

and see what comes out.

Regards. Mel.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top