1. sammyrulez
  2. data_munging_code_kata

Commits

sammyrulez  committed 78f356f

completed three

  • Participants
  • Parent commits f2c101d
  • Branches default

Comments (0)

Files changed (3)

File soccer/goals_spread.py

View file
  • Ignore whitespace
 import re
 import math
+from kutil import dry
 
-
-class GoalSpread():
+class GoalSpread(dry.BaseFileDifferentialObject):
 	
-	def __init__(self):
-		self.team = None
-		self.min_diff = None
+	def eval_row(self,row):
+		return (row[0],math.fabs( int(row[6]) - int(row[8])) )
 		
-	def load_file(self):
-		print 
-		return open('./data/football.dat','r')
+	def is_row_data(self,row):
+		return len(row) > 0 and re.search("^[0-9]*\.$",row[0])
 		
-	def eval_spread(self):
-		file = self.load_file()
-		
-		for line in file:
-			row = line.split()
-			
-			if len(row) > 0 and re.search("^[0-9]*\.$",row[0]):
-				
-				team = row[1]
-				spread = math.fabs(int(row[6]) - int(row[8]))
-				
-				if not self.min_diff or self.min_diff > spread:
-					self.min_diff = spread
-					self.team = team
-		
-		return self.team
+	
 				

File tests.py

View file
  • Ignore whitespace
 import unittest
 from weather.temp_spread import TemperatureSpread
 from soccer.goals_spread import GoalSpread
-from kutil import dry
+from kutil.dry import BaseFileDifferentialObject
 
 
 
 		
 	def test_spread(self):
 		t_spread = TemperatureSpread()
-		day_min_diff = t_spread.eval_spread()
+		day_min_diff = t_spread.eval_spread('./data/weather.dat')
 		self.assertTrue(day_min_diff)
 		self.assertTrue(day_min_diff > 0)
 		
 
 	def test_spread(self):
 		s_spread = GoalSpread()
-		goals_min_diff = s_spread.eval_spread()
+		goals_min_diff = s_spread.eval_spread('./data/football.dat')
 		self.assertTrue(goals_min_diff)
 		self.assertTrue(goals_min_diff > 0)
 		
-class TestDry(unittest.TestCase):
-	
-	def test_load_file(self):
-		file = dry.load_file('./data/weather.dat')
-		self.assertTrue(file)
-		
-		
+
+class TestBaseFileDifferentialObject(unittest.TestCase):
+
+	def test_spread(self):
+		b_spread = BaseFileDifferentialObject()
+		b_spread.eval_spread('./data/football.dat')
 		
 		
 		

File weather/temp_spread.py

View file
  • Ignore whitespace
 import math
-
+import re
 from kutil import dry
 
+class TemperatureSpread(dry.BaseFileDifferentialObject):
 
-class TemperatureSpread:
+	def _clean_string(self , string):
+		return ''.join(e for e in string if e.isalnum())
 
-	def __init__(self):
-		self.min_spread = None
-		self.min_spread_day = None
+	def eval_row(self,row):
+		return (row[0],math.fabs( int(self._clean_string(row[1])) - int(self._clean_string(row[2])) ) )
 		
-		
-	def eval_line(self, line):
-		row = line.split()
-		if len(row) < 14:
-			return
-			
-		try:
-			min = int(row[2])
-			max = int(row[1])
-			x_spread = max - min
-			if not self.min_spread or self.min_spread > x_spread:
-				self.min_spread = x_spread
-				self.min_spread_day = int(row[0])
-		except ValueError:
-			pass
-	
-	def eval_spread(self):
-		file = dry.load_file('./data/weather.dat')
-		for line in file:
-			self.eval_line(line)
-		return self.min_spread
+	def is_row_data(self,row):
+		return len(row) > 0 and re.search("^[0-9]*$",row[0])
+