From 5464b96398b62f1f07ea23c20d276a11227001ee Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Oct 08 2014 18:50:07 +0000 Subject: re-do user_stats stat finding using defaultdicts I keep coming back to this, but this way seems possibly slightly more efficient (doesn't require iterating over results twice) and more readable, and also doesn't use anything python 2.7-specific. It does introduce a dep on collections for defaultdict(), but that seems a pretty standard thing. --- diff --git a/relval b/relval index da9b5d3..29efabf 100755 --- a/relval +++ b/relval @@ -32,6 +32,7 @@ import getpass import sys import cgi from itertools import chain +from collections import defaultdict def setup_wiki(username=None, password=None, test=False): """Access and, if username and password are passed, log in to either the @@ -211,18 +212,16 @@ def user_stats(args): # from previous runs results = wiki.find_results(text, statuses=['pass', 'fail', 'warn'], transferred=False) - # set comprehension finds all unique users in the results. - users = {result.user for result in results if result.user} - reports = {} - bugs = {} - for user in users: - reports[user] = sum(1 for result in results if result.user == user) - # The comprehension produces a list containing some strings and some - # lists (when a single report references multiple bugs). Creating a - # set using itertools both flattens the list and removes duplicates. - # Ref: https://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python - bugs[user] = set(chain.from_iterable(result.bugs for result in results - if result.user == user and result.bugs)) + + # make two defaultdicts for number of reports and cited bugs per unique + # user. defaultdict(int) sets the value when adding a key to int(0). + reports = defaultdict(int) + bugs = defaultdict(set) + for result in results: + if result.user: + reports[result.user] += 1 + if result.bugs: + bugs[result.user].update(result.bugs) print_user_stats(reports, bugs)