python - KeyError after 10 hours of looping -


i following keyerror in following script after has been on 15 second loop, working 10 or hours. why key error come once in 10 hours when run every 15 seconds?

error:

traceback (most recent call last):    file "c:\venderfix.py", line 33, in <module>      if j['results']:  keyerror: 'results' 

code:

import json import urllib pprint import pprint import time arduino import arduino  vendtime = 0.2                                            delaytime = 15                                                searchterm = 'happy'                        = arduino('com3') #this need com3                a.output([12]) #output on pin 12                               counttweet = 0 #to test twitter consistancy  tweet= 0 notweet= 0    #the infinate loop while true:      #j contains json load url     j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q='+searchterm+'&result_type=recent&rpp=1&filter:retweets').read())      #debug json twitter (for faults on twitter end or possible limit id below 15 seconds per request)     #pprint(j) #needed debugging      #find text , tweet id     if j['results']:         text = j['results'][0]['text']         id = j['results'][0]['id']         #how many times json complete         tweet+= 1     else:         #how many times json incomplete (sometimes twitter malfunctions. 0.1 in 100 broken)         notweet += 1      #print text , id screen     pprint(text) #needed debugging     pprint(id)   #needed debugging      #to existing tweet before power on, if first id has been stored (count == 1)     if counttweet != 0:  #if counttweet not equal 0 it's not first tweet         #pprint ("new loop") #needed debugging          #if lastid not equal id         if lastid != id:         #tell arduino vend             #pin 12 high             a.sethigh(12)             #sleep time specified in vendtime             time.sleep(vendtime)             #pin 12 low             a.setlow(12)             #display tweet triggered vend             #pprint(text) #needed debugging             #pprint(id)   #needed debugging             #make lastid equal id next time can compare              lastid = id             #pprint ('lastid updated') #needed debugging         #if no new tweets, print              else:  #needed debugging             pprint ('no new tweets') #needed debugging     #if it's first loop, confirm printing screen     else:         pprint("first loop complete")         pprint(text)         pprint(id)         lastid = id         pprint(lastid)         counttweet += 1 #add 1 counttweet      pprint ('number of tweets')     pprint (counttweet)     pprint('working json')     pprint(tweet)     pprint('broken json')     pprint(notweet)      pprint('waiting')     time.sleep(delaytime) 

simply because in iteration, there no key results in dict.

the correct way of testing if key in dictionary doing:

if 'result' in j:      ....  

if want check if it's value not none or other falsey value, then:

if 'result' in j , j['result']:     ... 

another hypothesis, related fact you're invoking service, every , server return error message.

if happens, json structure not you're expecting, should check if docs , handle accordingly.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -