Example #1
0
def cast(db, context, log, fields):
    """Perform CAST command.
       This records the votes in the database, increments the tallies
       and issues a receipt.
    """
    """Get a CrusherDict for this voterid."""
    d = crusherdict.CrusherDict(db, context["id"])
    """Get the CrusherDict for the tallies."""
    t = crusherdict.CrusherDict(db, "T")
    """Currently the voter does not exist in the database at all."""
    d.status("UNCAST")
    """The voter just barely exists, having a status of UNCAST only."""
    for vote in context["votes"]:
        """Add an item with key (office, candidate) and no value to the
           voter dictlist.
        """
        d.getKey(vote[1:3])
    """The votes have been added to the voter, but not the tallies."""
    for vote in context["votes"]:
        """Find the item in the tallies with key (office, candidate) and
           increment it, passing the voterid as "when" the tally was last
           updated.
        """
        t.inc(vote[1:3], context["id"])
    """The votes have been tentatively tallied."""
    """Tentatively increment the number of voters, again using the voterid
       as "when" it was updated.
    """
    t.inc("voters", context["id"])
    """Number of voters has been tentatively incremented."""
    """Change the voter's status to cast."""
    d.status("CAST")
    """The votes have been tallied."""
    return inq(db, context, log, ("INQ", context["id"]))
Example #2
0
File: demo.py Project: csc456/test
def clean(db):
    """Check the database for any votes that are not properly cast.
    """
    t=crusherdict.CrusherDict(db,"T")
    try:
        x = t.getKey("voters")
        print("t.getKey")
        voters = db.fetch(x)
        print("voters = x")
        voters=db.fetch(t.getKey("voters"))
        """Get the CrusherDict for the last voter."""
        v=crusherdict.CrusherDict(db,voters[2]) 
        """Check if the vote was cast."""
        if(v.status()!="CAST"):
            """Last vote was not cast, so roll-back."""
            for tup in v:
                tally=db.fetch(t.getKey(tup[0]))
                try:
                    if(tally[2]==voters[2]):
                        t.getKey(tup[0],tally[1]-1)
                except IndexError:
                    """The tally was previously rolled back, and so
                       does not need to be rolled back this time.
                    """
            t.getKey(tup[0],tally[1]-1)
    except IndexError:
        """The tally was previously rolled back, and so
           does not need to be rolled back this time.
        """
    except KeyError:
        """c'mon python"""
Example #3
0
def inq(db, context, log, fields):
    """Perform INQ command."""
    context.clear()
    log.write("VOTER\n")
    for tup in crusherdict.CrusherDict(db, fields[1]):
        log.write("VOTE\t{}\t{}\n".format(tup[0][0], tup[0][1]))
    log.write("CAST\t{}\n".format(fields[1]))
    return db.doExit
Example #4
0
def report(db, log):
    """Perform final report."""
    t=crusherdict.CrusherDict(db,"T")
    voters=db.fetch(t.getKey("voters"))[1]
    log.write("VOTERS\t{}\n".format(voters))
    for tup in t:
        if tup[0]!="voters":
            log.write("TALLY\t{}\t{}\t{}\n".format(tup[0][0],tup[0][1],tup[1]))
Example #5
0
def cast(db, context, log, fields):
    """Perform CAST command."""
    d = crusherdict.CrusherDict(db, context["id"])
    t = crusherdict.CrusherDict(db, "T")
    """Currently the voter does not exist in the database at all."""
    d.status("UNCAST")
    """The voter just barely exists, having a status of UNCAST only."""
    for vote in context["votes"]:
        d.getKey(vote[1:3])
    """The votes have been added to the voter, but not the tallies."""
    for vote in context["votes"]:
        t.inc(vote[1:3], context["id"])
    """The votes have been tentatively tallied."""
    t.inc("voters", context["id"])
    """Number of voters has been tentatively incremented."""
    d.status("CAST")
    """The votes have been tallied."""
    return inq(db, context, log, ("INQ", context["id"]))