Tweets in Python - python

def twitter_search(twitter_api,q,max_results=200,**kw):
search_results = twitter_api.search.tweets(q=q,count=100,**kw)
statuses = search_results['statuses']
max_results=min(1000,max_results)
for _ in range(10):
try:
next_results = search_results['search_metadata']['next_results']
except KeyError, e:
break
kwargs = dict([ kv.split('=')
for kv in next_results[1:].split("&") ])
search_results = twitter_api.search.tweets(**kwargs)
statuses += search_results['statuses']
if len(statuses) > max_results:
break
return statuses
results = twitter_search(twitter_api,q,max_results=10)
print json.dumps(results[0], indent =1)
The last line is returning an error that 'NoneType' object has no attribute __getitem__

You have two issues here:
The return statement is inside the for loop, and might not be reached if the loop hits one of the break statements first. If it is reached, it will return earlier than you want, without running the rest of the loop iterations.
You are assuming results will have at least one element (results[0]). If results is an empty list, this will fail with an IndexError.
Solution:
Move the return statement outside of the for loop (dedent one level).
Check if results: before indexing into it.

The problem is q not have trends :S
q = '#vladimirala1'
count = 100
search_results = twitter_api.search.tweets(q=q, count =count)
statuses = search_results['statuses']
for _ in range(5):
print "Length of statuses", len(statuses)
try:
next_results = search_results['search_metadata']['next_results']
except KeyError, e: # No mas resultados si no hay mas datos
break
kwargs = dict([kv.split('=') for kv in next_results[1:].split('&') ])
search_results = twitter_api.search.tweets(**kwargs)
statuses += search_results['statuses']
print json.dumps(statuses[0], indent=1)

Related

How to handle error in a for loop (python) and continue execution

I m trying to get my script to still print action 2 "print (list1[3])" and skip action 1 "print (list1[9])" if it can not execute it. I aplozise in advnace if my question is not clear enough I m trying to do my best to explain the issue. I m a beginner.
list1 = ['a','b','c','d','f']
try:
for i in list1:
#action 1
print (list1[9])
#action 2
print (list1[3])
break
except:
pass
Try this cleaner way
l = ['a','b','c','d','f']
# execute the function in try-except
def try_to_do(fun, index):
try:
fun(l[index])
except:
pass
for j in l:
try_to_do(print, 11)
try_to_do(print, 1)
print('Done')
Just put a try for each action instead of both actions together, like this
list1 = ['a','b','c','d','f']
for i in list1:
try:
#action 1
print (list1[9])
except IndexError:
pass
try:
#action 2
print (list1[3])
except IndexError:
pass
break
Use try except inside the loop
list1 = ['a','b','c','d','f']
for i in list1:
try:
#action 1
print (list1[9])
except:
print(e.with_traceback())
try:
#action 2
print (list1[3])
except Exception as e:
print(e.with_traceback())

python issue in anagram script

Second modification to the script (changes made below)
Made the changes mentioned in the comments (renamed all to print_all, and added the excepetion, changed the code below to reflect the same)
However, execution still exits without any reason
Initial query:
The following is a script that tries to identify various anagrams of a word (as in the website: http://wordsmith.org/anagram/anagram.cgi?anagram=suchindra&t=1000&a=n):
import sys
import itertools
import threading
from collections import defaultdict
words_dict = defaultdict(lambda: "")
def lower_and_nocrlf(s):
return s.lower().strip()
def two_or_more(s):
if len(s) >= 1:
return 1
else:
return 0
def get_perms(cur_iter):
lst = []
for i in range(0, 10000):
try:
lst.append("".join(cur_iter.next()))
except:
break
return lst
def get_twordlist(z):
lst1 = []
lst2 = []
for i in range (1, len(z)):
lst1.append(z[:i])
lst2.append(z[i:])
return lst1, lst2
def filter_dict(x):
if x in words_dict.keys():
return x
else:
return 0
def main():
print_all = None
word = None
try:
word = sys.argv[1]
print_all = sys.argv[2]
except:
pass
if word == None:
try:
word = sys.stdin.readline()
print_all = sys.stdin.readline()
except:
pass
if word == None:
sys.exit(1)
fd = open('/usr/apps/words', 'r')
words = fd.readlines()
fd.close()
words_lower = map(lower_and_nocrlf, words)
words_lower = filter(two_or_more, words_lower)
from collections import defaultdict
for i in words_lower:
words_dict[i] = ""
iter = itertools.permutations(word)
all_permutations = []
iters = []
for i in range(0, 100):
iters.append(iter)
result = map(get_perms, iters)
main_list = []
for l in result:
if l != []:
for word in l:
main_list.append(word)
results = []
try:
main_list_len = len(main_list)
for index in range(0, main_list_len):
percent = (index/len(main_list)) * 100
lst1, lst2 = get_twordlist(main_list[index])
result1 = map(filter_dict, lst1)
result2 = map(filter_dict, lst2)
for index in range(0, len(result1)):
if (result1[index] != 0) and (result2[index] != 0):
results.append("%s %s" % (result1[index], result2[index]))
except KeyboardInterrupt:
print("User stopped execution, partial results:")
print results
sys.exit(1)
except Exception:
# catches all other types of exception here
print(sys.exc_info())
traceback.print_exc()
print(results)
if __name__ == "__main__":
try:
main()
except:
sys.exit(0)
So, your code is clearly executing down to the print index line and then failing somewhere inside the block. The exception handler only catches exceptions of type KeyboardInterrupt - i.e. when a user presses Ctl+C on their keyboard. Any other error will exit via the sys.exit(0) method, so you have no way of knowing what the error is.
Personally I quite like the traceback module for these printing out errors, so I suggest you modify your try catch block to be something like:
import traceback
try:
main_list_len = len(main_list)
print main_list_len
for index in range(0, main_list_len):
print index
percent = (index/len(main_list)) * 100
lst1, lst2 = get_twordlist(main_list[index])
result = map(filter_dict, lst1, lst2)
results.append[result]
except KeyboardInterrupt:
print("User stopped execution, partial results:")
print("Exception: %s" % (sys.exc_info()))
print results
except Exception:
# catches all other types of exception here
traceback.print_exc()
This will allow you to debug the problem as the traceback module will give you line numbers and an error message to work with.
Good luck!
OK after some analysis, it looks like filter does not accept more than one list. The second issue is because I am using multiple lists in the filter

Tweepy: Rotate API object in the middle of Cursor iteration

So basically I'm trying to obtain the followers of a user with many of them. I'm using tweepy for this and I can't simply wait the 60 seconds every time I hit the limit or this will take too long. So this is my approach, to me it makes sense as it s keeping track of next_cursor (which is initialized as -1):
while getting_followers:
try:
follower_cursors = tweepy.Cursor(api.followers_ids, id=userid, next_cursor=current_cursor)
for follower_cursor in follower_cursors.pages():
temp_list = temp_list + follower_cursor
print "Getting Followers..."
getting_followers = False
except Exception as e:
print "Error obtaining node followers"
print e
if str(e) == "Not authorized.":
print "Not authorized, skipping.", str(userid)
getting_followers = False
elif str(e) == "[{'message': 'Sorry, that page does not exist.', 'code': 34}]":
print e
getting_followers = False
else:
print "Changing accounts."
api = rotateAccounts()
current_cursor = follower_cursors.iterator.next_cursor
the problem is this doesn't work, the Cursor restarts every time I rotate api, it doesn't remember the current cursor

Stopiteration identfying the iterable object

I have 2 generator and using the next method & while loop to process thru it as below,
code
while end_of_loop = 'n':
try:
table1_row = next(table1_generator)
table2_row = next(table2_generator)
except StopIteration:
end_of_loop = 'y'
How do I identify which iterator object has no rows?
Im trying to compare 2 tables and each table rows are in generator objects.
def _compare(self):
end_of_table = 'N'
try:
while end_of_table =='N':
try:
if self.table1_key == self.table2_key:
print 'same key'
self.table1_row = next(self.table1_generator)
self._convert_table1_key_fields()
self.table2_row = next(self.table2_generator)
self._convert_table2_key_fields()
elif self.table1_key > self.table2_key:
self.table2_row = next(self.table1_generator)
self._convert_table2_key_fields()
elif self.table1_key < self.table2_key:
self.table1_row = next(self.table2_generator)
self._convert_table1_key_fields()
except StopIteration as e:
print e.args
print 'IterError'
end_of_table = 'y'
except StopIteration:
print 'Next Iteration'
You can provide a second "sentinel" value to next:
sentinel = object()
while end_of_loop = 'n':
table1_row = next(table1_generator,sentinel)
table2_row = next(table2_generator,sentinel)
#I don't account for the case where they could be exhausted at
#the same time. It's easy to add that if it matters though.
if table1_row is sentinel:
print "table1_generator gave up first"
#"break" might be more appropriate ...
#(Unless there more to the loop than this.)
end_of_loop = 'y'
elif table2_row is sentinel:
print "table2_generator gave up first"
end_of_loop = 'y'
You didn't provide details about the context or what you're actually trying to do, but it sounds like itertools.izip_longest might be useful in your case (and more pythonic). You can fill with Nones or another value which is suitable.
I can't think of a use case for this (care to share?), but I'd try wrapping next into my own function:
def mynext(it):
try:
return next(it)
except StopIteration:
raise StopIteration(it)
and then, for example,
a = iter([1,2,3,4,5])
b = iter([4,5,6,7])
try:
while True:
print mynext(a)
print mynext(b)
except StopIteration as e:
if e.args[0] == a: print 'a ended'
if e.args[0] == b: print 'b ended'

If else code help in try/ except block

SomeDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'}
try:
result = ""
theKey = raw_input("Enter some key: ")
val = someDict[theKey]
except keyErrorr:
result "hello"
else:
result = result + "" + "done"
print result
I understand the try block you can insert and code to try and see what error comes up, and the error then can be caught by the except block. I am trying to figure out the best way to insert a if / else in the try and except block for the same key error that is present in this code. I was thinking that i could just replace the try and except with If/else or is it possible to just add a if/else in the try and except. Any help on how to insert a if/else into this code for key error would be greatly appreciated. So basically i want to add a if/else code into the try and except block for the same key error.
SomeDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'}
try:
result = "" #could i insert something like if result == "" : #for this line?
theKey = raw_input("Enter some key: ")
val = someDict[theKey]
except keyErrorr:
result "hello"
else:
result = result + "" + "done"
print result
One reasonable option is to initialize result = None, then test if result is None:.
It's better to use None than the empty string, since someday you might want a dictionary value to be the empty string, plus None is probably clearer to the casual reader of your code.
You could also just skip the try-except, and use if theKey in someDict:.
you can add another except without a specification what exception it should handle.
try:
# do something
except KeyError:
# do something because of the Keyerror
except:
# do what you need to do if the exception is not a KeyError
someDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'} # corrected dict name
result = ""
theKey = raw_input("Enter some key: ")
try: # just try the code where the error could be
val = someDict[theKey]
except KeyError: # corrected exception name and indent level
result = "hello" # corrected syntax
else: # corrected indent level
result = result + "" + "done" # why add "" ?
print result
does this work for you?

Categories

Resources