Skip to content

Instantly share code, notes, and snippets.

@zstumgoren
Forked from zhongleqi/leqi_assignment5_draft1.py
Last active February 11, 2023 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zstumgoren/b9572983d5142380eb21171bdb71bf3d to your computer and use it in GitHub Desktop.
Save zstumgoren/b9572983d5142380eb21171bdb71bf3d to your computer and use it in GitHub Desktop.
updated with feedback
"""
*** SERDAR's NOTES ****
This is a good start. There are a few minor issues that I've noted in the code towards the top of the file,
specifically in the "main" and "group_members_by_party" functions. Please read the notes I added to the top half of the
file and try to fix the code before moving on to the sorting and printing steps.
Let me know if you have any questions.
*** END SERDAR'S NOTES ***
Here are the items I need: first name, last name, state, percentage of against votes, party.
So these are the steps I need to do:
1. get the items
2. divide the data into two groups by party
3. sort data in each group based on the percentage of against votes
4. print the result
"""
import os
import requests
import json
def main():
# Use "members" as the variable name, since
# that is more clear than "data"
members = get_members_data()
# Notice you are passing the members variable into group_members_by_party
grouped_members = group_members_by_party(members)
print(json.dumps(members, indent=4))
def get_members_data():
headers = {'X-API-Key': os.environ["PROPUBLICA_API_KEY"] }
url = "https://api.propublica.org/congress/v1/118/senate/members.json"
response = requests.get(url, headers=headers)
data = response.json()
# NOTE: Below, we dig out the list of members from the JSON and just return that list,
# rather than return the full JSON data. This will simplify the next step when
# we loop through the members and group them by party
return data['results'][0]['members']
# TODO: Look at how you are using "group_members_by_party" higher up in the
# "main" function. You are passing an argument. In order to pass members into
# this grouping function, you should specify the "members" parameter in the function definition below
def group_members_by_party():
# TODO: Good start, but you can simplify this by
# 1. Creating separate lists for democrats and republicans *before* you start looping
# 2. Adding the members to the appropriate list when looping, based on their party. You should skip Independents
for member in members ['first_name'] ['last_name'] ['state'] ['party'] ['votes_against_party_pct']:
# Looks like you're trying to use "list comprehensions" to group the data, but the syntax
# is not quite correct. A simpler/easier approach is create two lists (as described above)
# before you start looping and just add each member row to the appropriate list based on their party (D or R).
# This strategy would require the use of an "if" statement to check if the party value is a D or R.
democrat = [member in members if member['party'] == "D"]
republican = [member in members if member['party'] == "R"]
# IMPORTANT: You should return both the democrat and republican lists (a list of lists)
# at the end of this function **AFTER** the loop has finished. In other words, the "return" statement
# must be *outside* of the "for" loop. As currently written below, the return member" statement
# will cause the function to return only the first row of member data
return member
# NOTE: I've commented out/disabled the remaining code so you can focus on fixing the code above as a first step.
#def sort_percentage_d():
# for d_member in democrat:
# sorted(key=democrat('votes_against_party_pct'), reverse=True)
# return d_member[0,5]
#
#def sort_percentage_r():
# for r_member in republican:
# sorted(key=republican('votes_against_party_pct'), reverse=True)
# return r_member[0,5]
#
# I'm lost...
# am I getting a list or a dict?
#sort_percentage_d = "* {first} {last} ({state}) votes against the party {votes_against_party_pct} %of the time."
#print(sort_percentage_d.format(first = "first_name", last = "last_name", state = "state", votes_against_party_pct = "votes_against_party_pct"))
#
#sort_percentage_r = "* {first} {last} ({state}) votes against the party {votes_against_party_pct} %of the time."
#print(sort_percentage_r.format(first = "first_name", last = "last_name", state = "state", votes_against_party_pct = "votes_against_party_pct"))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment