Пример #1
0
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
Пример #2
0
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
Пример #3
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
Пример #4
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
Пример #5
0
args = parser.parse_args()

# This is a simple example script to read back and dump the database written
# by the builddb script. It also lets you print the results for a single constituency
# for all the years in order.

try :
  outputdatabase = shelve.open(args.input)
except :
  print 'No shelve database found at the given location, exiting'
  sys.exit(0)

# Let's print the result out in a nice way! 
if args.constituency != "" : 
  if outputdatabase.has_key(args.constituency) :
    niceprint(args.constituency,outputdatabase)
  else :  
    print
    print 'The constituency you requested does not exist in the database'
    print 'As the database is derived from electoral commission data, it'
    print 'may be that the name is simply in a different format to the'
    print 'one you expected. Here is the alphabetical list of all the'
    print 'constituencies which I know about.'
    print 
    keys = []
    for key in outputdatabase :
      if key == "elections" : continue
      keys.append(key)
    keys.sort()
    for key in keys : print key
#