Skip to content

Instantly share code, notes, and snippets.

@cooncesean
Created November 30, 2011 00:29
Show Gist options
  • Save cooncesean/1407334 to your computer and use it in GitHub Desktop.
Save cooncesean/1407334 to your computer and use it in GitHub Desktop.
Find the total number of EditorialReviews by platform and the average score given per platform/developer.
developers = {}
for er in EditorialReview.objects.all():
print er
for dtg in er.game.developers.all():
if dtg.company.name in developers:
_dict = developers[dtg.company.name]
_dict['total_reviews'] += 1
_dict['total_review_score'] += er.score
_dict['avg_review_score'] = '%.1f' % (float(_dict['total_review_score']) / float(_dict['total_reviews']))
else:
developers.update({
dtg.company.name:{
'total_reviews': 1,
'total_review_score':er.score,
'avg_review_score': er.score
}
})
print developers
platforms = {}
for er in EditorialReview.objects.all():
print er
for platform in er.platforms.all():
if platform.name in platforms:
platform_dict = platforms[platform.name]
platform_dict['total_reviews'] += 1
platform_dict['total_review_score'] += er.score
platform_dict['avg_review_score'] = '%.1f' % (float(platform_dict['total_review_score']) / float(platform_dict['total_reviews']))
else:
platforms.update({
platform.name:{
'total_reviews': 1,
'total_review_score':er.score,
'avg_review_score': er.score
}
})
print platforms
platforms = {}
for er in EditorialReview.objects.all():
print er
for platform in er.platforms.all():
if platform.name in platforms:
platform_dict = platforms[platform.name]
platform_dict['total_reviews'] += 1
platform_dict['total_review_score'] += er.score
platform_dict['avg_review_score'] = '%.1f' % (float(platform_dict['total_review_score']) / float(platform_dict['total_reviews']))
else:
platforms.update({
platform.name:{
'total_reviews': 1,
'total_review_score':er.score,
'avg_review_score': er.score
}
})
print platforms
@cooncesean
Copy link
Author

Can I use setdefault() to either speed this up?

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