Fix bug in JS implementation of exclusive Gregory

Remove dependency on Transcrypt's itertools.groupby as this does not appear to work
This commit is contained in:
RunasSudo 2021-01-03 02:05:49 +11:00
parent 3b9a98fabd
commit 891f82e148
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
7 changed files with 43 additions and 2 deletions

View File

@ -15,12 +15,38 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
__pragma__ = lambda x: None __pragma__ = lambda x: None
is_py = False
__pragma__('skip')
is_py = True
__pragma__('noskip')
from pyRCV2.model import CandidateState, CountCard, CountCompleted, CountStepResult from pyRCV2.model import CandidateState, CountCard, CountCompleted, CountStepResult
from pyRCV2.numbers import Num from pyRCV2.numbers import Num
from pyRCV2.safedict import SafeDict from pyRCV2.safedict import SafeDict
import itertools # Stubs for JS
def groupby(iterable, keyfunc):
if is_py:
__pragma__('skip')
import itertools
return [list(g) for k, g in itertools.groupby(iterable, keyfunc)]
__pragma__('noskip')
else:
groups = []
group = []
last_result = None
for i in iterable:
this_result = keyfunc(i)
__pragma__('opov')
if last_result is not None and this_result != last_result:
__pragma__('noopov')
groups.append(group)
group = []
last_result = this_result
group.append(i)
if group:
groups.append(group)
return groups
class STVException(Exception): class STVException(Exception):
pass pass
@ -569,7 +595,7 @@ class WIGSTVCounter(BaseSTVCounter):
ballots.sort(key=lambda x: x[1] / x[0].value, reverse=True) ballots.sort(key=lambda x: x[1] / x[0].value, reverse=True)
# Round to 8 decimal places to consider equality # Round to 8 decimal places to consider equality
# FIXME: Work out a better way of doing this # FIXME: Work out a better way of doing this
count_card.parcels = [list(g) for k, g in itertools.groupby(ballots, lambda x: (x[1] / x[0].value).round(8, x[1].ROUND_DOWN))] count_card.parcels = groupby(ballots, lambda x: (x[1] / x[0].value).round(8, x[1].ROUND_DOWN))
__pragma__('noopov') __pragma__('noopov')
if len(count_card.parcels) > 0: if len(count_card.parcels) > 0:

View File

@ -55,6 +55,8 @@ class Fixed:
def __eq__(self, other): def __eq__(self, other):
return self.impl.eq(other.impl) return self.impl.eq(other.impl)
def __ne__(self, other):
return not self.impl.eq(other.impl)
def __gt__(self, other): def __gt__(self, other):
return self.impl.gt(other.impl) return self.impl.gt(other.impl)
def __ge__(self, other): def __ge__(self, other):

View File

@ -76,6 +76,9 @@ class Fixed:
def __eq__(self, other): def __eq__(self, other):
return self.impl == other.impl return self.impl == other.impl
@compatible_types @compatible_types
def __ne__(self, other):
return self.impl != other.impl
@compatible_types
def __gt__(self, other): def __gt__(self, other):
return self.impl > other.impl return self.impl > other.impl
@compatible_types @compatible_types

View File

@ -50,6 +50,8 @@ class Native:
def __eq__(self, other): def __eq__(self, other):
return self.impl == other.impl return self.impl == other.impl
def __ne__(self, other):
return self.impl != other.impl
def __gt__(self, other): def __gt__(self, other):
return self.impl > other.impl return self.impl > other.impl
def __ge__(self, other): def __ge__(self, other):

View File

@ -69,6 +69,9 @@ class Native:
def __eq__(self, other): def __eq__(self, other):
return self.impl == other.impl return self.impl == other.impl
@compatible_types @compatible_types
def __ne__(self, other):
return self.impl != other.impl
@compatible_types
def __gt__(self, other): def __gt__(self, other):
return self.impl > other.impl return self.impl > other.impl
@compatible_types @compatible_types

View File

@ -60,6 +60,8 @@ class Rational:
def __eq__(self, other): def __eq__(self, other):
return self.impl.equals(other.impl) return self.impl.equals(other.impl)
def __ne__(self, other):
return not self.impl.equals(other.impl)
def __gt__(self, other): def __gt__(self, other):
return self.impl.greater(other.impl) return self.impl.greater(other.impl)
def __ge__(self, other): def __ge__(self, other):

View File

@ -78,6 +78,9 @@ class Rational:
def __eq__(self, other): def __eq__(self, other):
return self.impl == other.impl return self.impl == other.impl
@compatible_types @compatible_types
def __ne__(self, other):
return self.impl != other.impl
@compatible_types
def __gt__(self, other): def __gt__(self, other):
return self.impl > other.impl return self.impl > other.impl
@compatible_types @compatible_types