class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('electiondata.py') def testVotes(self): self.results.load() votes = self.results.votes() #Check that the first state is correct #Check with teacher why this syntax is incorrect (and object oriented.py) assert all_votes[0] == 212930
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('election_results_test_file.csv') def testLoad(self): self.results.load() assert self.results!=None assert self.results.file!=None def testStates(self): self.results.load() names = self.results.states() assert len(names)==2 assert names[0]=='Alaska' assert names[1]=='Alabama'
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults("election_results_test_file.csv") def testLoad(self): self.results.load() assert self.results != None assert self.results.file != None def testStateCount(self): self.results.load() state_count = self.results.state_count() assert state_count == 2 def testStates(self): self.results.load() names = self.results.states() assert len(names) == 2 assert names[0] == "Alaska" assert names[1] == "Alabama"
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('election_results_test_file.csv') def testLoad(self): self.results.load() assert self.results is not None assert self.results.file is not None assert len(self.results.all_lines) > 0 def testStateCount(self): self.results.load() state_count = self.results.state_count() assert state_count == 2 def testStates(self): self.results.load() names = self.results.states() assert len(names) == 2 assert names[0] == 'Alaska' assert names[1] == 'Alabama'
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('election_results_test_file.csv') def testLoad(self): self.results.load() assert self.results is not None assert self.results.file is not None assert len(self.results.all_lines) > 0 def testStateCount(self): self.results.load() state_count = self.results.state_count() assert state_count==2 def testStates(self): self.results.load() names = self.results.states() assert len(names)==2 assert names[0]=='Alaska' assert names[1]=='Alabama'
from electiondata import ElectionResults url = 'http://www.archives.gov/federal-register/electoral-college/2012/popular-vote.html' results = ElectionResults(url) results.load() state_names = results.states() print results.pretty_json() print "done ("+str(results.state_count())+" lines)"
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" state_names = results.states() for state in state_names: print " "+state print "done ("+str(results.state_count())+" lines)" print "total number of votes: " + str(results.ObamaVotes() + results.RoVotes())
# Verify that we can open and read the election results CSV correctly
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" state_names = results.states() for state in state_names: print " "+state print "done ("+str(results.state_count())+" lines)"
# Print out all the state names from the csv # Showing an "event-driven" style from electiondata import ElectionResults import sys filename = '2012_US_election_state.csv' results = ElectionResults(filename) while True: command = raw_input('Enter your input (1 to load, 2 to print, 3 to quit):') if command=="1": results.load(); print "Loaded ("+str(results.state_count())+" lines)" elif command=="2": state_names = results.states() for state in state_names: print " "+state elif command=="3": print "bye!" sys.exit() else: print "Uknown command :-("
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('election_results_test_file.csv') def testLoad(self): self.results.load() assert self.results!=None assert self.results.file!=None def testStateCount(self): self.results.load() state_count = self.results.state_count() assert state_count==3 def testStates(self): self.results.load() names = self.results.states() assert len(names)==3 assert names[1]=='Alaska' assert names[2]=='Alabama' def testObama(self): self.results.load() obamavotes = self.results.get_obama_votes() assert len(obamavotes)==3 def testRomney(self): self.results.load() romneyvotes = self.results.get_romney_votes() assert len(romneyvotes)==3
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" state_names = results.states() for state in state_names: print " "+state print "done ("+str(results.state_count())+" lines)" results.addCandidates() results.addTotalVotes() candidates = results.candidates for cand in candidates: print cand + ": " + str(results.totalVotes[cand])
def setUp(self): self.results = ElectionResults('election_results_test_file.csv')
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" state_names = results.states() for state in state_names: print " "+state print "Total votes for 2012 elections: " + str(results.VotesObama() + results.VotesRomney()) print "done ("+str(results.state_count())+" lines)"
# Election Results module # Coded in the "object-oriented" style from electiondata import ElectionResults state_arr[], obama_tot=0, romney_tot=0, filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() function state_names() { state_arr[]=results.states() return state_arr } print "done ("+str(results.state_count())+" lines)" # print total votes per candidate
# Print out all the state names from the csv # Showing an "event-driven" style from electiondata import ElectionResults import sys filename = '2012_US_election_state.csv' results = ElectionResults(filename) while True: command = raw_input('Enter your input (1 to view total votes for each, 2 to compare by state, 3 to quit):') if command=="1": results.load(); print "Obama total:",results.obamaVotes() print "Romney total:",results.romneyVotes() elif command=="2": results.load(); results.compare_bystate() elif command=="3": print "bye!" sys.exit() else: print "Uknown command :-("
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" print "" print "Total Votes" print "" state_names = results.states() for state in state_names: print " "+state print "" print "done ("+str(results.state_count())+" states)" print "" print all_votes[0]
def testEvaluateWinner(self): self.results.load() totalvotes = self.results.candidate_count() winner = ElectionResults.evaluate_winner(totalvotes.C1votes, totalvotes.C2votes) assert winner==2
class ElectionResultsTest(unittest.TestCase): def setUp(self): self.results = ElectionResults('/Users/harishnk/mas500/Programming-Style-Examples/election_results_test_file.csv') def testload(self): self.results.load() assert self.results!=None assert self.results.file!=None def testStateCount(self): self.results.load() state_count = self.results.state_count() assert state_count>=2 def testStates(self): self.results.load() all_names = [] all_names = self.results.states() assert len(all_names)==2 assert all_names[0]=='Alaska' assert all_names[1]=='Alabama' def testCandidateCount(self): self.results.load() totalvotes = self.results.candidate_count() assert totalvotes.C1votes==885316 assert totalvotes.C2votes==1373687 def testEvaluateWinner(self): self.results.load() totalvotes = self.results.candidate_count() winner = ElectionResults.evaluate_winner(totalvotes.C1votes, totalvotes.C2votes) assert winner==2
def setUp(self): self.results = ElectionResults('electiondata.py')
def setUp(self): self.results = ElectionResults('/Users/harishnk/mas500/Programming-Style-Examples/election_results_test_file.csv')
# Print out all the state names from the csv # Coded in the "object-oriented" style from electiondata import ElectionResults filename = '2012_US_election_state.csv' results = ElectionResults(filename) results.load() print "Opened file:" # state_names = results.states() # for state in state_names: # print " "+state obama = results.get_votes("obama") romney = results.get_votes("romney") print " The number of votes for Obama across all states are: "+str(obama) print " The number of votes for Romney across all states are: "+str(romney)