tisonkun commented on code in PR #103:
URL: https://github.com/apache/steve/pull/103#discussion_r3632817769
##########
v3/steve/vtypes/stv.py:
##########
@@ -37,32 +37,67 @@ def load_stv():
stv_tool = load_stv()
+def get_candidates(kv):
+ """Return normalized candidate details keyed by ballot label."""
+
+ version = kv.get('version', 1)
+ labelmap = kv['labelmap']
+
+ if version == 1:
+ candidates = {}
+ for label, name in labelmap.items():
+ if not isinstance(name, str):
+ raise ValueError(f'STV v1 candidate {label!r} must be a name')
+ candidates[label] = {'asfid': '', 'name': name}
+ elif version == 2:
+ candidates = {}
+ for label, value in labelmap.items():
+ if not isinstance(value, (list, tuple)) or len(value) != 2:
+ raise ValueError(
+ f'STV v2 candidate {label!r} must be an [asfid, name] pair'
+ )
+ asfid, name = value
+ if not isinstance(asfid, str) or not isinstance(name, str):
+ raise ValueError(
+ f'STV v2 candidate {label!r} must contain string values'
+ )
+ candidates[label] = {'asfid': asfid, 'name': name}
+ else:
+ raise ValueError(f'Unsupported STV KV version: {version}')
+
+ return candidates
+
+
def tally(votestrings, kv):
"""
Run the STV tally process.
votestrings: List of strings, each representing a voter's preferences as
comma-separated labels
(e.g., 'a,b,c' for votes in order of preference). Labels must
match keys in kv['labelmap'].
kv: Dict containing STV configuration.
- - 'version': Integer version of the kv format (currently 1).
- - 'labelmap': Dict mapping single-character labels to candidate names
(e.g., {'a': 'Alice'}).
+ - 'version': Integer version of the kv format.
+ - 'labelmap': Dict mapping labels to candidate details. Version 1 maps
+ labels directly to names; version 2 maps labels to [asfid, name]
pairs.
- 'seats': Integer number of seats to elect.
"""
# Trim the incoming votestrings: no empty strings:
- trimmed = [ s for vs in votestrings if (s := vs.strip()) ]
+ trimmed = [s for vs in votestrings if (s := vs.strip())]
- # kv['labelmap'] should be: LABEL: NAME
- # for example: { 'a': 'John Doe', }
- labelmap = kv['labelmap']
- revmap = { v: k for k, v in labelmap.items() }
+ candidates = get_candidates(kv)
+ labelmap = {label: candidate['name'] for label, candidate in
candidates.items()}
+ revmap = {name: label for label, name in labelmap.items()}
Review Comment:
This seems a new feature that we may handle in a follow-up PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]