Eos/eos/base/util/blt.py

51 lines
1.9 KiB
Python
Raw Normal View History

2017-12-15 23:05:49 +11:00
# Eos - Verifiable elections
# pyRCV - Preferential voting counting
2021-10-16 22:12:22 +11:00
# Copyright © 2016-2021 RunasSudo (Yingtong Li)
2017-12-15 23:05:49 +11:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def writeBLT(election, q_num, seats, withdrawn=[]):
question = election.questions[q_num]
flat_choices = question.flatten_choices()
electionLines = []
2017-12-16 21:47:17 +11:00
electionLines.append('{} {}'.format(len(flat_choices), seats))
2017-12-15 23:05:49 +11:00
if len(withdrawn) > 0:
2017-12-16 21:47:17 +11:00
electionLines.append(' '.join(['-{}'.format(flat_choices.index(candidate) + 1) for candidate in withdrawn]))
2017-12-15 23:05:49 +11:00
2017-12-17 12:46:53 +11:00
result_obj = election.results[q_num]
if result_obj._name == 'eos.base.election.MultipleResult':
result_obj = next(x for x in result_obj.results if x._name == 'eos.base.election.RawResult')
result = result_obj.count()
2017-12-15 23:05:49 +11:00
for answer, count in result:
if answer.choices:
2017-12-16 21:47:17 +11:00
electionLines.append('{} {} 0'.format(count, ' '.join(str(x + 1) for x in answer.choices)))
2017-12-15 23:05:49 +11:00
else:
2017-12-16 21:47:17 +11:00
electionLines.append('{} 0'.format(count))
2017-12-15 23:05:49 +11:00
2017-12-16 21:47:17 +11:00
electionLines.append('0')
2017-12-15 23:05:49 +11:00
for candidate in flat_choices:
if candidate.party:
2021-10-16 22:12:22 +11:00
electionLines.append('"{}{}"'.format(candidate.name, candidate.party))
2017-12-15 23:05:49 +11:00
else:
2021-10-16 22:12:22 +11:00
electionLines.append('"{}"'.format(candidate.name))
2017-12-15 23:05:49 +11:00
2021-10-16 22:12:22 +11:00
electionLines.append('"{}{}"'.format(election.name, question.prompt))
2017-12-15 23:05:49 +11:00
return electionLines