def tweet_votes_yday(self, if_major): # Tweet count of votes yesterday, by vote type if there were any major votes. from vote.models import Vote, VoteCategory votes = Vote.objects.filter( created__gte=timezone.now().date() - timedelta(days=1), created__lt=timezone.now().date(), ) if votes.count() == 0: return has_major = len([v for v in votes if v.is_major]) > 0 if not has_major and if_major: return if not has_major: count = votes.count() msg = "%d minor vote%s held by Congress yesterday." % ( count, "s were" if count != 1 else " was", ) else: counts = defaultdict(lambda: 0) for v in votes: counts[v.category] += 1 counts = list(counts.items()) counts.sort(key=lambda kv: (VoteCategory.by_value(kv[0]).importance, -kv[1])) msg = "Votes held by Congress yesterday: " + ", ".join( str(value) + " on " + VoteCategory.by_value(key).label for key, value in counts) self.post_tweet("%s:votes" % timezone.now().date().isoformat(), msg, "https://www.govtrack.us/congress/votes")
def tweet_votes_yday(self, if_major): # Tweet count of votes yesterday, by vote type if there were any major votes. from vote.models import Vote, VoteCategory votes = Vote.objects.filter( created__gte=timezone.now().date()-timedelta(days=1), created__lt=timezone.now().date(), ) if votes.count() == 0: return has_major = len([v for v in votes if v.is_major]) > 0 if not has_major and if_major: return if not has_major: msg = "%d vote%s held by Congress yesterday." % ( votes.count(), "s were" if count != 1 else " was", ) else: counts = defaultdict(lambda : 0) for v in votes: counts[v.category] += 1 counts = list(counts.items()) counts.sort(key = lambda kv : (VoteCategory.by_value(kv[0]).importance, -kv[1])) msg = "Votes held by Congress yesterday: " + ", ".join( str(value) + " on " + VoteCategory.by_value(key).label for key, value in counts ) self.post_tweet( "%s:votes" % timezone.now().date().isoformat(), msg, "https://www.govtrack.us/congress/votes")
else: state_pop_year = { state: pop for (state, year), pop in state_pop.items() if year == pop_year } # Sum up the total population represented by each vote option # (i.e. yes/no), # apportioning the population of each state evenly across the legislators representing # that state who voted. Also just count total yes/no/not voting votes. pop_by_option = defaultdict(lambda : 0) count_by_option = defaultdict(lambda : 0) for option, state, personid in votes: if state == "": continue # Vice President's tie-breaker if state not in state_pop_year: continue # one of the island territories, for which we don't have population data pop_by_option[option] += state_pop_year[state] / legislators_per_state[state] count_by_option[option] += 1 # Skip votes where the ayes were in the minority. Those votes represent failed outcomes. We're # only interested in the legitimacy of actual outcomes. if "+" not in pop_by_option or count_by_option["+"] < count_by_option.get("-", 0): continue # get the % of yes voters and % of the country's population represented by those voters count_percent_yea = count_by_option["+"]/(count_by_option["+"] + count_by_option.get("-", 0)) pop_percent_yea = pop_by_option["+"]/(pop_by_option["+"] + pop_by_option.get("-", 0)) # of those voting pop_percent_yea_of_total = pop_by_option["+"]/sum(pop_by_option.values()) # of sworn senators # print W.writerow([vote.congress, vote.session if len(vote.session) == 4 else vote.created.year, vote.created.isoformat(), vote.chamber_name, VoteCategory.by_value(vote.category).key, str(vote), percent(count_percent_yea), percent(pop_percent_yea), percent(pop_percent_yea_of_total), "https://www.govtrack.us"+vote.get_absolute_url()])
# Get the total U.S. population in this time. Interpolate (or beyond the last year, extrapolate). country_population = us_pop_at(pop_year) # Sum up the total population represented by each vote option # (i.e. yes/no), # apportioning the population of each state evenly across the legislators representing # that state who voted. Also just count total yes/no/not voting votes. pop_by_option = defaultdict(lambda : 0) count_by_option = defaultdict(lambda : 0) for option, state, personid in votes: if state == "": continue # Vice President's tie-breaker if option not in ("+", "-"): continue # we only care about voting legislators pop_by_option[option] += state_pop_year[state] / voting_legislators_per_state[state] count_by_option[option] += 1 # Skip votes where the ayes were in the minority. Those votes represent failed outcomes. We're # only interested in the legitimacy of actual outcomes. if "+" not in pop_by_option or count_by_option["+"] < count_by_option.get("-", 0): continue # get the % of yes voters out of senators voting and out of the country's population as a whole count_percent_yea = count_by_option["+"] / (count_by_option["+"] + count_by_option.get("-", 0)) statepop_percent_yea = pop_by_option["+"] / state_pop_total uspop_percent_yea = pop_by_option["+"] / country_population # print W.writerow([vote.congress, vote.session if len(vote.session) == 4 else vote.created.year, vote.created.isoformat(), VoteCategory.by_value(vote.category).key, str(vote), percent(count_percent_yea), percent(statepop_percent_yea), percent(uspop_percent_yea), "https://www.govtrack.us"+vote.get_absolute_url()])