Skip to content

Instantly share code, notes, and snippets.

@kf0jvt
Created January 30, 2014 19:16
Show Gist options
  • Save kf0jvt/8716623 to your computer and use it in GitHub Desktop.
Save kf0jvt/8716623 to your computer and use it in GitHub Desktop.
def aggregateIndustry(inArray):
returnArray = [{'_id':'31-33','friendly_name':'Manufacturing','count':0},
{'_id':'44-45','friendly_name':'Retail','count':0},
{'_id':'48-49','friendly_name':'Transportation','count':0}]
for eachIndustry in inArray:
if eachIndustry['_id'] in ['31','32','33']:
returnArray[0]['count'] += eachIndustry['count']
continue
if eachIndustry['_id'] in ['44','45']:
returnArray[1]['count'] += eachIndustry['count']
continue
if eachIndustry['_id'] in ['48','49']:
returnArray[2]['count'] += eachIndustry['count']
continue
try:
eachIndustry['friendly_name'] = apiconstants.industry_remap[eachIndustry['_id']]
returnArray.append(eachIndustry)
except:
eachIndustry['friendly_name'] = 'Error'
returnArray.append(eachIndustry)
returnArray = sorted(returnArray,key=itemgetter('count'),reverse=True)
return returnArray
@krmaxwell
Copy link

For your try/except block, that will be difficult to debug and maintain and understand. If you're only trying to catch KeyErrors, just avoid them in the first place:

if eachIndustry['_id'] in apiconstants.industry_remap:
  eachIndustry['friendly_name'] = apiconstants.industry_remap[eachIndustry['_id']]
else:
  eachIndustry['friendly_name'] = 'Error'
returnArray.append(eachIndustry)

@krmaxwell
Copy link

Maybe try key=attrgetter('count')?

@krmaxwell
Copy link

You'll need to add that to your import call, naturally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment