Skip to content

Instantly share code, notes, and snippets.

@blackfist
Created January 30, 2014 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackfist/8716623 to your computer and use it in GitHub Desktop.
Save blackfist/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
@blackfist
Copy link
Author

So basically I get in a list of dictionaries. Each dictionary has a naics code in the '_id' field and an integer in the 'count' field. I want to add a third field called 'friendly_name' which the function will get from a dictionary called apiconstants.industry_remap.

In the case of Manufacturing, Retail, and Transportation I want to merge those industries into a single entry.

Everything works fine except that when I sort the output online 21 the count for Manufacturing, Retail, and Transportation becomes zero. If I do not sort the output, then the count is the sum of the fields as you would expect

@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