1

The github link for the code is given below:

https://github.com/AlchemyAPI/alchemyapi-recipes-twitter

I get the following error when I run recipe.py:

Traceback (most recent call last):
  File "recipe.py", line 340, in <module>
    main(sys.argv[1], int(sys.argv[2]))
  File "recipe.py", line 43, in main
    print_results()
  File "recipe.py", line 303, in print_results
    avg_pos_score = mean_results['result'][2]['avgScore'] 
TypeError: 'CommandCursor' object has no attribute '__getitem__'

I am using python version 2.7.6 Please do help me out to solve this.

HelloWorld
  • 76
  • 1
  • 9

2 Answers2

2

Yeah, I finally got the correct output.Thanks to Games Brainiac for helping me to figure it out.

    mean_results = list(tweets.aggregate([{"$group" : {"_id": "$sentiment",   

   "avgScore" : { "$avg" : "$score"}}}]))
    avg_pos_score = mean_results[1]['avgScore'] 
    avg_neg_score = mean_results[0]['avgScore']

The mean_results will contain a list of dictionary entities(in this case 3 entities-neg,pos,neutral). So mean_results[0] refers to the negative entity. mean_results[1] refers to the positive entity. and so on. mean_results[1]['avgScore]=avg score of the positive entity. and so on...

HelloWorld
  • 76
  • 1
  • 9
1

I think you need to change line 301 to 304 to reflect the new changes in the API.

Firstly, change this line:

mean_results = tweets.aggregate([{"$group" : {"_id": "$sentiment", "avgScore" : { "$avg" : "$score"}}}])

to

mean_results = list(tweets.agg....)

So now, you no longer need to use the result for the CommandCursor.

Instead, what you have to do is this:

list(mean_results[2]['avgScore'])

Instead, and repeat with the next line too. Just remove the result part.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • The CommandCursor error is no more there but now it says float object is not iterable! Traceback (most recent call last): File "recipe.py", line 343, in main(sys.argv[1], int(sys.argv[2])) File "recipe.py", line 43, in main print_results() File "recipe.py", line 306, in print_results avg_pos_score = list(mean_results[2]['avgScore']) TypeError: 'float' object is not iterable – HelloWorld Apr 17 '15 at 14:33
  • No.It is not solved.Please check the comment for the details...:) – HelloWorld Apr 17 '15 at 14:40
  • try printing out what `mean_results` give you, and edit your question. – Games Brainiac Apr 17 '15 at 14:41
  • Thank you...It was of great help...:) – HelloWorld Apr 17 '15 at 15:27
  • It is a pity that when projects upgrade to newer versions, they break backwards compatibility! When we upgraded our servers from ubuntu 12 (uses python 2.7.3) to ubuntu 14 (uses python 2.7.6) our code started breaking! – arun May 06 '15 at 18:13