def marginals(year, cutoff, printout = True) : # This is a simple analysis which prints out the list of marginals after each # election. I.e. if you would like to know which seats were marginals going into # 1997, modulo boundary changes, ask for the marginals after 1992. The analysis # can be given a cutoff for what you would like to consider a marginal if printout : print print '------------------------------------------------------------' print 'Retreiving marginals below',cutoff+"%",'for',year print '------------------------------------------------------------' print seats = [] for constituency in outputdatabase : if constituency == "elections" : continue # First, does it exist in the given year? if not outputdatabase[constituency].has_key(year) : continue else : thismargin = getmargin(constituency,year,outputdatabase) if 100.0 * thismargin < float(cutoff): seats.append((thismargin,constituency)) seats.sort() if printout : for seat in seats : printmarginal(seat[1],year,outputdatabase) if args.verbose : niceprint(seat[1],outputdatabase,yeartoprint=year) print if printout : print print '------------------------------------------------------------' print 'Finished printout of',len(seats),'marginals below',cutoff+"%",'for',year print '------------------------------------------------------------' print return seats
def marginals_between(party1, party2, year, cutoff, printout = True) : # This is a simple analysis which prints out the list of marginals after each # election which party1 holds with respect to party2. I.e. if you would like to # know which seats were Tory-Labour marginals (held by Tories) going into # 1997, modulo boundary changes, ask for the marginals after 1992. The analysis # can be given a cutoff for what you would like to consider a marginal if printout : print print '------------------------------------------------------------' print 'Retreiving',party1+'-'+party2,'marginals below',cutoff+"%",'for',year print '------------------------------------------------------------' print seats = [] for constituency in outputdatabase : if constituency == "elections" : continue # First, does it exist in the given year? if not outputdatabase[constituency].has_key(year) : continue else : if not party1 == outputdatabase[constituency][year]["winner"]["party"] : continue if not party2 == outputdatabase[constituency][year]["second"]["party"] : continue thismargin = getmargin(constituency,year,outputdatabase) if 100.0 * thismargin < float(cutoff): seats.append((thismargin,constituency)) seats.sort() if printout : for seat in seats : printmarginal(seat[1],year,outputdatabase) if args.verbose : niceprint(seat[1],outputdatabase,yeartoprint=year) print if printout : print print '------------------------------------------------------------' print 'Finished printout of',len(seats),party1+'-'+party2,'marginals below',cutoff+"%",'for',year print '------------------------------------------------------------' print return seats
def gainbyparty(party, year, former_year = -999, printout = True) : # This analysis prints an ordered list of votes gained by a party in a given # election, with respect to the previous election. Only seats contested in both # elections are counted. At the end, a small printout is also made of the overall # totals, and the relevance of these votes to the 2015 picture in the seats # # Get the previous election year if printout : print print '--------------------------------------------------------------------------------------------------------------------------------------------------------' print 'Starting printout of votes gained by',party,'in',year print '--------------------------------------------------------------------------------------------------------------------------------------------------------' print # if former_year == -999 : former_year = str(outputdatabase["elections"][outputdatabase["elections"].index(int(year))-1]) year = str(year) former_year = str(former_year) if args.debug : print former_year, year votegains = [] alsoin2015 = 0 totalvotegain = 0 votegainsin2015 = { "Labour" : [], "Conservative" : [], "Lib Dem" : []} for constituency in outputdatabase : if constituency == "elections" : continue # Was it contested in both elections? if not (outputdatabase[constituency].has_key(former_year) and \ outputdatabase[constituency].has_key(year) ) : continue # OK they did, carry on now if args.debug : niceprint(constituency,outputdatabase) party_scores = [] # This loop works because a party only has one entry per seat per year for election in [former_year,year] : for result in possibleresults() : if party == outputdatabase[constituency][election][result]["party"] : party_scores.append(outputdatabase[constituency][election][result]["vote"]) if not len(party_scores) == 2 : continue # The votes gaines thisgain_abs = party_scores[1]-party_scores[0] # The percentage majority at the previous election thismajority = getmargin(constituency,year,outputdatabase) formermajority = getmargin(constituency,former_year,outputdatabase) votegains.append((thisgain_abs,thismajority,formermajority,constituency, outputdatabase[constituency][year]["winner"]["party"], outputdatabase[constituency][former_year]["winner"]["party"])) totalvotegain += thisgain_abs if outputdatabase[constituency].has_key('2015') : alsoin2015 += 1 for winner2015 in ['Labour','Conservative','Lib Dem'] : if outputdatabase[constituency]['2015']["winner"]["party"] in [winner2015] : votegainsin2015[winner2015].append(thisgain_abs) # # Now print it out # votegains.sort() if printout : print '--------------------------------------------------------------------------------------------------------------------------------------------------------' print "{:<35}".format("Constituency"), "{:^20}".format("Votes Gained"), "{:^20}".format(str(year)+" winner"), "{:^20}".format(str(year)+" majority"), "{:^20}".format(str(former_year)+" winner"), "{:^20}".format(str(former_year)+" majority") print '--------------------------------------------------------------------------------------------------------------------------------------------------------' for votegain in votegains : print "{:40}".format(votegain[3]),"{:>8}".format(votegain[0]), "{:>21}".format(votegain[4]), "{:>19.2%}".format(votegain[1]), "{:>21}".format(votegain[5]), "{:>19.2%}".format(votegain[2]) # print print 'There were',len(votegains),'contituencies in common between the',year,'and',former_year,'elections' print alsoin2015,'of these also existed in the 2015 election' print 'The overall vote gain for',party,'in',year,"was",totalvotegain for winner2015 in ['Labour','Conservative','Lib Dem'] : print 'The overall vote gain in the',len(votegainsin2015[winner2015]),'seats which the',winner2015,'party still holds in 2015 was',sum(votegainsin2015[winner2015]) print print '--------------------------------------------------------------------------------------------------------------------------------------------------------' print 'Finished printout of votes gained by',party,'in',year print '--------------------------------------------------------------------------------------------------------------------------------------------------------' print return alsoin2015,totalvotegain,votegainsin2015,votegains