Example #1
0
def swing(party1, party2, year, former_year = -999, printout = True) :
  # This analysis prints an ordered list of swings from party1 to party2 in a given
  # election, with respect to the previous election. Only seats contested in both
  # elections are counted. 
  #
  # The swing is defined as half the change between the score differences of the parties
  # from the first election to the second one
  #
  # Get the previous election year
  if printout :
    print
    print '-------------------------------------------------------------------------------------------------'
    print 'Starting printout of swings from',party1,"to",party2,'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
  swings = []
  for constituency in outputdatabase :
    if constituency == "elections" : continue    
    # Was it contested in both elections?
    if not (outputdatabase[constituency].has_key(year)        and \
            outputdatabase[constituency].has_key(former_year) ) :
      continue
    # Have to see if the parties both contested the constituency in both elections
    passedchecks = True
    for election in [former_year,year] :
      parties_in_year = []
      for result in possibleresults() :
        parties_in_year.append(outputdatabase[constituency][election][result]["party"])
      if not (party1 in parties_in_year and party2 in parties_in_year) :
        passedchecks = False
    if not passedchecks : continue
    # OK they did, carry on now
    if args.debug : 
      niceprint(constituency,outputdatabase)
    party1_scores = []
    party2_scores = []
    # This loop works because each party only has one entry per seat per year
    for election in [former_year,year] :
      for result in possibleresults() :
        if party1 == outputdatabase[constituency][election][result]["party"] :
          party1_scores.append(outputdatabase[constituency][election][result]["vote"])
        elif party2 == outputdatabase[constituency][election][result]["party"] :
          party2_scores.append(outputdatabase[constituency][election][result]["vote"])       
    # The percentage swing
    if args.debug : print constituency, outputdatabase[constituency][year]
    thisswing = 100.0*(party2_scores[1]-party1_scores[1])/(outputdatabase[constituency][year]["electorate"]*outputdatabase[constituency][year]["turnout"]) - \
                100.0*(party2_scores[0]-party1_scores[0])/(outputdatabase[constituency][former_year]["electorate"]*outputdatabase[constituency][former_year]["turnout"])
    # The absolute swing
    thisswing_abs = (party2_scores[1]-party1_scores[1]) - (party2_scores[0]-party1_scores[0])
    # The percentage majority at the previous election
    thismajority = 100.0*(party2_scores[0]-party1_scores[0])/(outputdatabase[constituency][former_year]["electorate"]*outputdatabase[constituency][former_year]["turnout"])
    swings.append((thisswing/2.,thisswing_abs/2.,thismajority,constituency))
  #
  # Now print it out
  #
  swings.sort()
  if printout :
    totalvoteswing = 0
    avgpercswing   = 0
    print '-------------------------------------------------------------------------------------------------'
    print "{:<35}".format("Constituency"), "{:^20}".format("Percentage swing"), "{:^20}".format("Absolute swing"), "{:^20}".format(str(year)+" majority")
    print '-------------------------------------------------------------------------------------------------'
    for swing in swings :
      print "{:40}".format(swing[3]),"{:>8.2%}".format(swing[0]), "{:>21}".format(swing[1]), "{:>19.2%}".format(swing[2])
      totalvoteswing += swing[1]
      avgpercswing   += swing[0]
    #
    avgpercswing /= len(swings)
    print
    print 'The average swing from',party1,"to",party2,'in',year,"was","{:0.2%}".format(avgpercswing)
    print 'The overall vote swing from',party1,"to",party2,'in',year,"was",totalvoteswing
    print
    print '-------------------------------------------------------------------------------------------------'
    print 'Finished printout of swings from',party1,"to",party2,'in',year
    print '-------------------------------------------------------------------------------------------------'
    print
  return swings
Example #2
0
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