473,513 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeError: unsubscriptable object.

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
Jul 18 '05 #1
2 3528

"Balaji" <ba****@email.arizona.edu> wrote in message
news:49**************************@posting.google.c om...
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)
----------------------------------------------
I'm getting the following error TypeError: unsubscriptable object...
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


Jul 18 '05 #2
In article <49**************************@posting.google.com >,
ba****@email.arizona.edu (Balaji) wrote:
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]
-------------------------------------
I 'm at my wit ends..

Can anyone help...


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.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13053
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
0
3603
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm just transforming a Python module into Pyrex, and I get the following error: File "indicadorPyrex.pyx", line 37, in...
5
22718
by: Randall Parker | last post by:
Using Python 2.4.2 on Windows 2000 in SPE. Getting: TypeError: 'str' object is not callable on this line: TmpErrMsg1 = "State machine %s " (StateMachineName) In Winpdb 1.0.6 the...
1
1706
by: Gary Wessle | last post by:
dear python users I am not sure why I am getting **************************************************************** Traceback (most recent call last): File "my.py", line 3, in ?...
9
80753
by: k.retheesh | last post by:
Can anybody tell me why am I getting this error message while trying to print a part of a string. Is there a better approach for this... Traceback (most recent call last): File...
10
11473
by: Charles Russell | last post by:
Why does this work from the python prompt, but fail from a script? How does one make it work from a script? #! /usr/bin/python import glob # following line works from python prompt; why not in...
2
6698
by: AWasilenko | last post by:
I'm trying to test a few different approaches to displaying pages via Cherrypy and I'm not having much luck. Here is my code so far: import sys, cherrypy, html class Root: @cherrypy.expose...
33
56382
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
0
1773
by: Chris Rebert | last post by:
On Fri, Nov 14, 2008 at 10:40 AM, Indian <write2abdul@gmail.comwrote: You don't need and shouldn't be using `exec` here. It appears as though you're just doing the following in a much more obtuse...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.