Implement native numbers mode
This commit is contained in:
parent
7d1635269a
commit
9092376998
@ -26,35 +26,35 @@ def readBLT(data):
|
|||||||
election = Election()
|
election = Election()
|
||||||
|
|
||||||
# Read first line
|
# Read first line
|
||||||
num_candidates = int(lines[0].split(' ')[0])
|
num_candidates = int(lines[0].strip().split(' ')[0])
|
||||||
election.seats = int(lines[0].split(' ')[1])
|
election.seats = int(lines[0].strip().split(' ')[1])
|
||||||
|
|
||||||
# Read withdrawn candidates
|
# Read withdrawn candidates
|
||||||
withdrawn = []
|
withdrawn = []
|
||||||
i = 1
|
i = 1
|
||||||
if lines[i].startswith("-"):
|
if lines[i].strip().startswith('-'):
|
||||||
withdrawn.extend([int(x[1:]) - 1 for x in lines[i].split(" ")])
|
withdrawn.extend([int(x[1:]) - 1 for x in lines[i].strip().split(' ')])
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# Read ballots
|
# Read ballots
|
||||||
ballot_data = []
|
ballot_data = []
|
||||||
for j in range(i, len(lines)):
|
for j in range(i, len(lines)):
|
||||||
if lines[j] == '0': # End of ballots
|
if lines[j].strip() == '0': # End of ballots
|
||||||
break
|
break
|
||||||
bits = lines[j].split(' ')
|
bits = lines[j].strip().split(' ')
|
||||||
preferences = [int(x) - 1 for x in bits[1:] if x != '0']
|
preferences = [int(x) - 1 for x in bits[1:] if x != '0']
|
||||||
ballot_data.append((bits[0], preferences))
|
ballot_data.append((bits[0], preferences))
|
||||||
|
|
||||||
# Read candidates
|
# Read candidates
|
||||||
for k in range(j + 1, j + 1 + num_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
|
# Read name
|
||||||
if j + 1 + num_candidates < len(lines):
|
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?
|
# 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')
|
raise BLTException('Unexpected data at end of BLT file')
|
||||||
if len(lines) > j + 3 + num_candidates:
|
if len(lines) > j + 3 + num_candidates:
|
||||||
raise BLTException('Unexpected data at end of BLT file')
|
raise BLTException('Unexpected data at end of BLT file')
|
||||||
|
@ -22,12 +22,14 @@ __pragma__('noskip')
|
|||||||
|
|
||||||
if is_py:
|
if is_py:
|
||||||
__pragma__('skip')
|
__pragma__('skip')
|
||||||
|
from pyRCV2.numbers.native_py import Native
|
||||||
from pyRCV2.numbers.rational_py import Rational
|
from pyRCV2.numbers.rational_py import Rational
|
||||||
__pragma__('noskip')
|
__pragma__('noskip')
|
||||||
else:
|
else:
|
||||||
|
from pyRCV2.numbers.native_js import Native
|
||||||
from pyRCV2.numbers.rational_js import Rational
|
from pyRCV2.numbers.rational_js import Rational
|
||||||
|
|
||||||
_numclass = Rational
|
_numclass = Native
|
||||||
|
|
||||||
def set_numclass(cls):
|
def set_numclass(cls):
|
||||||
global _numclass
|
global _numclass
|
||||||
|
40
pyRCV2/numbers/native_js.py
Normal file
40
pyRCV2/numbers/native_js.py
Normal 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
|
41
pyRCV2/numbers/native_py.py
Normal file
41
pyRCV2/numbers/native_py.py
Normal 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
|
@ -89,7 +89,7 @@
|
|||||||
|
|
||||||
elTd = document.createElement('td');
|
elTd = document.createElement('td');
|
||||||
elTd.style.borderTop = '1px solid black';
|
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);
|
elTd.innerText = countCard.transfers.pp(2);
|
||||||
}
|
}
|
||||||
elTr1.appendChild(elTd);
|
elTr1.appendChild(elTd);
|
||||||
@ -111,7 +111,7 @@
|
|||||||
// Display exhausted votes
|
// Display exhausted votes
|
||||||
elTd = document.createElement('td');
|
elTd = document.createElement('td');
|
||||||
elTd.style.borderTop = '1px solid black';
|
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);
|
elTd.innerText = result.exhausted.transfers.pp(2);
|
||||||
}
|
}
|
||||||
elExhausted1.appendChild(elTd);
|
elExhausted1.appendChild(elTd);
|
||||||
|
Reference in New Issue
Block a user