def inc_integrity(entries, dirty, mv, numRecursions=0): '''Check integrity of the .inc command. ''' vprint(1, ' inc_integrity()') t = crusherdict3.CrusherDict(mv['db'], "___T___") numRecursions += 1 nr = numRecursions # Cast but only if T previously failed. if dirty: vprint(1, ' inc_integrity::mv') d = crusherdict3.CrusherDict(mv['db'], context["id"]) makeVotes(mv['db'], mv['context']) # Loop for k, r in entries.items(): s = -1 # Debug output try: s = t.safeFetch(r['entry']) if str(s) != str(r['valEntry']): raise LookupError except: vprint(1, ' inc_integrity::mv & recurse1') t.safeStore(r['entry'], r['valEntry']) t.safeStore(r['index'], r['valIndex']) return inc_integrity(entries, True, mv, nr) # Recurse s = -1 # Debug output try: s = t.safeFetch(r['index']) if str(s) != str(r['valIndex']): raise LookupError except: vprint(1, ' inc_integrity::mv & recurse2') t.safeStore(r['entry'], r['valEntry']) t.safeStore(r['index'], r['valIndex']) return inc_integrity(entries, True, mv, nr) # Recurse
def threadVote(db, context, fields, stop_event, numberRecursions=0): '''Debug: numberRecursions .inc() ought to return a list of Key-Value database pairs (KVP). Then demo.py adds these to a list to be checked later on when all .inc calls have been made. Then demo.py will read all Keys from the Key-Value pairs (KVP) from the database to ensure that they match the corresponding value. If any KVP does not match then rewrite that Key-Value pair. Also, you may want to run a MVL check (matchesVoteLog) here as well. ''' d = crusherdict3.CrusherDict(db, context["id"]) t = crusherdict3.CrusherDict(db, "___T___") d.status("UNCAST") # Cast makeVotes(db, context) """The votes have been added to the voter, but not the tallies.""" entries = {} for vote in context["votes"]: rslt = t.inc(vote[1:3], context["id"]) entries[str( vote[1:3] )] = rslt # When there are multiple key-value pairs the last vote will override the previous ones. """The votes have been tentatively tallied.""" rslt = t.inc("voters", context["id"]) entries['voters'] = rslt ################################### ## Recurse __T__.integrity. ################################### inc_integrity(entries, False, {'db': db, 'context': context}) """Number of voters has been tentatively incremented.""" d.status("CAST") """The votes have been tallied.""" return
def inq(dbs, context, log, fields): """Perform INQ command.""" voter_id = fields[1] context.clear() log.write("VOTER\n") #vote on db logs logs = {} best = None curr = 0 for i in dbs: c = crusherdict3.CrusherDict(i, voter_id) try: tmp = check_inq(c) except: raise Exception(' inq::tmp_log fails') try: logs[tmp] += 1 except: logs[tmp] = 0 if logs[tmp] > curr: curr = logs[tmp] best = tmp # Write best log.write(tmp) log.write("CAST\t{}\n".format(voter_id)) return dbsdoexit(dbs)
def matchesVoteLog(db, checkvl, vid): '''Check that successful write before continuing on. Needs to match eg: VOTE President Donald Trump VOTE Governor Mickey Mouse VOTE Lt. Governor Melinda Gates ''' c = crusherdict3.CrusherDict(db, vid) # Ppen db-X with vid-Y x = pre_check_inq(c) if x is False: print(' mvl:Fails checka') print(' Looking for:') print(''.join(str(x) for x in checkvl)) print(' Seeing:') print(x) return False else: for i in checkvl: if i not in x: print(' mvl:Fails checkb') print(' Looking for:') print(''.join(str(x) for x in checkvl)) print(' Seeing:') print(x) return False # Success return True
def makeVotes(i, context): '''For each voter save their votes, then check integrity of that process. Do not run equivalent of .getKey as this we already know what needs to be written. At the end save countName. Then run .inc for each voter, then check integrity of that process. Note:Save to file debug is i.db.save() ''' vprint(1, ' makeVotes()') d = crusherdict3.CrusherDict(i, context["id"]) checkvl = [] # Array to match against # Note: IndexName and EntryName stored for every vote processed. for j in range(len(context["votes"])): vote = context["votes"][j] key = vote[1:3] # Office,Cand d.safeStore(entryName(context["id"], j), (key, None)) d.safeStore(indexName(context["id"], key), j) checkvl.append("VOTE\t{}\t{}\n".format(vote[1], vote[2])) # Store countName only one time, that is, the last entry. d.safeStore(countName(context["id"]), j + 1) if not matchesVoteLog(i, checkvl, context['id']): #numberRecursions+=1 return makeVotes(i, context) # Recurse
def report(dbs, log): """Perform final report.""" print('demo.py report()') # Get total voters voters = {} best = None curr = 0 for i in dbs: t = crusherdict3.CrusherDict(i, "___T___") try: n = t.getKey("voters") x = t.safeFetch(n) # eval here x = ast.literal_eval(x) x = x[1] except: continue try: voters[x] += 1 except: voters[x] = 1 if voters[x] > curr: curr = voters[x] best = x # Best head = "VOTERS\t{}\n".format(best) log.write(head) # Get tallies try: voters = {} best = None curr = 0 for i in dbs: t = crusherdict3.CrusherDict(i, "___T___") j = 0 vprint(1, len(t)) vprint(1, len(t)) vprint(1, len(t)) vprint(1, len(t)) vprint(1, j < len(t)) tmp = '' while j < len(t): #calls __len__() vprint(1, j, end=', ') try: n = t.safeFetch(entryName('___T___', j)) vprint(1, ' str-tup:', n) tup = ast.literal_eval(n) vprint(1, ' ', tup) vprint(1, ' ', tup[0]) if str(tup[0]) != "voters": vprint(1, ' ', tup[0], '!="voters"') tmp += "TALLY\t{}\t{}\t{}\n".format( tup[0][0], tup[0][1], tup[1]) j += 1 except: vprint(1, 'jx') try: voters[tmp] += 1 except: voters[tmp] = 1 if voters[tmp] > curr: curr = voters[tmp] best = tmp log.write(best) except: vprint(1, ' Exception:', sys.exc_info()[0]) vprint(1, ' report::vote-log3 ...')
def threadCastEmpty(db, context): t = crusherdict3.CrusherDict(db, "___T___") entries = {} rslt = t.inc("voters", context["id"]) entries['voters'] = rslt inc_integrity(entries, False, {'db': db, 'context': context})