BLT editor: Basic BLT import functionality

This commit is contained in:
RunasSudo 2021-01-25 20:25:32 +11:00
parent b6f3f56280
commit b109d99054
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
7 changed files with 100 additions and 16 deletions

View File

@ -28,8 +28,8 @@
<input type="file" id="inpFile" style="display: none;" onchange="changeInpFile()">
<button onclick="clickOpenJSON()">Open JSON</button>
<button onclick="clickSaveJSON()">Save JSON</button>
<!--<button>Import BLT</button>
<button>Export BLT</button>-->
<button onclick="clickImportBLT()">Import BLT</button>
<!--<button>Export BLT</button>-->
<!--GITREV-->
<a href="https://yingtongli.me/blog/2020/12/24/pyrcv2.html">Information and instructions</a>
</div>
@ -55,6 +55,7 @@
<div>Warning: Adding, removing or reordering candidates once ballots have been input may result in unexpected behaviour.</div>
</div>
<script src="../bundle.js?v=GITVERSION"></script>
<script src="index.js?v=GITVERSION"></script>
</body>
</html>

View File

@ -106,13 +106,23 @@ function changeBallot() {
tblBallot.querySelector('input').select();
}
let openMode = 'json';
function clickOpenJSON() {
openMode = 'json';
inpFile.value = '';
inpFile.click();
}
function clickImportBLT() {
openMode = 'blt';
inpFile.value = '';
inpFile.click();
}
async function changeInpFile() {
if (inpFile.value !== '') {
if (openMode === 'json') {
// Open JSON file
let text = await inpFile.files[0].text();
let obj = JSON.parse(text);
@ -120,6 +130,35 @@ async function changeInpFile() {
// Load data
candidates = obj['candidates'];
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
initBallot();
@ -133,9 +172,10 @@ async function changeInpFile() {
elOption.innerText = 'Ballot ' + (i + 1);
selBallots.insertBefore(elOption, selBallots.selectedOptions[0]);
}
}
inpFile.value = '';
}
}
function clickSaveJSON() {
let result = {

View File

@ -34,6 +34,7 @@ else: # pragma: no cover
from pyRCV2.numbers.gfixed_js import FixedGuarded, _gfixed_set_dps
from pyRCV2.numbers.native_js import Native
from pyRCV2.numbers.rational_js import Rational
from pyRCV2.numbers.string_js import StringNum
# GLOBALS

View File

@ -16,7 +16,11 @@
from pyRCV2.numbers.base import BaseNum, compatible_types
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
Big.DP = 6
else:
# Fail gracefully if dependencies not present
pass
def _fixed_set_dps(dps):
Big.DP = dps

View File

@ -16,9 +16,13 @@
from pyRCV2.numbers.base import BaseNum, compatible_types
if __pragma__('js', '{}', 'typeof(bigInt)') != 'undefined':
Big.DP = 12
_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):
global _pos_epsilon, _neg_epsilon

View 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

View File

@ -14,8 +14,14 @@
# 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/>.
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:
MAX_VAL = bigInt(2).pow(256).subtract(1)
MAX_VAL = _MAX_VAL
def __init__(self, seed):
self.seed = seed