BLT editor: Basic BLT import functionality
This commit is contained in:
parent
b6f3f56280
commit
b109d99054
@ -28,8 +28,8 @@
|
|||||||
<input type="file" id="inpFile" style="display: none;" onchange="changeInpFile()">
|
<input type="file" id="inpFile" style="display: none;" onchange="changeInpFile()">
|
||||||
<button onclick="clickOpenJSON()">Open JSON</button>
|
<button onclick="clickOpenJSON()">Open JSON</button>
|
||||||
<button onclick="clickSaveJSON()">Save JSON</button>
|
<button onclick="clickSaveJSON()">Save JSON</button>
|
||||||
<!--<button>Import BLT</button>
|
<button onclick="clickImportBLT()">Import BLT</button>
|
||||||
<button>Export BLT</button>-->
|
<!--<button>Export BLT</button>-->
|
||||||
<!--GITREV-->
|
<!--GITREV-->
|
||||||
<a href="https://yingtongli.me/blog/2020/12/24/pyrcv2.html">Information and instructions</a>
|
<a href="https://yingtongli.me/blog/2020/12/24/pyrcv2.html">Information and instructions</a>
|
||||||
</div>
|
</div>
|
||||||
@ -55,6 +55,7 @@
|
|||||||
<div>Warning: Adding, removing or reordering candidates once ballots have been input may result in unexpected behaviour.</div>
|
<div>Warning: Adding, removing or reordering candidates once ballots have been input may result in unexpected behaviour.</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script src="../bundle.js?v=GITVERSION"></script>
|
||||||
<script src="index.js?v=GITVERSION"></script>
|
<script src="index.js?v=GITVERSION"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -106,20 +106,59 @@ function changeBallot() {
|
|||||||
tblBallot.querySelector('input').select();
|
tblBallot.querySelector('input').select();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let openMode = 'json';
|
||||||
|
|
||||||
function clickOpenJSON() {
|
function clickOpenJSON() {
|
||||||
|
openMode = 'json';
|
||||||
|
inpFile.value = '';
|
||||||
|
inpFile.click();
|
||||||
|
}
|
||||||
|
|
||||||
|
function clickImportBLT() {
|
||||||
|
openMode = 'blt';
|
||||||
inpFile.value = '';
|
inpFile.value = '';
|
||||||
inpFile.click();
|
inpFile.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function changeInpFile() {
|
async function changeInpFile() {
|
||||||
if (inpFile.value !== '') {
|
if (inpFile.value !== '') {
|
||||||
// Open JSON file
|
if (openMode === 'json') {
|
||||||
let text = await inpFile.files[0].text();
|
// Open JSON file
|
||||||
let obj = JSON.parse(text);
|
let text = await inpFile.files[0].text();
|
||||||
|
let obj = JSON.parse(text);
|
||||||
|
|
||||||
// Load data
|
// Load data
|
||||||
candidates = obj['candidates'];
|
candidates = obj['candidates'];
|
||||||
ballots = obj['ballots'];
|
ballots = obj['ballots'];
|
||||||
|
} else if (openMode === 'blt') {
|
||||||
|
// Open BLT file
|
||||||
|
let text = await inpFile.files[0].text();
|
||||||
|
|
||||||
|
// Import data
|
||||||
|
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.StringNum);
|
||||||
|
let election = py.pyRCV2.blt.readBLT(text);
|
||||||
|
|
||||||
|
candidates = [];
|
||||||
|
for (let candidate of election.candidates) {
|
||||||
|
candidates.push(candidate.py_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
ballots = [];
|
||||||
|
for (let ballot of election.ballots) {
|
||||||
|
let js_ballot = {'value': ballot.value.impl, 'preferences': []};
|
||||||
|
for (let i = 0; i < candidates.length; i++) {
|
||||||
|
js_ballot['preferences'].push('');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add preference numbers
|
||||||
|
for (let i = 0; i < ballot.preferences.length; i++) {
|
||||||
|
candidate = ballot.preferences[i];
|
||||||
|
js_ballot['preferences'][election.candidates.indexOf(candidate)] = '' + (i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ballots.push(js_ballot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update ballot entry
|
// Update ballot entry
|
||||||
initBallot();
|
initBallot();
|
||||||
@ -133,8 +172,9 @@ async function changeInpFile() {
|
|||||||
elOption.innerText = 'Ballot ' + (i + 1);
|
elOption.innerText = 'Ballot ' + (i + 1);
|
||||||
selBallots.insertBefore(elOption, selBallots.selectedOptions[0]);
|
selBallots.insertBefore(elOption, selBallots.selectedOptions[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inpFile.value = '';
|
||||||
}
|
}
|
||||||
inpFile.value = '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function clickSaveJSON() {
|
function clickSaveJSON() {
|
||||||
|
@ -34,6 +34,7 @@ else: # pragma: no cover
|
|||||||
from pyRCV2.numbers.gfixed_js import FixedGuarded, _gfixed_set_dps
|
from pyRCV2.numbers.gfixed_js import FixedGuarded, _gfixed_set_dps
|
||||||
from pyRCV2.numbers.native_js import Native
|
from pyRCV2.numbers.native_js import Native
|
||||||
from pyRCV2.numbers.rational_js import Rational
|
from pyRCV2.numbers.rational_js import Rational
|
||||||
|
from pyRCV2.numbers.string_js import StringNum
|
||||||
|
|
||||||
# GLOBALS
|
# GLOBALS
|
||||||
|
|
||||||
|
@ -16,7 +16,11 @@
|
|||||||
|
|
||||||
from pyRCV2.numbers.base import BaseNum, compatible_types
|
from pyRCV2.numbers.base import BaseNum, compatible_types
|
||||||
|
|
||||||
Big.DP = 6
|
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
|
||||||
|
Big.DP = 6
|
||||||
|
else:
|
||||||
|
# Fail gracefully if dependencies not present
|
||||||
|
pass
|
||||||
|
|
||||||
def _fixed_set_dps(dps):
|
def _fixed_set_dps(dps):
|
||||||
Big.DP = dps
|
Big.DP = dps
|
||||||
|
@ -16,9 +16,13 @@
|
|||||||
|
|
||||||
from pyRCV2.numbers.base import BaseNum, compatible_types
|
from pyRCV2.numbers.base import BaseNum, compatible_types
|
||||||
|
|
||||||
Big.DP = 12
|
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
|
||||||
_pos_epsilon = Big('10').pow(-6).div('2')
|
Big.DP = 12
|
||||||
_neg_epsilon = _pos_epsilon.times('-1')
|
_pos_epsilon = Big('10').pow(-6).div('2')
|
||||||
|
_neg_epsilon = _pos_epsilon.times('-1')
|
||||||
|
else:
|
||||||
|
# Fail gracefully if dependencies not present
|
||||||
|
pass
|
||||||
|
|
||||||
def _gfixed_set_dps(dps):
|
def _gfixed_set_dps(dps):
|
||||||
global _pos_epsilon, _neg_epsilon
|
global _pos_epsilon, _neg_epsilon
|
||||||
|
28
pyRCV2/numbers/string_js.py
Normal file
28
pyRCV2/numbers/string_js.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# pyRCV2: Preferential vote counting
|
||||||
|
# Copyright © 2020–2021 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/>.
|
||||||
|
|
||||||
|
from pyRCV2.numbers.base import BaseNum
|
||||||
|
|
||||||
|
class StringNum(BaseNum):
|
||||||
|
"""
|
||||||
|
Dummy class wrapping simple strings
|
||||||
|
Used when reading/writing BLTs with no arithmetic required - avoids loading dependencies
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _to_impl(cls, value):
|
||||||
|
"""Implements BaseNum._to_impl"""
|
||||||
|
return value
|
@ -14,8 +14,14 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
|
||||||
|
_MAX_VAL = bigInt(2).pow(256).subtract(1)
|
||||||
|
else:
|
||||||
|
# Fail gracefully if dependencies not present
|
||||||
|
_MAX_VAL = None
|
||||||
|
|
||||||
class SHARandom:
|
class SHARandom:
|
||||||
MAX_VAL = bigInt(2).pow(256).subtract(1)
|
MAX_VAL = _MAX_VAL
|
||||||
|
|
||||||
def __init__(self, seed):
|
def __init__(self, seed):
|
||||||
self.seed = seed
|
self.seed = seed
|
||||||
|
Reference in New Issue
Block a user