Solved

Python TypeError

Posted on 2012-03-11
10
305 Views
Last Modified: 2012-03-12
Ok, so these lines usually work fine, but sometime produce type error (the bold part is producing it).
 1228         url = "http://www.google.com/" % params
 1229        [b] output = getPage("https://api-ssl.bitly.com/v3/shorten?login=xxx&apiKey=xxx&%s&format=txt"\
 1230         % urllib.urlencode({'longUrl':url}))[:22].replace("http://", "")[/b]

Open in new window


Error:
TypeError: unsubscriptable object
      args = ('unsubscriptable object',)

Also in debug report, when this error occured both params and url variables were setup fine. HELP!
0
Comment
Question by:GVNPublic123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 37706777
You should copy/paste our error message with the trace.
0
 

Author Comment

by:GVNPublic123
ID: 37706779
But I gave you traced line of code and error...what more do you need? There aint no more in report.
0
 
LVL 29

Expert Comment

by:pepr
ID: 37707704
How do you run it?  Is it a part of some web server code?  Where the 'args = ...' came from?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:GVNPublic123
ID: 37707728
No idea...its just a random python fail.
0
 

Author Comment

by:GVNPublic123
ID: 37707730
It occured, but I cant recreate it. I put exception handling for next time.
0
 
LVL 29

Expert Comment

by:pepr
ID: 37707784
How do you run your Python code?  Is it saved in a file and launched say from command line?  How do you know that the mentioned line caused the error?  What version of Python do you use?  What OS do you use?
0
 

Author Comment

by:GVNPublic123
ID: 37707813
CentOS 5, its a website, python 2.4.3

Got it from bug report.
0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 37708614
OK.  I cannot help too much here.  If you know it was the line
1229  output = getpage(...)[:22].replace("http://", "")[/b]

Open in new window


The error generally means that you try to index/slice what cannot be indexed -- i.e. the object you probably got cannot be sliced using the [:22].  It is likely that the getpage() returned None as the error result or something like that.

Try to avoid that kind of method chaining if the code is a bit complex and if it can fail:
    output = ''         # or some better initial value
    page = getpage(...)
    if page:              # or some more explicit testing
        output = page[:22].replace(...)

Open in new window

0
 

Author Comment

by:GVNPublic123
ID: 37708635
Yeh, I put exception handling in so now I dont care if slicing fails. The worker will pick it up later automatically if it fails on request.
0
 
LVL 29

Expert Comment

by:pepr
ID: 37708691
Sorry I could not be more explicit.  Thanks for the points, and have a good time ;)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question