This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

JoeNotCharles

By: a guest on Jan 2nd, 2011  |  syntax: Python  |  size: 2.55 KB  |  views: 165  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. import sys
  3.  
  4. # map of candidate names to candidate numbers
  5. candidates = {}
  6.  
  7. # the highest candidate number seen so far
  8. maxCandidateNum = 0
  9.  
  10. # contents of the ballots file
  11. ballots = []
  12.  
  13. # open ballots.txt for writing
  14. with open("ballots.txt", "w") as ballotFile:
  15.  
  16.     # loop through all command-line args except the first (the program name)
  17.     for filename in sys.argv[1:]:
  18.  
  19.         # start an empty ballot for this file
  20.         ballot = []
  21.  
  22.         # open the file for reading and loop through all lines in it
  23.         with open(filename, "r") as currentFile:
  24.             for line in currentFile:
  25.  
  26.                 # the current candidate is the line with all leading and trailing whitespace stripped
  27.                 candidateName = line.strip()
  28.  
  29.                 # if the candidate has already been seen, get its number
  30.                 # otherwise, assign a new number to it
  31.                 if candidateName in candidates:
  32.                     candidateNum = candidates[candidateName]
  33.                 else:
  34.                     maxCandidateNum += 1
  35.                     candidateNum = maxCandidateNum
  36.                     candidates[candidateName] = candidateNum
  37.  
  38.                 # add the candidate number to the current ballot
  39.                 ballot.append(candidateNum)
  40.  
  41.         # write the current ballot, as one line with > between each candidate number
  42.         ballotString = " > ".join(str(num) for num in ballot)
  43.         ballots.append(ballotString)
  44.  
  45.     # write the -cands and -tie parameters
  46.     candsString = " ".join(str(num) for num in sorted(candidates.values()))
  47.     ballotFile.write("-cands " + candsString + "\n\n")
  48.     tieString = " ".join(str(candidates[name]) for name in sorted(candidates.keys()))
  49.     ballotFile.write("-tie " + tieString + "\n\n")
  50.  
  51.     # write all the ballots to the ballot file
  52.     for ballot in ballots:
  53.         ballotFile.write(ballot + "\n\n")
  54.  
  55. # now that we've read all candidates from all files, write candidates.txt
  56. with open("candidates.txt", "w") as candidateFile:
  57.  
  58.     # define a comparison function which, given two candidate names, returns the one with the lower candidate number
  59.     def candidateNumberSort(name1, name2):
  60.         num1 = candidates[name1]
  61.         num2 = candidates[name2]
  62.         return cmp(num1, num2)
  63.  
  64.     # use this function to loop through the candidate map in order of number, and write the name
  65.     candidateNames = candidates.keys()
  66.     for candidateName in sorted(candidateNames, candidateNumberSort):
  67.         candidateFile.write(candidateName + "\n")
clone this paste RAW Paste Data