Skip to content

Instantly share code, notes, and snippets.

@mariot
Created March 9, 2017 11:21
Show Gist options
  • Select an option

  • Save mariot/3a0aaa99e45392ff2ad5144d421d97c5 to your computer and use it in GitHub Desktop.

Select an option

Save mariot/3a0aaa99e45392ff2ad5144d421d97c5 to your computer and use it in GitHub Desktop.
The good, the bad, the ugly
campaign_details = campaign_details_daily(args.get('campaign_id'), args.get('month'), args.get('year'), args.get('include_offline'))
final_output = []
ads = []
""" put each campaign details in an array """
for cd in campaign_details:
campaign_detail = CampaignDetail(cd.get("impressions"), cd.get("clicks"), cd.get("date"), cd.get("uniques"))
ads.append(AdBundle(cd.get("ad_id"), cd.get("ad_name"), cd.get("name"), campaign_detail))
""" divide the array by ad_id """
groups = defaultdict(list)
for obj in ads:
groups[obj.ad_id].append(obj)
list_of_details_by_id = groups.values()
""" for each ad in the new divided array """
for detail in list_of_details_by_id:
output = []
""" put the details in a bundle """
for ab in detail:
output.append(ab.campaign_details)
ad_bundle = CampaignDetailBundle(detail[0].ad_id, detail[0].ad_name, detail[0].campaign_name, output)
final_output.append(ad_bundle)
return final_output
campaign_details = campaign_details_daily(args.get('campaign_id'), args.get('month'), args.get('year'), args.get('include_offline'))
ad_bundles = {}
for cd in campaign_details:
ad_id = cd['ad_id']
if ad_id in ad_bundles:
ad_bundle = ad_bundles[ad_id]
else:
ad_bundle = AdBundle(cd['ad_id'], cd['ad_name'], cd['name'], [])
ad_bundles[ad_id] = ad_bundle
ad_bundle.campaign_details.append(CampaignDetail(cd.get("impressions"), cd.get("clicks"), cd.get("date"), cd.get("uniques")))
return ad_bundles.values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment