Author: gstein
Date: Thu Mar 11 23:38:29 2021
New Revision: 1887522

URL: http://svn.apache.org/viewvc?rev=1887522&view=rev
Log:
Clean up some of the vote trimming logic.

note: we'd like to sort some of this for repeatability, but there
might be slight changes in the outcome due to generate_random() values
assigned to each Candidate. more review is warranged before altering
the input sort order.

Modified:
    steve/trunk/whatif.py

Modified: steve/trunk/whatif.py
URL: 
http://svn.apache.org/viewvc/steve/trunk/whatif.py?rev=1887522&r1=1887521&r2=1887522&view=diff
==============================================================================
--- steve/trunk/whatif.py (original)
+++ steve/trunk/whatif.py Thu Mar 11 23:38:29 2021
@@ -78,18 +78,21 @@ if __name__ == '__main__':
     for name in sys.argv: names.remove(alias[name.lstrip('-').lower()])
   else:
     # only include specified candidates
+    # NOTE: order is important, so sort the names for repeatability
     names = list(map(lambda name: alias[name.lower()], set(sys.argv)))
+    ### not yet.
+    #names = [ alias[n.lower()] for n in sorted(sys.argv) ]
 
-  # limit votes only to candidates
-  for vote in votes.values():
-    for i in range(len(vote)-1,-1,-1):
-      if names.count(vote[i]) == 0: vote.pop(i)
-
-  # remove empty votes
-  for hashid in list(votes.keys()):
-    if votes[hashid] == []: del votes[hashid]
+  # Trim the raw votes based on cmdline params. Eliminate votes that
+  # are not for one of the allowed names. Do not include voters who
+  # did not vote for anybody [post-trimming].
+  trimmed = { }
+  for hashid, voteseq in votes.items():
+    newseq = [ v for v in voteseq if v in names ]
+    if newseq:
+      trimmed[hashid] = newseq
 
   # run the vote
-  candidates = stv_tool.run_vote(names, votes, seats)
+  candidates = stv_tool.run_vote(names, trimmed, seats)
   candidates.print_results()
   print('Done!')


Reply via email to