While calculating the results for a vote today ran into an issue with
the stv_tool which had hardcoded values within it for the total number
of seats. The following patch adds argparse and makes the number of
seats as an available option
-Jake
Help print out:
usage: stv_tool.py [-h] [-s SEATS] [-v] raw_file
Calculate a winner for a vote
positional arguments:
raw_file
optional arguments:
-h, --help show this help message and exit
-s SEATS, --seats SEATS
Number of seats available, default 9
-v, --verbose Enable verbose logging
Patch:
--- /home/voter/steve/monitoring/stv_tool.py 2013-05-13
14:42:46.407723615 +0000
+++ stv_tool.py 2013-06-13 02:21:16.154038271 +0000
@@ -31,6 +31,7 @@
import os.path
import random
import ConfigParser
+import argparse
import re
ELECTED = 1
@@ -377,24 +378,35 @@
print fmt % args
-def usage():
- print 'USAGE: %s [-v] RAW_VOTES_FILE' % (os.path.basename(sys.argv[0]),)
- sys.exit(1)
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description="Calculate a winner for
a vote")
+ parser.add_argument('raw_file')
-if __name__ == '__main__':
- if len(sys.argv) < 2 or len(sys.argv) > 3:
- usage()
+ parser.add_argument("-s", "--seats", dest="seats",
+ help="Number of seats available, default 9",
+ default=9)
+
+ parser.add_argument("-v", "--verbose", dest="verbose",
action="store_true",
+ help="Enable verbose logging", default=False)
- if sys.argv[1] == '-v':
- VERBOSE = True
+ args = parser.parse_args()
+
+ VERBOSE = args.verbose
+ votefile = args.raw_file
+ num_seats = args.seats
- votefile = sys.argv[-1]
if not os.path.exists(votefile):
- usage()
+ parser.print_help()
+ sys.exit(1)
+
+ nominees = os.path.join(os.path.dirname(votefile),
'board_nominations.ini')
+ if not os.path.exists(nominees):
+ print 'Error: board_nominations.ini could not be found at %s' %
nominees
+ sys.exit(1)
names, votes = load_votes(votefile)
- ### take the count from options? (for whatif.cgi)
- run_vote(names, votes, 9)
+ run_vote(names, votes, num_seats)
print 'Done!'
+