Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit 74ceb4a

Browse files
committed
add test-driven example
1 parent e3bdfcb commit 74ceb4a

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

election_results_test_file.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
State Postal,County Name,FIPS,Obama vote,%,Romney vote,%
2+
AK,Alaska,0,91696,41.6,121234,55
3+
AL,Alabama,0,793620,38.4,1252453,60.7

electiondata.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ElectionResults:
2+
3+
def __init__(self, filename):
4+
self.filename = filename
5+
6+
def load(self):
7+
self.file = open(self.filename, 'r')
8+
self.all_lines = self.file.readlines()
9+
10+
def states(self):
11+
all_names = []
12+
for line in self.all_lines:
13+
columns = line.split(',')
14+
all_names.append(columns[1])
15+
return all_names
16+
17+
def state_count(self):
18+
return len(self.all_lines)

object-oriented.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Print out all the state names from the csv# Coded in the "object-oriented" styleclass ElectionResults: def __init__(self, filename): self.filename = filename def load(self): self.file = open(filename, 'r') self.all_lines = self.file.readlines() def states(self): all_names = [] for line in self.all_lines: columns = line.split(',') all_names.append(columns[1]) return all_names def state_count(self): return len(self.all_lines)filename = '2012_US_election_state.csv'results = ElectionResults(filename)results.load()print "Opened file:"state_names = results.states()for state in state_names: print " "+stateprint "done ("+str(results.state_count())+" lines)"
1+
# Print out all the state names from the csv# Coded in the "object-oriented" stylefrom electiondata import ElectionResultsfilename = '2012_US_election_state.csv'results = ElectionResults(filename)results.load()print "Opened file:"state_names = results.states()for state in state_names: print " "+stateprint "done ("+str(results.state_count())+" lines)"

test-driven.py

743 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)