0

I'm new to Python. I'm writing a simple class but I'm with an error.

My class:

import config   # Ficheiro de configuracao
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python

class TwitterC:
    def logToDatabase(self, tweet, timestamp):
        # Will log to the database
        database = sqlite3.connect('database.db') # Create a database file
        cursor   = database.cursor() # Create a cursor
        cursor.execute("CREATE TABLE IF NOT EXISTS twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT);") # Make a table
        # Assign the values for the insert into
        msg_ins       = tweet
        timestamp_ins = timestamp
        values        = [msg_ins, timestamp_ins]
        # Insert data into the table
        cursor.execute("INSERT INTO twitter(tweet, timestamp) VALUES(?, ?)", values)
        database.commit() # Save our changes
        database.close() # Close the connection to the database

    def shortUrl(self, url):
        bit = bitly_api.Connection(config.bitly_username, config.bitly_key) # Instanciar a API
        return bit.shorten(url) # Encurtar o URL

    def updateTwitterStatus(self, update): 
        short = self.shortUrl(update["url"]) # Vou encurtar o URL
        update = update["msg"] + short['url']
        # Will post to twitter and print the posted text
        twitter_api = twitter.Api(consumer_key=config.twitter_consumer_key, 
                          consumer_secret=config.twitter_consumer_secret, 
                          access_token_key=config.twitter_access_token_key, 
                          access_token_secret=config.twitter_consumer_secret)
        status = twitter_api.PostUpdate(update) # Fazer o update
        msg    = status.text # Vou gravar o texto enviado para a variavel 'msg'
        # Vou gravar p a Base de Dados
        self.logToDatabase(msg, time.time())
        print msg

x = TwitterC()
x.updateTwitterStatus([{"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "}])

The error is:

Traceback (most recent call last):
  File "C:\Documents and Settings\anlopes\workspace\redes_soc\src\twitterC.py", line 42, in <module>
    x.updateTwitterStatus([{"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "}])
  File "C:\Documents and Settings\anlopes\workspace\redes_soc\src\twitterC.py", line 28, in updateTwitterStatus
    short = self.shortUrl(update["url"]) # Vou encurtar o URL
TypeError: list indices must be integers, not str

Any clues on how to solve it?

Best Regards,

1
  • remember you can also upvote good answers (like I just did for Steve), and if it's checkmarked, I presume it's good. :) Commented Mar 31, 2011 at 21:01

2 Answers 2

1

It looks like your call to updateTwitterStatus just needs to lose the square brackets:

 x.updateTwitterStatus({"url": "http://xxxx.com/?cat=31", "msg": "See some strings..., "})

You were passing a list with a single dictionary element. It looks as though the method just requires a dictionary with "url" and "msg" keys.

In Python, {...} creates a dictionary, and [...] creates a list.

Sign up to request clarification or add additional context in comments.

Comments

0

The error message tells you everything you need to know. It says "list indices must be integers, not str" and points to the code short = self.shortUrl(update["url"]). So obviously the python interpreter thinks update is a list, and "url" is not a valid index into the list.

Since update is passed in as a parameter we have to see where it came from. It looks like [{...}], which means it's a list with a single dictionary inside. Presumably you intended to pass just the dictionary, so remove the square brackets when calling x.updateTwitterStatus

The first rule of debugging is to assume that the error message is correct, and that you should take it literally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.