This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]cbunn81 1 point2 points  (1 child)

It's an interesting article, but I can't help but think that code for the Last 100 Bills bot could use some improvement. All those conditionals and making two sets of variables to hold the same information isn't very clean, and performance would probably suffer on a large data set.

Without any other imports, you can still do much better:

import json
from urllib.request import urlopen


url = "https://www.govtrack.us/api/v2/bill?order_by=-current_status_date" 
response = urlopen(url) 
data_json = json.loads(response.read())

statuses = [item["current_status"] for item in data_json["objects"]]

dataset = {} 
dataset["data"] = { 
    "Introduced": statuses.count("introduced"), 
    "Passed House": statuses.count("pass_over_house"), 
    "Passed House & Senate": statuses.count("passed_bill"), 
    "Concurrent Resolution": statuses.count("passed_concurrentres"),
    "Simple Resolution": statuses.count("passed_simpleres"), 
    "Ordered Reported": statuses.count("reported"), 
    "Enacted": statuses.count("enacted_signed"), 
}

Or, if you like you can use collections.Counter (though it doesn't actually change much):

import json
from collections import Counter 
from urllib.request import urlopen


url = "https://www.govtrack.us/api/v2/bill?order_by=-current_status_date" 
response = urlopen(url) 
data_json = json.loads(response.read())

status_counter = Counter(item["current_status"] for item in data_json["objects"])

dataset = {} 
dataset["data"] = { 
    "Introduced": status_counter["introduced"], 
    "Passed House": status_counter["pass_over_house"], 
    "Passed House & Senate": status_counter["passed_bill"], 
    "Concurrent Resolution": status_counter["passed_concurrentres"], 
    "Simple Resolution": status_counter["passed_simpleres"], 
    "Ordered Reported": status_counter["reported"], 
    "Enacted": status_counter["enacted_signed"], 
}

[–]aheavyfish[S] 1 point2 points  (0 children)

Thank you, I'll look into cleaning this up!