2

I've studied Python only for a short time, so I'm practising through other persons' examples. I want to do word filtering on Twitter, its Python code may be summarized as follows.

import tweepy
import simplejson as json
from imp import reload
import sys

reload(sys)

consumer_key = 'blah'
consumer_skey = 'blah'
access_tokenA = 'blah'
access_stoken = 'blah'

def get_api():
 api_key = consumer_key
 api_secret = consumer_skey
 access_token = access_tokenA
 access_token_secret = access_stoken
 auth = tweepy.OAuthHandler(api_key, api_secret)
 auth.set_access_token(access_token, access_token_secret)
 return auth

class CustomStreamListener(tweepy.StreamListener):
 def on_status(self, status):
    print ('Got a Tweet')
    self.count += 1
    tweet = status.text
    tweet = self.pattern.sub(' ',tweet)
    words = tweet.split()
    for word in words:
        if len(word) > 2 and word != '' and word not in self.common:
            if word not in self.all_words:
                self.all_words[word] = 1
            else:
                self.all_words[word] += 1

if __name__ == '__main__':
 l = CustomStreamListener()
 try:
    auth = get_api()
    s = "obamacare"
    twitterStreaming = tweepy.Stream(auth, l)
    twitterStreaming.filter(track=[s])
 except KeyboardInterrupt:
    print ('-----total tweets-----')
    print (l.count)
    json_data = json.dumps(l.all_words, indent=4)
    with open('word_data.json','w') as f:
        print >> f, json_data
        print (s)

But there is an error as follows.

File "C:/Users/ID500/Desktop/Sentiment analysis/untitled1.py", line 33, in on_status
self.count += 1

AttributeError: 'CustomStreamListener' object has no attribute 'count'

I think the version of example and my version is not correct because I already modified some parts.

What should I do?

Rachel Park
  • 47
  • 1
  • 1
  • 9

2 Answers2

1
self.count += 1

python read it as

self.count = self.count + 1

That search for self.count first then add + 1 and assign to self.count.

-=, *=, /= does similar for subtraction, multiplication, and division.

What += exactly do ??

In your code, you don't initialized self.count . to initialize count define self.count in __init_() method of class

def __init__(self)
    self.count = 0
Kallz
  • 3,244
  • 1
  • 20
  • 38
  • @RachelPark Glad to help, if this answer solved your problem please mark it as accepted by clicking the check mark next to the answer. see: How does accepting an answer work? https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Kallz Aug 27 '17 at 06:51
  • I checked the mark next to the answer, is that it? Cuz I use this stackoverflow for the first time.. – Rachel Park Aug 27 '17 at 08:21
  • @RachelPark yes, it's right to accept the answer which solves your problem. ::) – Kallz Aug 27 '17 at 08:57
0

This is because you haven't initialized the count variable either in your user-defined class CustomStreamListener() or in main program.

You can initialize it in main program and pass it to the class in such a way:

count=<some value>
class CustomStreamListener(tweepy.StreamListener):
    def __init__(self,count):
        self.count=count

    def on_status(self, status):
        print ('Got a Tweet')
        self.count += 1
       tweet = status.text
       tweet = self.pattern.sub(' ',tweet)
       words = tweet.split()
       for word in words:
           if len(word) > 2 and word != '' and word not in self.common:
               if word not in self.all_words:
                   self.all_words[word] = 1
           else:
                self.all_words[word] += 1
Shiva Gupta
  • 402
  • 1
  • 3
  • 13