GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Head_First_Python/applets.py at master · ninth-lan/Head_First_Python Expand file tree/ Copy pathapplets.pyMore file actions 341 lines (325 loc) · 12.9 KB / Copy pathapplets.pyFile metadata and controls341 lines (325 loc) · 12.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 import PrintNester import os import pickle ''' list1 = [1,'as','dd',[2,'dfd','wqw','rwe',['da',3,['dd',4],'fda','ds']]] PrintNester.print_list (list1) """等价于PrintNester.print_list (list1,0),因为这里函数设置了默认参数""" print('1==================================================') PrintNester.print_list(list1,True) print('2==================================================') PrintNester.print_list(list1,True,2) ''' """ os.chdir ('E:\\qsl\\learning\\Python') man = [] other = [] try: with open('Sketch.txt') as data: for each_line in data: try: (role,line_spoken)=each_line.split(':',1) line_spoken=line_spoken.strip() if role == 'Man': man.append(line_spoken) elif role == 'Other Man': other.append(line_spoken) except ValueError: pass data.close() except IOError: print("The datafile is missing!") """ """ try: with open('man_data.txt','w')as man_file: PrintNester.print_list(man, fh=man_file) with open('other_data.txt','w')as other_file: PrintNester.print_list(other,fh=other_file) except IOError as err: print('File Error: '+str(err)) """ ''' try:#采用dump保存文件,wb参数代表二进制保存,需要导入pickle模块 with open('man_data1.txt','wb')as man_file,open('other_data1.txt','wb')as other_file: pickle.dump(man, man_file) pickle.dump(other,other_file) except IOError as err: print('File Error: '+str(err)) except pickle.PickleError as peer: print('Pickling Error: '+str(peer)) ''' ''' >>> import PrintNester >>> import pickle >>> new_man=[] >>> try: with open('man_data1.txt','rb') as man_file: new_man=pickle.load(man_file) except IOError as err: print('File Error: '+str(err)) except pickle.PickleError as perr: print("Pickling Error: "+str(perr)) >>> PrintNester.print_list(new_man) >>> print(new_man[0])#显示这个人讲的第一行 Is this the right room for an argument? >>> print(new_man[-1])#显示这个人讲的最后一行 Yes it is! ''' ###------第5章代码-------# ###1. 初始读取文件 ##os.chdir("E:\\qsl\\learning\\Python\\chapter5") ##with open("james.txt") as jaf: ## data1=jaf.readline() ##james_data = data1.strip().split(',') ##with open("julie.txt") as juf: ## data2=juf.readline() ##julie_data = data2.strip().split(',') ##with open("mikey.txt") as mif: ## data3=mif.readline() ##mikey_data = data3.strip().split(',') ##with open("sarah.txt") as saf: ## data4=saf.readline() ##sarah_data = data4.strip().split(',') ##""" ##print('===========1===============') ##print(james_data) ##print(julie_data) ##print(mikey_data) ##print(sarah_data) ## ##print('===========2===============') ##print(sorted(james_data)) ##print(sorted(julie_data)) ##print(sorted(mikey_data)) ##print(sorted(sarah_data)) ## ##""" ##''' ###两种排序方式: ###a. 原地排序(sort()方法,调用data=data.sort())--按照指定的顺序排列数据,排序后的数据替换原来的数据,原来的顺序丢失 ###b. 复制排序(soted()BIF,调用data2=sorted(data))--按指定顺序排序,返回原数据的有序副本,原数据顺序依然保留,只是对副本排序 ###2. 直接进行排序 ##print('===========1===============') ##print('james_data: '+str(james_data)) ##print('julie_data: '+str(julie_data)) ##print('mikey_data: '+str(mikey_data)) ##print('sarah_data: '+str(sarah_data)) ## ##print('===========2(排序)===============') ##print('james_data_sorted: '+str(sorted(james_data))) ##print('julie_data_sorted: '+str(sorted(julie_data))) ##print('mikey_data_sorted: '+str(sorted(mikey_data))) ##print('sarah_data_sorted: '+str(sorted(sarah_data))) ##''' ## ## ###3. 处理数据然后进行排序 ##def santize(time_string): ## if '-' in time_string: ## splitter='-' ## elif ':' in time_string: ## splitter=':' ## else: ## return time_string ## (mins,secs)=time_string.split(splitter) ## return(mins+'.'+secs) ##""" ##clean_james=[] ##clean_julie=[] ##clean_mikey=[] ##clean_sarah=[] ## ##for each_james in james_data: ## clean_james.append(santize(each_james)) ## ###for语句等价于以下语句,转换成一行代码利用列表推到来完成,这个列表推导隐含append()方法 ###clean_james=[santize(each_james) for each_james in james_data] ## ##for each_julie in julie_data: ## clean_julie.append(santize(each_julie)) ##for each_mikey in mikey_data: ## clean_mikey.append(santize(each_mikey)) ##for each_sarah in sarah_data: ## clean_sarah.append(santize(each_sarah)) ## ##print('=====================处理1======================') ##print(clean_james) ##print(clean_julie) ##print(clean_mikey) ##print(clean_sarah) ##print('=====================处理2(排序)======================') ## ##print(sorted(clean_james)) ###print(sorted(clean_james,reverse=True))#第二个参数表名数据降序排序 ##print(sorted(clean_julie)) ##print(sorted(clean_mikey)) ##print(sorted(clean_sarah)) ## ## ###4. 迭代删除重复项,打印最快的3次时间 ##unique_james=[] ##for each_unique_james in sorted(clean_james): ## if each_unique_james not in unique_james: ## unique_james.append(each_unique_james) ##unique_julie=[] ##for each_unique_julie in sorted(clean_julie): ## if each_unique_julie not in unique_julie: ## unique_julie.append(each_unique_julie) ##unique_mikey=[] ##for each_unique_mikey in sorted(clean_mikey): ## if each_unique_mikey not in unique_mikey: ## unique_mikey.append(each_unique_mikey) ##unique_sarah=[] ##for each_unique_sarah in sorted(clean_sarah): ## if each_unique_sarah not in unique_sarah: ## unique_sarah.append(each_unique_sarah) ##print('===========最快3次============') ##print(unique_james) ##print(unique_julie[0:3]) ##print(unique_mikey[0:3]) ##""" ##''' ###5. 用集合删除重复项 ##print('===========注释第2~4段代码,保留sanitize()方法============') ##print(sorted(set([santize(t) for t in james_data]))) ##print(sorted(set([santize(t) for t in julie_data]))[0:3]) ##print(sorted(set([santize(t) for t in mikey_data]))[0:3]) ##print(sorted(set([santize(t) for t in sarah_data]))[0:3]) ##''' ###6. 将第1部分的4个with语句改为函数然后进行调用 ## ##def get_coach_data(filename): ## try:#记住必须进行异常处理 ## with open(filename) as f: ## data=f.readline() ## return(data.strip().split(',')) ## except IOError as ioerr: ## print("File Error: "+str(ioerr)) ## return(None) ##james_data1 = get_coach_data('james.txt') ##julie_data1 = get_coach_data('julie.txt') ##mikey_data1 = get_coach_data('mikey.txt') ##sarah_data1 = get_coach_data('sarah.txt') ##print('====使用两个自定义函数的调用打印列表前三项数据===') ##print(sorted(set([santize(t) for t in james_data1]))[0:3]) ##print(sorted(set([santize(t) for t in julie_data1]))[0:3]) ##print(sorted(set([santize(t) for t in mikey_data1]))[0:3]) ##print(sorted(set([santize(t) for t in sarah_data1]))[0:3]) ## #第6章 定制数据对象--打包代码与数据 os.chdir("E:\\qsl\\learning\\Python\\chapter6") ##def get_coach_data(filename): ## try:#记住必须进行异常处理 ## with open(filename) as f: ## data=f.readline() ## return(data.strip().split(',')) ## except IOError as ioerr: ## print("File Error: "+str(ioerr)) ## return(None) def santize(time_string): if '-' in time_string: splitter='-' elif ':' in time_string: splitter=':' else: return time_string (mins,secs)=time_string.split(splitter) return(mins+'.'+secs) #1. 有额外的数据,抽取数据的名称及出生日期 ##james_data1 = get_coach_data('james2.txt') ##julie_data1 = get_coach_data('julie2.txt') ##mikey_data1 = get_coach_data('mikey2.txt') ##sarah_data1 = get_coach_data('sarah2.txt') ##(james_name,james_dob) = james_data1.pop(0),james_data1.pop(0) ##print(james_name + "'s faster times are: " + str(sorted(set([santize(t) for t in james_data1]))[0:3])) ##(julie_name,julie_dob) = julie_data1.pop(0),julie_data1.pop(0) ##print(julie_name + "'s faster times are: " + str(sorted(set([santize(t) for t in julie_data1]))[0:3])) ##(mikey_name,mikey_dob) = mikey_data1.pop(0),mikey_data1.pop(0) ##print(mikey_name + "'s faster times are: " + str(sorted(set([santize(t) for t in mikey_data1]))[0:3])) ##(sarah_name,sarch_dob) = sarah_data1.pop(0),sarah_data1.pop(0) ##print(sarah_name + "'s faster times are: " + str(sorted(set([santize(t) for t in sarah_data1]))[0:3])) ###2. 使用字典关联数据,改写get_coach_data(),将print语句中的str(……)写入函数 ## ##def get_coach_data(filename): ## try: ## with open(filename) as f: ## data=f.readline() ## temp=data.strip().split(',') ## return({'Name':temp.pop(0), ## 'Dob':temp.pop(0), ## 'Times':str(sorted(set([santize(t) for t in temp]))[0:3])}) ## except IOError as ioerr: ## print("File Error: "+str(ioerr)) ## return(None) ##james_data2 = get_coach_data('james2.txt') ##julie_data2 = get_coach_data('julie2.txt') ##mikey_data2 = get_coach_data('mikey2.txt') ##sarah_data2 = get_coach_data('sarah2.txt') ## ##print(james_data2['Name']+"'s fastest times are: "+james_data2['Times']) ##print(julie_data2['Name']+"'s fastest times are: "+julie_data2['Times']) ##print(mikey_data2['Name']+"'s fastest times are: "+mikey_data2['Times']) ##print(sarah_data2['Name']+"'s fastest times are: "+sarah_data2['Times']) #3. 使用class定义类,类中的方法也用def进行定义 ##class Athlete: ## def __init__(self, a_name, a_dob=None, a_times=[]):#每个定义类的特殊方法,控制如何初始化对象 ## #The code to initialize a "Athlete" object. ## self.name=a_name ## self.dob=a_dob ## self.times=a_times ## def top3(self): ## #santize()不用修改 ## return(sorted(set([santize(t) for t in self.times]))[0:3]) ## ## #在print调试完成后再次添加新的方法 ## def add_time(self,timevalue): ## return(self.times.append(timevalue)) ## def add_times(self,list_of_time): ## return(self.times.extend(list_of_time)) ## ###get_coach_data()需要修改 ##def get_coach_data(filename): ## try: ## with open(filename) as f: ## data=f.readline() ## temp=data.strip().split(',') ## return(Athlete(temp.pop(0),temp.pop(0),temp)) ## except IOError as ioerr: ## print("File Error: "+str(ioerr)) ## return(None) ##james_data3=get_coach_data('james2.txt') ##print(james_data3.name+"'s fastest times are: "+str(james_data3.top3())) ##julie_data3=get_coach_data('julie2.txt') ##print(julie_data3.name+"'s fastest times are: "+str(julie_data3.top3())) ##mikey_data3=get_coach_data('mikey2.txt') ##print(mikey_data3.name+"'s fastest times are: "+str(mikey_data3.top3())) ##sarah_data3=get_coach_data('sarah2.txt') ##print(sarah_data3.name+"'s fastest times are: "+str(sarah_data3.top3())) #4. 继承python内置的list(Athlete类实际上只是一个列表,只是增加了一些属性) #将原来的Athlete类代码替换为AthleteList类,继承自list class AthleteList(list): def __init__(self,a_name,a_dob=None,a_times=[]): self.name=a_name self.dob=a_dob self.extend(a_times) def top3(self): return(sorted(set([santize(t) for t in self]))[0:3]) #相应的修改get_coach_data()代码 def get_coach_data(filename): try: with open(filename) as f: data = f.readline() temp = data.strip().split(',') return(AthleteList(temp.pop(0),temp.pop(0),temp)) except IOError as ioerr: print('File Error: '+str(ioerr)) return(None) james_data4=get_coach_data('james2.txt') print(james_data4.name+"'s fastest times are: "+str(james_data4.top3())) julie_data4=get_coach_data('julie2.txt') print(julie_data4.name+"'s fastest times are: "+str(julie_data4.top3())) mikey_data4=get_coach_data('mikey2.txt') print(mikey_data4.name+"'s fastest times are: "+str(mikey_data4.top3())) sarah_data4=get_coach_data('sarah2.txt') print(sarah_data4.name+"'s fastest times are: "+str(sarah_data4.top3())) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |