Last active
December 25, 2015 17:29
-
-
Save mike-anderson/7013364 to your computer and use it in GitHub Desktop.
Electoral District parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Election: | |
# ed_data: [district_name, record] | |
ed_data = None | |
# va_data: {district_name: [record]} | |
va_data = None | |
def __init__(self,ed_data_,va_data_): | |
self.ed_data = ed_data_ | |
self.va_data = va_data_ | |
for district in self.ed_data.values(): | |
self.ed_data[district[2]][0] = set(district[0][51:-55].split(' ')) | |
for districtId in self.va_data: | |
for area in self.va_data[districtId]: | |
area[0] = set(area[0][51:-55].split(' ')) | |
def adjacencies(self): | |
regions = {} | |
connectedRegions = {} | |
for district in self.ed_data.values(): | |
for otherRegion in regions.values(): | |
test = self.ed_data[district[2]][0].intersection(self.ed_data[otherRegion[2]][0]) | |
if len(test) > 1: | |
try: | |
connectedRegions[district[2]].append(otherRegion[2]) | |
except: | |
connectedRegions[district[2]] = [otherRegion[2]] | |
try: | |
connectedRegions[otherRegion[2]].append(district[2]) | |
except: | |
connectedRegions[otherRegion[2]] = [district[2]] | |
regions[district[2]] = district | |
return connectedRegions | |
''' | |
Computes the adjacent voting areas along the border of the specified electoral district, and all adjacent electoral districts. | |
edOfInterest: District name to check for adjacency. | |
connectedEds: List of adjacent ED names. | |
Output: {votingAreaId: [(connectedVotingAreaId, connectedAreaEDName)]} | |
For each voting area in the selected electoral district, output a list of adjacent voting areas. | |
Voting areas are identified as a tuple, so the electoral district name can be easily retrieved. | |
''' | |
def adjacent_va(self, edOfInterest, connectedEds): | |
adjacentVotingAreas = {} | |
for connectedEd in connectedEds: # est ~2-5 | |
for connectedVotingArea in self.va_data[connectedEd]: # est ~100 | |
for votingArea in self.va_data[edOfInterest]: # est ~100 | |
if self.isAdjacent(votingArea, connectedVotingArea): | |
try: | |
adjacentVotingAreas[votingArea[4]].append((connectedVotingArea[4], connectedEd)) | |
except: | |
adjacentVotingAreas[votingArea[4]] = [(connectedVotingArea[4], connectedEd)] | |
return adjacentVotingAreas | |
''' | |
Tests whether two specified voting areas are adjacent. | |
''' | |
def isAdjacent(self, votingArea, connectedVotingArea): | |
test = votingArea[0].intersection(connectedVotingArea[0]) | |
return len(test) > 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
import Election | |
csv.field_size_limit(sys.maxsize) | |
def main(): | |
try: | |
targetParty = sys.argv[3] | |
except: | |
targetParty = 'NDP' | |
electoralDistrictInfo = {} | |
votingAreaInfo = {} | |
file = open(sys.argv[1]) | |
reader = csv.reader(file) | |
reader.next() | |
for row in reader: | |
electoralDistrictInfo[row[2]] = row | |
file.close() | |
file = open(sys.argv[2]) | |
reader = csv.reader(file) | |
reader.next() | |
for row in reader: | |
try: | |
votingAreaInfo[row[3]].append(row) | |
except: | |
votingAreaInfo[row[3]] = [row] | |
file.close() | |
ed = Election.Election(electoralDistrictInfo,votingAreaInfo) | |
connectedRegions = ed.adjacencies() | |
ridingsOfInterest = [] | |
print "possible swing ridings, with adjacent ridings" | |
for region in connectedRegions: | |
if electoralDistrictInfo[region][13] != targetParty and int(electoralDistrictInfo[region][14]) < 50: | |
print region + ':' + electoralDistrictInfo[region][13] + ' ' + electoralDistrictInfo[region][14] | |
ridingsOfInterest.append(region) | |
for adjacentRegion in connectedRegions[region]: | |
print ' ' + str(adjacentRegion) + ': ' + str(electoralDistrictInfo[adjacentRegion][13]) + ' ' + str(electoralDistrictInfo[adjacentRegion][14]) | |
swingRiding = ridingsOfInterest[0] | |
swingRidingVotingAreas = votingAreaInfo[swingRiding] | |
swingRidingInfo = electoralDistrictInfo[swingRiding] | |
swingRidingAdjacents = connectedRegions[swingRiding] | |
swingRidingAdjacentVotingAreas = {} | |
for riding in swingRidingAdjacents: | |
swingRidingAdjacentVotingAreas[riding] = votingAreaInfo[riding] | |
adjacentVotingAreas = ed.adjacent_va(swingRiding, swingRidingAdjacents) | |
print adjacentVotingAreas | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment