[Head First Python]6. 定制数据对象:打包代码与数据
相同功能,演进实现
数据文件
sarah2.txt
sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
1- 返回dict
return({'Name':data_list.pop(0),
'DOB':data_list.pop(0),
'Time':str( sorted( set([sanitize(t) for t in data_list] ) )[0:3])})
1 def sanitize(time_string): 2 if '-' in time_string: 3 splitter = '-' 4 elif ':' in time_string: 5 splitter = ':' 6 else: 7 return (time_string) 8 9 (mins, secs) = time_string.split(splitter) 10 return(mins + '.' + secs) 11 12 def get_coach_data(filename): 13 try: 14 with open(filename) as f: 15 data = f.readline() 16 data_list = data.strip().split(',') 17 return({'Name':data_list.pop(0), 18 'DOB':data_list.pop(0), 19 'Time':str( sorted( set([sanitize(t) for t in data_list] ) )[0:3])}) 20 except IOError as err: 21 print("file err:" + str(err)) 22 return(none) 23 24 25 julie = get_coach_data('julie2.txt') 26 james = get_coach_data('james2.txt') 27 sarah = get_coach_data('sarah2.txt') 28 mikey = get_coach_data('mikey2.txt') 29 30 print( julie['Name'] + "'s faster time are:" + julie['Time']) 31 print( james['Name'] + "'s faster time are:" + james['Time']) 32 print( sarah['Name'] + "'s faster time are:" + sarah['Time']) 33 print( mikey['Name'] + "'s faster time are:" + mikey['Time'])
2- 返回类 return ( Athlete(datalist.pop(0), datalist.pop(0), datalist))
1 def sanitize(time_string): 2 if '-' in time_string: 3 splitter = '-' 4 elif ':' in time_string: 5 splitter = ':' 6 else: 7 return(time_string) 8 9 (mins, secs) = time_string.split(splitter) 10 return(mins + "." + secs) 11 12 class Athlete: 13 def __init__(self, a_name, a_dob = None, a_time = []): 14 self.name = a_name 15 self.dob = a_dob 16 self.times = a_time 17 18 def top3(self): 19 return( sorted(set([sanitize(t) for t in self.times]))[0:3] ) 20 21 def add_time(self,time_value): 22 self.times.append(time_value) 23 24 def add_times(self,list_of_times): 25 self.times.extend(list_of_times) 26 27 28 def get_coach_data(filename): 29 try: 30 with open(filename) as f: 31 data = f.readline(); 32 datalist = data.strip().split(',') 33 return ( Athlete(datalist.pop(0), datalist.pop(0), datalist)) 34 except IOError as err: 35 print('file err:' + str(err)) 36 return(none) 37 38 39 julie = get_coach_data('julie2.txt') 40 james = get_coach_data('james2.txt') 41 sarah = get_coach_data('sarah2.txt') 42 mikey = get_coach_data('mikey2.txt') 43 44 print(julie.name + "'s faster times are:" + str(julie.top3())) 45 print(james.name + "'s faster times are:" + str(james.top3())) 46 print(sarah.name + "'s faster times are:" + str(sarah.top3())) 47 print(mikey.name + "'s faster times are:" + str(mikey.top3())) 48 49 vera = Athlete('vera vi') 50 vera.add_time('1.30') 51 print(vera.top3()) 52 vera.add_times(['1.00','0.3','1.2']) 53 print(vera.top3())
3- 继承python内置list
1 class Athletelist(list): 2 def __init__(self, a_name, a_dob = None, a_times = []): 3 list.__init__([]) 4 self.name = a_name 5 self.dob = a_dob 6 self.extend(a_times) 7 def top3(self): 8 return( sorted( set([sanitize(t) for t in self] ) )[0:3]) 9 10 def sanitize(time_string): 11 if '-' in time_string: 12 splitter = '-' 13 elif ':' in time_string: 14 splitter = ':' 15 else: 16 return(time_string) 17 18 (mins, secs) = time_string.split(splitter) 19 return(mins + "." + secs) 20 21 #vera = Athletelist("vera vi") 22 #vera.append('1.31') 23 #print(vera.top3()) 24 #vera.extend(["1","2","0"]) 25 #print(vera.top3()) 26 27 def get_coach_data(filename): 28 try: 29 with open(filename) as f: 30 data = f.readline(); 31 datalist = data.strip().split(',') 32 return ( Athletelist(datalist.pop(0), datalist.pop(0), datalist)) 33 except IOError as err: 34 print('file err:' + str(err)) 35 return(none) 36 37 38 julie = get_coach_data('julie2.txt') 39 james = get_coach_data('james2.txt') 40 sarah = get_coach_data('sarah2.txt') 41 mikey = get_coach_data('mikey2.txt') 42 43 print(julie.name + "'s faster times are:" + str(julie.top3())) 44 print(james.name + "'s faster times are:" + str(james.top3())) 45 print(sarah.name + "'s faster times are:" + str(sarah.top3())) 46 print(mikey.name + "'s faster times are:" + str(mikey.top3()))