''' Authors: Sohie Lee Date: Fall 2021 Purpose: Lab10, practice with dictionaries and json Filename: congress.py ------------------------------------------------------------ Data obtained from fiveThirtyEight This folder https://github.com/fivethirtyeight/data/tree/master/congress-age contains the data behind the story Both Republicans And Democrats Have an Age Problem https://fivethirtyeight.com/features/both-republicans-and-democrats-have-an-age-problem/ congress-terms.csv has an entry for every member of congress who served at any point during a particular congress between January 1947 and Februrary 2014. House membership data is from the @unitedstates project, with Congress meeting numbers added using code from GovTrack ''' # ------------------------------------------------------------ # Write your functions below here import json def getCongressDict(jsonfilename): '''Reads json file containing congress data and returns dictionary ''' try: with open(jsonfilename) as jfile: congressDict = json.load(jfile) return congressDict except FileNotFoundError: print('Oops, cannot find file', jsonfilename) except: print('Oops, encountered error reading file ' + jsonfilename + '.') def partyList(congressPeople, party): '''Returns a list of all congress people that belong to the given party''' partyPeople = [] for person in congressPeople: currentParty = congressPeople[person]['party'] if currentParty == party: partyPeople.append(person) return partyPeople def stateAndPartyList(congressPeople, party, state): '''Returns a list of all congress people that belong to the given party and given state''' people = [] for person in congressPeople: # Note that these variables make the code easier to read currentParty = congressPeople[person]['party'] currentState= congressPeople[person]['state'] if currentParty == party and currentState == state: people.append(person) return people def buildStateDict(congressPeople): '''Build new dictionary where keys are states, and values are lists of tuples of people representing that state in this format: (name, party, houseOrSenate)''' stateDict = {} for person in congressPeople: state = congressPeople[person]['state'] name = person party = congressPeople[person]['party'] hOrS = congressPeople[person]['houseOrSenate'] tup = (name, party, hOrS) if state in stateDict: stateDict[state].append(tup) else: stateDict[state] = [tup] # important that this is a list return stateDict def writeDictToJson(theDict, jsonfilename): '''Writes the given dictionary to the jsonfilename as json file ''' try: with open(jsonfilename, 'w') as jfile: json.dump(theDict, jfile) except: print('Oops, encountered error writing file ' + jsonfilename + '.') def houseSenateCountDict(congressPeople): '''Takes the congress dict and builds a new dictionary with two keys: house and senate. The values for each of those keys is another dictionary, where the keys are the states, and the values are yet another dictionary, where the keys are the parties and the values are the counts of congress members from that state with that party affiliation. Returns the houseSenateCount dictionary. ''' countDict = {} for person in congressPeople: # these variables make the code more readable state = congressPeople[person]['state'] party = congressPeople[person]['party'] hOrS = congressPeople[person]['houseOrSenate'] if hOrS in countDict: if state in countDict[hOrS]: if party in countDict[hOrS][state]: countDict[hOrS][state][party] += 1 else: countDict[hOrS][state][party] = 1 else: countDict[hOrS][state] = {party: 1} else: countDict[hOrS] = {state: {party: 1}} return countDict # Testing code congressDict = getCongressDict('congress.json') stateDict = buildStateDict(congressDict) houseSenateDict = houseSenateCountDict(congressDict)