def person_session_stats_overview(request, session, cohort, specific_stat): from person.views_sessionstats import get_cohort_name, stat_titles try: stats = Person.load_session_stats(session) except ValueError: # no stats raise Http404() if specific_stat is not None and specific_stat not in stat_titles: # no stats raise Http404() try: cohort_title = get_cohort_name(cohort, True) if cohort else None except ValueError: # invalid URL raise Http404() # Get all of the cohorts in the data. cohorts = {} cohort_keys = set() for person in stats["people"].values(): for c in person["cohorts"]: c = c["key"] cohorts[c] = cohorts.get(c, 0) + 1 cohort_keys.add(c) cohorts = [(-v, k, get_cohort_name(k, True), v) for k, v in cohorts.items() if "delegation" not in k and v > 10] cohorts = sorted(cohorts) # Gather data. metrics = {} for pid, person in stats["people"].items(): try: personobj = Person.objects.get(id=int(pid)) except: # debugging continue for stat, statinfo in person["stats"].items(): if specific_stat is not None and stat != specific_stat: continue for cohort_key, context in statinfo.get("context", {}).items(): # filter by cohort, if we're doing that if cohort is not None and cohort != cohort_key: continue if cohort is None and cohort_key not in ("house", "senate"): continue # create an entry for this statistic metrics.setdefault( stat, { "key": stat, "title": stat_titles[stat]["title"], "superlatives": stat_titles[stat]["superlatives"], "icon": stat_titles[stat]["icon"], "contexts": {} }) metrics[stat]["title"] = metrics[stat]["title"].replace( "{{other_chamber}}", "Other Chamber") metrics[stat]["contexts"].setdefault( cohort_key, { "key": cohort_key, "title": get_cohort_name(cohort_key, True), "N": context["N"], "people": ([], []), }) # if this person ranks #1, #2, #3, fill him in c = metrics[stat]["contexts"][cohort_key]["people"] if specific_stat is not None: c[0].append((context["rank_descending"], statinfo["value"], personobj)) elif context["rank_ties"] <= 3: if context["rank_ascending"] < 3: c[1].append((context["rank_descending"], statinfo["value"], personobj)) elif context["rank_descending"] < 3: c[0].append((context["rank_descending"], statinfo["value"], personobj)) metrics = sorted(metrics.values(), key=lambda m: m["title"]) for m in metrics: m["contexts"] = sorted(m["contexts"].values(), key=lambda c: -c["N"]) for c in m["contexts"]: c["people"][0].sort( key=lambda v: (v[0], v[2].sortname)) # sort by rank, then by name c["people"][1].sort(key=lambda v: (v[0], v[2].sortname)) import dateutil.parser return { "session": session, "period": session_stats_period(session, stats), "meta": stats["meta"], "metrics": metrics, "cohorts": cohorts, "cohort": cohort, "cohort_title": cohort_title, "specific_stat": specific_stat, "specific_stat_title": stat_titles[specific_stat]["title"].replace( "{{other_chamber}}", "Other Chamber") if specific_stat else None, "publishdate": dateutil.parser.parse(stats["meta"]["as-of"]), }
statinfo = stat_titles[statname] tweet += statinfo["verb"][0].lower() + " " + statinfo["verb"][1] + " " if groupinfo["rank_ascending"] == 1: tweet += statinfo["superlatives"][1] elif groupinfo["rank_descending"] == 1: tweet += statinfo["superlatives"][0] elif groupinfo["rank_ascending"] < group_info["rank_descending"]: tweet += "?th " + statinfo["superlatives"][1] else: tweet += "?th " + statinfo["superlatives"][0] tweet += " " + statinfo["verb"][2] + " " tweet = tweet.replace(" ", " ") tweet += "out of " + get_cohort_name(cohortname).replace("All ", "all ") if allstats["meta"]["is_full_congress_stats"]: tweet += " last Congress" else: tweet += " in " + str(allstats["meta"]["session"]) if groupinfo["rank_ties"] > 0: tweet += " (tied w/ %d)" % groupinfo["rank_ties"] tweet += ".\n" tweet += "https://www.govtrack.us" + person.get_absolute_url() + "/report-card/" + str(allstats["meta"]["session"]) print(tweet.encode("utf8")) print()
def person_session_stats_overview(request, session, cohort, specific_stat): try: stats = Person.load_session_stats(session) except ValueError: # no stats raise Http404() from person.views_sessionstats import get_cohort_name, stat_titles try: cohort_title = get_cohort_name(cohort, True) if cohort else None except ValueError: # invalid URL raise Http404() # Get all of the cohorts in the data. cohorts = { } cohort_keys = set() for person in stats["people"].values(): for c in person["cohorts"]: c = c["key"] cohorts[c] = cohorts.get(c, 0) + 1 cohort_keys.add(c) cohorts = [ (-v, k, get_cohort_name(k, True), v) for k,v in cohorts.items() if "delegation" not in k and v > 10] cohorts = sorted(cohorts) # Gather data. metrics = { } for pid, person in stats["people"].items(): try: personobj = Person.objects.get(id=int(pid)) except: # debugging continue for stat, statinfo in person["stats"].items(): if specific_stat is not None and stat != specific_stat: continue for cohort_key, context in statinfo.get("context", {}).items(): # filter by cohort, if we're doing that if cohort is not None and cohort != cohort_key: continue if cohort is None and cohort_key not in ("house", "senate"): continue # create an entry for this statistic metrics.setdefault(stat, { "key": stat, "title": stat_titles[stat]["title"], "superlatives": stat_titles[stat]["superlatives"], "icon": stat_titles[stat]["icon"], "contexts": { } }) metrics[stat]["title"] = metrics[stat]["title"].replace("{{other_chamber}}", "Other Chamber") metrics[stat]["contexts"].setdefault(cohort_key, { "key": cohort_key, "title": get_cohort_name(cohort_key, True), "N": context["N"], "people": ([], []), }) # if this person ranks #1, #2, #3, fill him in c = metrics[stat]["contexts"][cohort_key]["people"] if specific_stat is not None: c[0].append( (context["rank_descending"], statinfo["value"], personobj) ) elif context["rank_ties"] <= 3: if context["rank_ascending"] < 3: c[1].append( (context["rank_descending"], statinfo["value"], personobj) ) elif context["rank_descending"] < 3: c[0].append( (context["rank_descending"], statinfo["value"], personobj) ) metrics = sorted(metrics.values(), key = lambda m : m["title"]) for m in metrics: m["contexts"] = sorted(m["contexts"].values(), key = lambda c : -c["N"]) for c in m["contexts"]: c["people"][0].sort() c["people"][1].sort() #from person.views_sessionstats import clean_person_stats #for pid, personstats in stats["people"].items(): # clean_person_stats(personstats) import dateutil.parser return { "session": session, "period": session_stats_period(session, stats), "meta": stats["meta"], "metrics": metrics, "cohorts": cohorts, "cohort": cohort, "cohort_title": cohort_title, "specific_stat": specific_stat, "specific_stat_title": stat_titles[specific_stat]["title"].replace("{{other_chamber}}", "Other Chamber") if specific_stat else None, "publishdate": dateutil.parser.parse(stats["meta"]["as-of"]), }
statinfo = stat_titles[statname] tweet += statinfo["verb"][0].lower() + " " + statinfo["verb"][1] + " " if groupinfo["rank_ascending"] == 1: tweet += statinfo["superlatives"][1] elif groupinfo["rank_descending"] == 1: tweet += statinfo["superlatives"][0] elif groupinfo["rank_ascending"] < group_info["rank_descending"]: tweet += "?th " + statinfo["superlatives"][1] else: tweet += "?th " + statinfo["superlatives"][0] tweet += " " + statinfo["verb"][2] + " " tweet = tweet.replace(" ", " ") tweet += "out of " + get_cohort_name(cohortname).replace("All ", "all ") if allstats["meta"]["is_full_congress_stats"]: tweet += " last Congress" else: tweet += " in " + str(allstats["meta"]["session"]) if groupinfo["rank_ties"] > 0: tweet += " (tied w/ %d)" % groupinfo["rank_ties"] tweet += ". " tweet += "https://www.govtrack.us" + person.get_absolute_url() + "/report-card/" + session print(tweet) print()