Implement native numbers mode

This commit is contained in:
RunasSudo 2020-10-17 23:03:32 +11:00
parent 7d1635269a
commit 9092376998
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
5 changed files with 95 additions and 12 deletions

View File

@ -26,35 +26,35 @@ def readBLT(data):
election = Election()
# Read first line
num_candidates = int(lines[0].split(' ')[0])
election.seats = int(lines[0].split(' ')[1])
num_candidates = int(lines[0].strip().split(' ')[0])
election.seats = int(lines[0].strip().split(' ')[1])
# Read withdrawn candidates
withdrawn = []
i = 1
if lines[i].startswith("-"):
withdrawn.extend([int(x[1:]) - 1 for x in lines[i].split(" ")])
if lines[i].strip().startswith('-'):
withdrawn.extend([int(x[1:]) - 1 for x in lines[i].strip().split(' ')])
i += 1
# Read ballots
ballot_data = []
for j in range(i, len(lines)):
if lines[j] == '0': # End of ballots
if lines[j].strip() == '0': # End of ballots
break
bits = lines[j].split(' ')
bits = lines[j].strip().split(' ')
preferences = [int(x) - 1 for x in bits[1:] if x != '0']
ballot_data.append((bits[0], preferences))
# Read candidates
for k in range(j + 1, j + 1 + num_candidates):
election.candidates.append(Candidate(lines[k][1:-1]))
election.candidates.append(Candidate(lines[k].strip()[1:-1]))
# Read name
if j + 1 + num_candidates < len(lines):
election.name = lines[j + 1 + num_candidates][1:-1]
election.name = lines[j + 1 + num_candidates].strip()[1:-1]
# Any additional data?
if len(lines) > j + 2 + num_candidates and len(lines[j + 2 + num_candidates]) > 0:
if len(lines) > j + 2 + num_candidates and len(lines[j + 2 + num_candidates].strip()) > 0:
raise BLTException('Unexpected data at end of BLT file')
if len(lines) > j + 3 + num_candidates:
raise BLTException('Unexpected data at end of BLT file')

View File

@ -22,12 +22,14 @@ __pragma__('noskip')
if is_py:
__pragma__('skip')
from pyRCV2.numbers.native_py import Native
from pyRCV2.numbers.rational_py import Rational
__pragma__('noskip')
else:
from pyRCV2.numbers.native_js import Native
from pyRCV2.numbers.rational_js import Rational
_numclass = Rational
_numclass = Native
def set_numclass(cls):
global _numclass

View File

@ -0,0 +1,40 @@
# pyRCV2: Preferential vote counting
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
#
# 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 <https://www.gnu.org/licenses/>.
class Native:
def __init__(self, val):
self.impl = parseFloat(val)
def pp(self, dp):
return self.impl.toFixed(dp)
def __add__(self, other):
return Native(self.impl + other.impl)
def __sub__(self, other):
return Native(self.impl - other.impl)
def __mul__(self, other):
return Native(self.impl * other.impl)
def __div__(self, other):
return Native(self.impl / other.impl)
def __gt__(self, other):
return self.impl > other.impl
def __ge__(self, other):
return self.impl >= other.impl
def __lt__(self, other):
return self.impl < other.impl
def __le__(self, other):
return self.impl <= other.impl

View File

@ -0,0 +1,41 @@
# pyRCV2: Preferential vote counting
# Copyright © 2020 Lee Yingtong Li (RunasSudo)
#
# 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 <https://www.gnu.org/licenses/>.
class Native:
def __init__(self, val):
self.impl = float(val)
def pp(self, dp):
"""Pretty print to specified number of decimal places"""
return format(self.impl, '.{}f'.format(dp))
def __add__(self, other):
return Native(self.impl + other.impl)
def __sub__(self, other):
return Native(self.impl - other.impl)
def __mul__(self, other):
return Native(self.impl * other.impl)
def __div__(self, other):
return Native(self.impl / other.impl)
def __gt__(self, other):
return self.impl > other.impl
def __ge__(self, other):
return self.impl >= other.impl
def __lt__(self, other):
return self.impl < other.impl
def __le__(self, other):
return self.impl <= other.impl

View File

@ -89,7 +89,7 @@
elTd = document.createElement('td');
elTd.style.borderTop = '1px solid black';
if (!countCard.transfers.impl.equals(0)) {
if (countCard.transfers.pp(2) != '0.00') {
elTd.innerText = countCard.transfers.pp(2);
}
elTr1.appendChild(elTd);
@ -111,7 +111,7 @@
// Display exhausted votes
elTd = document.createElement('td');
elTd.style.borderTop = '1px solid black';
if (!result.exhausted.transfers.impl.equals(0)) {
if (result.exhausted.transfers.pp(2) != '0.00') {
elTd.innerText = result.exhausted.transfers.pp(2);
}
elExhausted1.appendChild(elTd);