Mercurial > hg > elinux-vote
changeset 3:b8f7fd73d44e
Separacion por funciones para impresion de datos
author | "German Poo-Caaman~o <gpoo@gnome.org>" |
---|---|
date | Thu, 07 Dec 2006 17:21:13 -0300 |
parents | 343351be18e8 |
children | ffa9fad76d38 |
files | vote-counter.py |
diffstat | 1 files changed, 84 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- a/vote-counter.py Thu Dec 07 16:37:33 2006 -0300 +++ b/vote-counter.py Thu Dec 07 17:21:13 2006 -0300 @@ -181,83 +181,97 @@ def valid_voter(addr): return valid_addresses.has_key(addr) -valid_ballots = {} +def print_error(ballots): + valid_ballots = {} -i = 0 -for b in ballots: - error = 0 - if not b.member: - error = "missing member address" - elif not b.token: - error = "missing auth token" - elif len (b.votes) > MAX_CANDIDATES: - error = "too many votes (%d votes)" % len (b.votes) - elif len (b.votes) == 0: - error = "didn't list any candidates" - elif contains_dups (b): - error = "contains duplicate votes for the same candidate" - elif md5_is_bad (b): - error = "bad authentication token" - elif not valid_voter (b.member): - error = "ballot from someone not on the list of valid voters" - else: - if valid_ballots.has_key (b.token): - old = valid_ballots[b.token] - print "Overriding previous valid ballot %d from %s with new ballot %d" %\ - (old[1], old[0].email, i) - valid_ballots[b.token] = (b, i) + i = 0 + for b in ballots: + error = 0 + if not b.member: + error = "missing member address" + elif not b.token: + error = "missing auth token" + elif len(b.votes) > MAX_CANDIDATES: + error = "too many votes (%d votes)" % len(b.votes) + elif len(b.votes) == 0: + error = "didn't list any candidates" + elif contains_dups(b): + error = "contains duplicate votes for the same candidate" + elif md5_is_bad(b): + error = "bad authentication token" + elif not valid_voter(b.member): + error = "ballot from someone not in the list of valid voters" + else: + if valid_ballots.has_key(b.token): + old = valid_ballots[b.token] + print "Overriding previous valid ballot %d from %s with " \ + "new ballot %d" % (old[1], old[0].email, i) + valid_ballots[b.token] = (b, i) - if error: - print "Ignoring ballot %d from '%s' due to: %s" % (i, b.email, error) - - i = i + 1 + if error: + print "Ignoring ballot %d from '%s' due to: %s" % (i, b.email, error) + + i = i + 1 + + return valid_ballots def tupcmp (a, b): - return cmp (a[1], b[1]) - -## Print results only after all errors have been printed, so -## we don't lose any errors. -valids = valid_ballots.values() -valids.sort(tupcmp) -for (b, i) in valids: - print "Ballot %d:" % i - - print " From: " + b.email - print " Member: " + b.member_name - print " Member Address: " + b.member - print " Token: " + b.token - print " Voted for %d candidates:" % len (b.votes) - - voted_for = [] - - valid_addresses[b.member] = 1 - - for v in b.votes: - id = v[1] - candidates[id].count = candidates[id].count + 1 - candidates[id].voters.append (b.member) - voted_for.append (candidates[id].name) - - for v in voted_for: - print " " + v - -print "The following members did not vote:" -for addr in valid_addresses.keys (): - if not valid_addresses[addr]: - print addr + return cmp(a[1], b[1]) def cmpcand(a, b): - return cmp (a.count, b.count) + return cmp(a.count, b.count) + +def print_valid_votes(valid_ballots, valid_addresses, candidates): + ## Print results only after all errors have been printed, so + ## we don't lose any errors. + valids = valid_ballots.values() + #valids.sort(tupcmp) + valids.sort(lambda a, b: cmp(a[1], b[1])) + for (b, i) in valids: + print "Ballot %d:" % i -cand_list = candidates.values() -cand_list.sort(cmpcand) + print " From: " + b.email + print " Member: " + b.member_name + print " Member Address: " + b.member + print " Token: " + b.token + print " Voted for %d candidates:" % len(b.votes) + + voted_for = [] + + valid_addresses[b.member] = 1 + + for v in b.votes: + id = v[1] + candidates[id].count = candidates[id].count + 1 + candidates[id].voters.append (b.member) + voted_for.append(candidates[id].name) -print "" -print "" -print "ELECTION RESULTS:" + for v in voted_for: + print " " + v + + return valids + +def print_no_votes(valid_addresses): + print "The following members did not vote:" + for addr in valid_addresses.keys (): + if not valid_addresses[addr]: + print addr -print " %d of %d members cast a valid ballot" \ - % (len (valids), len (valid_addresses.keys())) +def print_summary(valid_addresses, candidates, valids): + cand_list = candidates.values() + #cand_list.sort(cmpcand) + cand_list.sort(lambda a, b: cmp(a.count, b.count)) + + print "\n\nELECTION RESULTS:" -for c in cand_list: - print " %s (%d votes)" % (c.name, c.count) + print " %d of %d members cast a valid ballot" \ + % (len(valids), len(valid_addresses.keys())) + + for c in cand_list: + print " %s (%d votes)" % (c.name, c.count) + + +valid_ballots = print_error(ballots) +valids = print_valid_votes(valid_ballots, valid_addresses, candidates) +print_no_votes(valid_addresses) +print_summary(valid_addresses, candidates, valids)