def check_reported_selids(e): # make sure e.selids_c[cid] contains all +/- selids seen in reported votes # and that e.votes_c[cid] contains all reported votes for cid in e.cids: for pbcid in e.possible_pbcid_c[cid]: for bid in e.bids_p[pbcid]: if bid in e.rv_cpb[cid][pbcid]: rv = e.rv_cpb[cid][pbcid][bid] else: rv = ("-NoSuchContest", ) utils.nested_set(e.votes_c, [cid, rv], True) for selid in rv: if ids.is_writein(selid) or ids.is_error_selid(selid): e.selids_c[cid][selid] = True
def plurality(e, cid, tally): """ Return, for input dict tally mapping votes to (int) counts, vote with largest count. (Tie-breaking done arbitrarily here.) Winning vote must be a valid winner (e.g. not ("-Invalid",) or ("-NoSuchContest",) ) an Exception is raised if this is not possible. An undervote or an overvote can't win. """ max_cnt = -1e90 max_vote = None for vote in tally: if tally[vote] > max_cnt and \ len(vote) == 1 and \ not ids.is_error_selid(vote[0]): max_cnt = tally[vote] max_vote = vote if max_vote == None: assert "No winner allowed in plurality contest.", tally return max_vote
def check_reported(e): if not isinstance(e.rn_cpr, dict): utils.myerror("e.rn_cpr is not a dict.") for cid in e.rn_cpr: if cid not in e.cids: utils.mywarning("cid `{}` not in e.cids.".format(cid)) for pbcid in e.rn_cpr[cid]: if pbcid not in e.pbcids: utils.mywarning("pbcid `{}` is not in e.pbcids.".format(pbcid)) for cid in e.rn_cpr: for pbcid in e.rn_cpr[cid]: for rv in e.rn_cpr[cid][pbcid]: for selid in rv: if selid not in e.selids_c[cid] and selid[0].isalnum(): utils.mywarning( "selid `{}` is not in e.selids_c[{}].".format( selid, cid)) for cid in e.rn_cpr: for pbcid in e.rn_cpr[cid]: for rv in e.rn_cpr[cid][pbcid]: if not isinstance(e.rn_cpr[cid][pbcid][rv], int): utils.mywarning( "value `e.rn_cpr[{}][{}][{}] = `{}` is not an integer." .format(cid, pbcid, rv, e.rn_cpr[cid][pbcid][rv])) if not (0 <= e.rn_cpr[cid][pbcid][rv] <= e.rn_p[pbcid]): utils.mywarning( "value `e.rn_cpr[{}][{}][{}] = `{}` is out of range 0:{}." .format(cid, pbcid, rv, e.rn_cpr[cid][pbcid][rv], e.rn_p[pbcid])) if not (0 <= e.rn_cpr[cid][pbcid][rv] <= e.rn_c[cid]): utils.mywarning( "value `e.rn_cpr[{}][{}][{}] = `{}` is out of range 0:{}." .format(cid, pbcid, rv, e.rn_cpr[cid][pbcid][rv], e.rn_p[pbcid])) for cid in e.cids: for rv in e.votes_c[cid]: if e.rn_cr[cid][rv] != \ sum([e.rn_cpr[cid][pbcid][rv] for pbcid in e.rn_cpr[cid]]): utils.mywarning( "sum of e.rn_cpr[{}][*][{}] is not e.rn_cr[{}][{}].". format(cid, rv, cid, rv)) for cid in e.cids: if cid not in e.rn_cpr: utils.mywarning("cid `{}` is not a key for e.rn_cpr".format(cid)) for pbcid in e.possible_pbcid_c[cid]: if pbcid not in e.rn_cpr[cid]: utils.mywarning( "pbcid {} is not a key for e.rn_cpr[{}].".format( pbcid, cid)) # for selid in e.selids_c[cid]: # assert selid in e.rn_cpr[cid][pbcid], (cid, pbcid, selid) # ## not necessary, since missing selids have assumed count of 0 if not isinstance(e.rn_c, dict): utils.myerror("e.rn_c is not a dict.") for cid in e.rn_c: if cid not in e.cids: utils.mywarning("e.rn_c key `{}` is not in e.cids.".format(cid)) if not isinstance(e.rn_c[cid], int): utils.mywarning("e.rn_c[{}] = {} is not an integer.".format( cid, e.rn_c[cid])) for cid in e.cids: if cid not in e.rn_c: utils.mywarning("cid `{}` is not a key for e.rn_c".format(cid)) if not isinstance(e.rn_cr, dict): utils.myerror("e.rn_cr is not a dict.") for cid in e.rn_cr: if cid not in e.cids: utils.mywarning( "e.rn_cr key cid `{}` is not in e.cids".format(cid)) for vote in e.rn_cr[cid]: for selid in vote: if (not ids.is_writein(selid) and not ids.is_error_selid(selid)) \ and not selid in e.selids_c[cid]: utils.mywarning( "e.rn_cr[{}] key `{}` is not in e.selids_c[{}]".format( cid, selid, cid)) if not isinstance(e.rn_cr[cid][vote], int): utils.mywarning( "e.rn_cr[{}][{}] = {} is not an integer.".format( cid, vote, e.rn_cr[cid][vote])) for cid in e.cids: if cid not in e.rn_cr: utils.mywarning("cid `{}` is not a key for e.rn_cr".format(cid)) if not isinstance(e.bids_p, dict): utils.myerror("e.bids_p is not a dict.") for pbcid in e.pbcids: # if not isinstance(e.bids_p[pbcid], dict): # utils.myerror("e.bids_p[{}] is not a dict.".format(pbcid)) if not isinstance(e.bids_p[pbcid], list): utils.myerror("e.bids_p[{}] is not a list.".format(pbcid)) if not isinstance(e.rv_cpb, dict): utils.myerror("e.rv_cpb is not a dict.") for cid in e.rv_cpb: if cid not in e.cids: utils.mywarning("e.rv_cpb key `{}` is not in e.cids.".format(cid)) for pbcid in e.rv_cpb[cid]: if pbcid not in e.pbcids: utils.mywarning( "e.rv_cpb[{}] key `{}` is not in e.pbcids.".format( cid, pbcid)) if not isinstance(e.rv_cpb[cid][pbcid], dict): utils.myerror("e.rv_cpb[{}][{}] is not a dict.".format( cid, pbcid)) bidsset = set(e.bids_p[pbcid]) for bid in e.rv_cpb[cid][pbcid]: if bid not in bidsset: utils.mywarning( "bid `{}` from e.rv_cpb[{}][{}] is not in e.bids_p[{}]." .format(bid, cid, pbcid, pbcid)) for cid in e.cids: if cid not in e.rv_cpb: utils.mywarning("cid `{}` is not a key in e.rv_cpb.".format(cid)) for pbcid in e.possible_pbcid_c[cid]: if pbcid not in e.rv_cpb[cid]: utils.mywarning( ("pbcid `{}` from e.possible_pbcid_c[{}] " "is not a key for e.rv_cpb[{}].").format(pbcid, cid, cid)) if not isinstance(e.ro_c, dict): utils.myerror("e.ro_c is not a dict.") for cid in e.ro_c: if cid not in e.cids: utils.mywarning( "cid `{}` from e.rv_cpb is not in e.cids".format(cid)) for cid in e.cids: if cid not in e.ro_c: utils.mywarning("cid `{}` is not a key for e.ro_c.".format(cid)) if utils.warnings_given > 0: utils.myerror("Too many errors; terminating.")
def test_is_error_selid(): assert ids.is_error_selid("abc") == False assert ids.is_error_selid("-abc") == True