Polling/pollAnalysis.py
def poll_analysis(pollCsvFile=None):
    """This function performs poll analysis on the data contained in a passed CSV file."""

    # Check that a file was passed
    if pollCsvFile == None:
        return []

    # Initialize a candidates dictionary.
    theCandidates  = dict()

    # Create an instance of ReadCsv for reading the election CSV file
    electionData = ReadCsv(pollCsvFile)
    
    # Ignore the header row
    if len(electionData.next_row()) == 0:
        print(f"\nHeader row not found in CSV file {pollCsvFile}\n")
        return []
    
    # Read the first row of data.
    row = electionData.next_row()
    if len(row) == 0:
        print(f"\nNo data found in CSV file {pollCsvFile}")
        print("    after header row\n")
        return []
    elif len(row) != 3:
        print(f"\nUnexpected data found in CSV file {pollCsvFile}")
        print(f"    at line  {electionData.row_num()}\n")
        return []
    
    # Set this candidate's vote count to 1
    theCandidates[row[2]] = 1
    
    # Loop through each remaining row of data in the CSV file
    row = electionData.next_row()
    while len(row) == 3:
    
        # If this candidate is 'in' the dictionary, update this candidate's
        # vote count. Else set this candidate's vote count to 1
        if row[2] in theCandidates:
            theCandidates[row[2]] += 1
        else:
            theCandidates[row[2]] = 1

        # Read the next row
        row = electionData.next_row()
    
    else:
        if len(row) != 0:
            print(f"\nUnexpected data found in CSV file {pollCsvFile}")
            print(f"    at line  {electionData.row_num()}\n")
            return []

    # Create a List of tuples from the candidates dictionary that are sorted by decending vote count
    theCandidates = [(c,v) for c, v in sorted(theCandidates.items(), key=lambda pair : pair[1], reverse=True)]

    # Determine total number of votes
    totalVotes = 0
    for candidate in theCandidates:
        totalVotes += candidate[1]

    # Create the List of the strings to return
    returnStrs = [
                    ("\n Election Results\n -------------------------------\n"),
                    (f" Total Votes: {totalVotes}\n -------------------------------\n"),
                 ]
    for candidate in theCandidates:
        votes = candidate[1]
        returnStrs.append(f" {candidate[0]}: {round((votes/totalVotes) * 100, 2)}%  ({votes})\n"),

    winnerTuple = theCandidates.pop(0)
    returnStrs.append(" -------------------------------\n"),
    returnStrs.append(f" Winner: {winnerTuple[0]}\n"),
    returnStrs.append(" -------------------------------\n")

    # Return the List
    return returnStrs
This function performs poll analysis on the data contained in a passed CSV file.
This function performs poll analysis on the data contained in a passed CSV file.
This function performs poll analysis on the data contained in a passed CSV file .
def poll_analysis(pollCsvFile=None):
    if pollCsvFile == None:
        return []
    theCandidates  = dict()
    electionData = ReadCsv(pollCsvFile)
    if len(electionData.next_row()) == 0:
        print(f"\nHeader row not found in CSV file {pollCsvFile}\n")
        return []
    row = electionData.next_row()
    if len(row) == 0:
        print(f"\nNo data found in CSV file {pollCsvFile}")
        print("    after header row\n")
        return []
    elif len(row) != 3:
        print(f"\nUnexpected data found in CSV file {pollCsvFile}")
        print(f"    at line  {electionData.row_num()}\n")
        return []
    theCandidates[row[2]] = 1
    row = electionData.next_row()
    while len(row) == 3:
        if row[2] in theCandidates:
            theCandidates[row[2]] += 1
        else:
            theCandidates[row[2]] = 1
        row = electionData.next_row()
    else:
        if len(row) != 0:
            print(f"\nUnexpected data found in CSV file {pollCsvFile}")
            print(f"    at line  {electionData.row_num()}\n")
            return []
    theCandidates = [(c,v) for c, v in sorted(theCandidates.items(), key=lambda pair : pair[1], reverse=True)]
    totalVotes = 0
    for candidate in theCandidates:
        totalVotes += candidate[1]
    returnStrs = [
                    ("\n Election Results\n -------------------------------\n"),
                    (f" Total Votes: {totalVotes}\n -------------------------------\n"),
                 ]
    for candidate in theCandidates:
        votes = candidate[1]
        returnStrs.append(f" {candidate[0]}: {round((votes/totalVotes) * 100, 2)}%  ({votes})\n"),
    winnerTuple = theCandidates.pop(0)
    returnStrs.append(" -------------------------------\n"),
    returnStrs.append(f" Winner: {winnerTuple[0]}\n"),
    returnStrs.append(" -------------------------------\n")
    return returnStrs
def poll_analysis ( pollCsvFile = None ) : if pollCsvFile == None : return [ ] theCandidates = dict ( ) electionData = ReadCsv ( pollCsvFile ) if len ( electionData . next_row ( ) ) == 0 : print ( f"\nHeader row not found in CSV file {pollCsvFile}\n" ) return [ ] row = electionData . next_row ( ) if len ( row ) == 0 : print ( f"\nNo data found in CSV file {pollCsvFile}" ) print ( "    after header row\n" ) return [ ] elif len ( row ) != 3 : print ( f"\nUnexpected data found in CSV file {pollCsvFile}" ) print ( f"    at line  {electionData.row_num()}\n" ) return [ ] theCandidates [ row [ 2 ] ] = 1 row = electionData . next_row ( ) while len ( row ) == 3 : if row [ 2 ] in theCandidates : theCandidates [ row [ 2 ] ] += 1 else : theCandidates [ row [ 2 ] ] = 1 row = electionData . next_row ( ) else : if len ( row ) != 0 : print ( f"\nUnexpected data found in CSV file {pollCsvFile}" ) print ( f"    at line  {electionData.row_num()}\n" ) return [ ] theCandidates = [ ( c , v ) for c , v in sorted ( theCandidates . items ( ) , key = lambda pair : pair [ 1 ] , reverse = True ) ] totalVotes = 0 for candidate in theCandidates : totalVotes += candidate [ 1 ] returnStrs = [ ( "\n Election Results\n -------------------------------\n" ) , ( f" Total Votes: {totalVotes}\n -------------------------------\n" ) , ] for candidate in theCandidates : votes = candidate [ 1 ] returnStrs . append ( f" {candidate[0]}: {round((votes/totalVotes) * 100, 2)}%  ({votes})\n" ) , winnerTuple = theCandidates . pop ( 0 ) returnStrs . append ( " -------------------------------\n" ) , returnStrs . append ( f" Winner: {winnerTuple[0]}\n" ) , returnStrs . append ( " -------------------------------\n" ) return returnStrs
This function performs poll analysis on the data contained in a passed CSV file.
This function performs poll analysis on the data contained in a passed CSV file .
"""This function performs poll analysis on the data contained in a passed CSV file.""" # Check that a file was passed # Initialize a candidates dictionary. # Create an instance of ReadCsv for reading the election CSV file # Ignore the header row # Read the first row of data. # Set this candidate's vote count to 1 # Loop through each remaining row of data in the CSV file # If this candidate is 'in' the dictionary, update this candidate's # vote count. Else set this candidate's vote count to 1 # Read the next row # Create a List of tuples from the candidates dictionary that are sorted by decending vote count # Determine total number of votes # Create the List of the strings to return # Return the List
pollCsvFile