refact: replaced all try on key by the get function to have a cleaner

code
pull/12/head
Sami Mokaddem 2017-11-29 16:34:02 +01:00
parent d0c72fe214
commit ae4e828beb
1 changed files with 18 additions and 44 deletions

View File

@ -119,17 +119,11 @@ def handler_sighting(zmq_name, jsondata):
jsonsight = jsondata['Sighting'] jsonsight = jsondata['Sighting']
org = jsonsight['Event']['Orgc']['name'] org = jsonsight['Event']['Orgc']['name']
categ = jsonsight['Attribute']['category'] categ = jsonsight['Attribute']['category']
try: action = jsondata.get('action', None)
action = jsondata['action']
except KeyError:
action = None
contributor_helper.handleContribution(zmq_name, org, 'Sighting', categ, action, pntMultiplier=2) contributor_helper.handleContribution(zmq_name, org, 'Sighting', categ, action, pntMultiplier=2)
handler_attribute(zmq_name, jsonsight, hasAlreadyBeenContributed=True) handler_attribute(zmq_name, jsonsight, hasAlreadyBeenContributed=True)
try: timestamp = jsonsight.get('date_sighting', None)
timestamp = jsonsight['date_sighting']
except KeyError:
pass
if jsonsight['type'] == "0": # sightings if jsonsight['type'] == "0": # sightings
trendings_helper.addSightings(timestamp) trendings_helper.addSightings(timestamp)
@ -144,13 +138,12 @@ def handler_event(zmq_name, jsonobj):
eventName = jsonevent['info'] eventName = jsonevent['info']
timestamp = jsonevent['timestamp'] timestamp = jsonevent['timestamp']
trendings_helper.addTrendingEvent(eventName, timestamp) trendings_helper.addTrendingEvent(eventName, timestamp)
try: tags = []
temp = jsonobj['EventTag'] for tag in jsonobj.get('EventTag', []):
tags = [] try:
for tag in temp:
tags.append(tag['Tag']) tags.append(tag['Tag'])
except KeyError: except KeyError:
tags = [] pass
trendings_helper.addTrendingTags(tags, timestamp) trendings_helper.addTrendingTags(tags, timestamp)
#redirect to handler_attribute #redirect to handler_attribute
@ -164,18 +157,9 @@ def handler_event(zmq_name, jsonobj):
else: else:
handler_attribute(zmq_name, attributes) handler_attribute(zmq_name, attributes)
try: action = jsonobj.get('action', None)
action = jsonobj['action'] eventLabeled = len(jsonobj.get('EventTag', [])) > 0
except KeyError: org = jsonobj.get('Orgc', {}).get('name', None)
action = None
try:
eventLabeled = len(jsonobj['EventTag']) > 0
except KeyError:
eventLabeled = False
try:
org = jsonobj['Orgc']['name']
except KeyError:
org = None
if org is not None: if org is not None:
contributor_helper.handleContribution(zmq_name, org, contributor_helper.handleContribution(zmq_name, org,
@ -191,18 +175,14 @@ def handler_attribute(zmq_name, jsonobj, hasAlreadyBeenContributed=False):
#Add trending #Add trending
categName = jsonattr['category'] categName = jsonattr['category']
try: timestamp = jsonattr.get('timestamp', int(time.time()))
timestamp = jsonattr['timestamp']
except KeyError:
timestamp = int(time.time())
trendings_helper.addTrendingCateg(categName, timestamp) trendings_helper.addTrendingCateg(categName, timestamp)
try: tags = []
temp = jsonattr['Tag'] for tag in jsonattr.get('Tag', []):
tags = [] try:
for tag in temp:
tags.append(tag['Tag']) tags.append(tag['Tag'])
except KeyError: except KeyError:
tags = [] pass
trendings_helper.addTrendingTags(tags, timestamp) trendings_helper.addTrendingTags(tags, timestamp)
to_push = [] to_push = []
@ -225,14 +205,8 @@ def handler_attribute(zmq_name, jsonobj, hasAlreadyBeenContributed=False):
geo_helper.getCoordFromPhoneAndPublish(jsonattr['value'], jsonattr['category']) geo_helper.getCoordFromPhoneAndPublish(jsonattr['value'], jsonattr['category'])
if not hasAlreadyBeenContributed: if not hasAlreadyBeenContributed:
try: eventLabeled = len(jsonobj.get('EventTag', [])) > 0
eventLabeled = len(jsonattr['Tag']) > 0 action = jsonobj.get('action', None)
except KeyError:
eventLabeled = False
try:
action = jsonobj['action']
except KeyError:
action = None
contributor_helper.handleContribution(zmq_name, jsonobj['Event']['Orgc']['name'], contributor_helper.handleContribution(zmq_name, jsonobj['Event']['Orgc']['name'],
'Attribute', 'Attribute',
jsonattr['category'], jsonattr['category'],