ChocolatePi

Alaska CVR Quick Parser

Jan 2nd, 2023
1,830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | Software | 0 0
  1. import os, json
  2.  
  3. path_to_json = 'your_file_path_here' # ONLY include CvrExport_XXXX.json files in this folder!
  4. vote_data = {}
  5.  
  6. def readFiles():
  7.     for file in os.listdir(path_to_json):
  8.         f = open(path_to_json + file)
  9.         file_data = json.load(f)
  10.         f.close()
  11.        
  12.         for session in file_data["Sessions"]:
  13.             for card in session["Original"]["Cards"]:
  14.                 for contest in card["Contests"]:
  15.                     contest_id = contest["Id"]
  16.                     if contest_id not in vote_data:
  17.                         vote_data[contest_id] = []
  18.                     my_ballot = []
  19.                     prev_rank = 0
  20.                     for mark in contest["Marks"]:
  21.                         my_ballot.append(mark["CandidateId"])
  22.                         prev_rank = mark["Rank"]
  23.                     vote_data[contest_id].append(my_ballot)
  24.         print("Completed file " + str(file))
  25.  
  26. def calcPairs(contestIndex):
  27.     contest = vote_data[contestIndex]
  28.     candidateList = []
  29.     for ballot in contest:
  30.         for vote in ballot:
  31.             if vote not in candidateList:
  32.                 candidateList.append(vote)
  33.     print(candidateList)
  34.                
  35.     results = {}
  36.     for c1 in candidateList:
  37.         results[c1] = {}
  38.         for c2 in candidateList:
  39.             results[c1][c2] = 0
  40.  
  41.     for ballot in contest:
  42.         pending = candidateList.copy()
  43.         for vote in ballot:
  44.             if vote not in pending:
  45.                 break
  46.             pending.remove(vote)
  47.             for c in pending:
  48.                 results[vote][c] += 1
  49.  
  50.     return results
Advertisement
Add Comment
Please, Sign In to add comment