HeadFirstPython---------(四)第六章(定制数据对象__打包代码与数据)
2017-06-08 22:25
676 查看
1、
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/7418f0bb179e9aff3c6a90e486e3a89e)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/ae91d882fd56da54bbd1f94ee6425a30)
实现下面的效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/e2b2f10c7d3383c89e845f33eceb5d0d)
代码如下:
字典这是一个内置的数据结构(内置于Python中),允许将数据与键而不是数字关联。这样可以使内存中的数据与实际数据的结构保持一致。
代码如下:
sanitize1.py中的代码如下:
get_coach_data1.py中的代码如下
实验结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/d2548edf7901ef0ddaef663e7fd2492b)
3、对2中代码的进一步 的改善。(针对的是重复性代码)
sanitize1.py中的代码为:
chapter6.py主程序的代码为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/ac2960b59f2654ac9efbeae44ee58eb1)
4、类的应用:
代码如下:
import os
import sanitize1
os.chdir("D:/pythontest/HeadFirstPython/chapter6");
class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name=a_name;
self.dob=a_dob;
self.times=a_times;
def top3(self):
return (sorted(set([sanitize1.sanitize(t) for t in self.times]))[0:3]);
def get_coach_data(filename):
try:
with open(filename) as f:
data=f.readline();
templ=data.strip().split(',')
return (Athlete(templ.pop(0),templ.pop(0),templ));
except IOError as ioerr:
print('File error:'+str(ioerr));
return(None);
try:
james=get_coach_data('james2.txt');
julie=get_coach_data('julie2.txt');
mikey=get_coach_data('mikey2.txt');
sarah=get_coach_data('sarah2.txt');
print(james.name+"'s fastest times are:"+str(james.top3()));
print(julie.name+"'s fastest times are:"+str(julie.top3()));
print(mikey.name+"'s fastest times are:"+str(mikey.top3()));
print(sarah.name+"'s fastest times are:"+str(sarah.top3()));
except IOError as err:
print("File error"+str(err));运行结果为:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/24/2175a374804d33da1903305612efae9f)
5、继承内置类的子类:
代码如下:
import os
os.chdir("D:/pythontest/HeadFirstPython/chapter6");
def sanitize(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);
class AthleteList(list):
def __init__(self,a_name,a_dob=None,a_times=[]):
list.__init__([]);
self.name=a_name;
self.dob=a_dob;
self.extend(a_times);
def top3(self):
return (sorted(set([sanitize(t) for t in self]))[0:3]);
def get_coach_data(filename):
try:
with open(filename) as f:
data=f.readline();
templ=data.strip().split(','
da2d
);
return (AthleteList(templ.pop(0),templ.pop(0),templ));
except IOError as ioerr:
print('File error:'+str(ioerr));
return(None);
try:
james=get_coach_data('james2.txt');
julie=get_coach_data('julie2.txt');
mikey=get_coach_data('mikey2.txt');
sarah=get_coach_data('sarah2.txt');
print(james.name+"'s fastest times are:"+str(james.top3()));
print(julie.name+"'s fastest times are:"+str(julie.top3()));
print(mikey.name+"'s fastest times are:"+str(mikey.top3()));
print(sarah.name+"'s fastest times are:"+str(sarah.top3()));
except IOError as err:
print("File error"+str(err));
运行结果为:
实现下面的效果:
代码如下:
import os os.chdir("D:/pythontest/HeadFirstPython/chapter6"); def sanitize(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); 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); try: james=get_coach_data('james2.txt'); julie=get_coach_data('julie2.txt'); mikey=get_coach_data('mikey2.txt'); sarah=get_coach_data('sarah2.txt'); (james_name,james_dob)=james.pop(0),james.pop(0);# (julie_name,julie_dob)=julie.pop(0),julie.pop(0); (mikey_name,mikey_dob)=mikey.pop(0),mikey.pop(0); (sarah_name,sarah_dob)=sarah.pop(0),sarah.pop(0); print(james_name+"'s fastest times are:"+str(sorted(set([sanitize(time_string) for time_string in james]))[0:3])); print(julie_name+"'s fastest times are:"+str(sorted(set([sanitize(time_string) for time_string in julie]))[0:3])); print(mikey_name+"'s fastest times are:"+str(sorted(set([sanitize(time_string) for time_string in mikey]))[0:3])); print(sarah_name+"'s fastest times are:"+str(sorted(set([sanitize(time_string) for time_string in sarah]))[0:3])); except IOError as err: print("File error"+str(err));2、改善,使用字典关联数据:
字典这是一个内置的数据结构(内置于Python中),允许将数据与键而不是数字关联。这样可以使内存中的数据与实际数据的结构保持一致。
代码如下:
sanitize1.py中的代码如下:
def sanitize(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);
get_coach_data1.py中的代码如下
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);read.py主程序代码如下:
import os
import sanitize1
import get_coach_data1 os.chdir("D:/pythontest/HeadFirstPython/chapter6"); try: james=get_coach_data1.get_coach_data('james2.txt'); julie=get_coach_data1.get_coach_data('julie2.txt'); mikey=gget_coach_data1.et_coach_data('mikey2.txt'); sarah=get_coach_data1.get_coach_data('sarah2.txt'); #利用字典的表达形式 james_data={}; james_data['Name']=james.pop(0); james_data['DOB']=james.pop(0); james_data['Times']=james; print(james_data['Name']+"'s fastest times are: "+str(sorted(set([sanitize1.sanitize(t)for t in james_data['Times']]))[0:3])); julie_data={}; julie_data['Name']=julie.pop(0); julie_data['DOB']=julie.pop(0); julie_data['Times']=julie; print(julie_data['Name']+"'s fastest times are: "+str(sorted(set([sanitize1.sanitize(t)for t in julie_data['Times']]))[0:3])); mikey_data={}; mikey_data['Name']=mikey.pop(0); mikey_data['DOB']=mikey.pop(0); mikey_data['Times']=mikey; print(mikey_data['Name']+"'s fastest times are: "+str(sorted(set([sanitize1.sanitize(t)for t in mikey_data['Times']]))[0:3])); sarah_data={}; sarah_data['Name']=sarah.pop(0); sarah_data['DOB']=sarah.pop(0); sarah_data['Times']=sarah; print(sarah_data['Name']+"'s fastest times are: "+str(sorted(set([sanitize1.sanitize(t)for t in sarah_data['Times']]))[0:3]));
实验结果为:
3、对2中代码的进一步 的改善。(针对的是重复性代码)
sanitize1.py中的代码为:
import osget_coach_data1.py中的代码是:
os.chdir("D:/pythontest/HeadFirstPython/chapter5");
def sanitize(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);
import sanitize1 def get_coach_data(filename): try: with open(filename) as f: data=f.readline(); templ=data.strip().split(',') name_data={}; name_data['Name']=templ.pop(0); name_data['DOB']=templ.pop(0); name_data['Times']=str(sorted(set([sanitize1.sanitize(t) for t in templ]))[0:3]); return name_data; except IOError as ioerr: print('File error:'+str(ioerr)); return(None);
chapter6.py主程序的代码为:
import os import get_coach_data1 os.chdir("D:/pythontest/HeadFirstPython/chapter6"); try: james=get_coach_data1.get_coach_data('james2.txt'); julie=get_coach_data1.get_coach_data('julie2.txt'); mikey=get_coach_data1.get_coach_data('mikey2.txt'); sarah=get_coach_data1.get_coach_data('sarah2.txt'); print(james['Name']+"'s fastest times are:"+james['Times']); print(julie['Name']+"'s fastest times are:"+julie['Times']); print(mikey['Name']+"'s fastest times are:"+mikey['Times']); print(sarah['Name']+"'s fastest times are:"+sarah['Times']); except IOError as err: print("File error"+str(err));运行结果为:
4、类的应用:
代码如下:
import os
import sanitize1
os.chdir("D:/pythontest/HeadFirstPython/chapter6");
class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name=a_name;
self.dob=a_dob;
self.times=a_times;
def top3(self):
return (sorted(set([sanitize1.sanitize(t) for t in self.times]))[0:3]);
def get_coach_data(filename):
try:
with open(filename) as f:
data=f.readline();
templ=data.strip().split(',')
return (Athlete(templ.pop(0),templ.pop(0),templ));
except IOError as ioerr:
print('File error:'+str(ioerr));
return(None);
try:
james=get_coach_data('james2.txt');
julie=get_coach_data('julie2.txt');
mikey=get_coach_data('mikey2.txt');
sarah=get_coach_data('sarah2.txt');
print(james.name+"'s fastest times are:"+str(james.top3()));
print(julie.name+"'s fastest times are:"+str(julie.top3()));
print(mikey.name+"'s fastest times are:"+str(mikey.top3()));
print(sarah.name+"'s fastest times are:"+str(sarah.top3()));
except IOError as err:
print("File error"+str(err));运行结果为:
5、继承内置类的子类:
代码如下:
import os
os.chdir("D:/pythontest/HeadFirstPython/chapter6");
def sanitize(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);
class AthleteList(list):
def __init__(self,a_name,a_dob=None,a_times=[]):
list.__init__([]);
self.name=a_name;
self.dob=a_dob;
self.extend(a_times);
def top3(self):
return (sorted(set([sanitize(t) for t in self]))[0:3]);
def get_coach_data(filename):
try:
with open(filename) as f:
data=f.readline();
templ=data.strip().split(','
da2d
);
return (AthleteList(templ.pop(0),templ.pop(0),templ));
except IOError as ioerr:
print('File error:'+str(ioerr));
return(None);
try:
james=get_coach_data('james2.txt');
julie=get_coach_data('julie2.txt');
mikey=get_coach_data('mikey2.txt');
sarah=get_coach_data('sarah2.txt');
print(james.name+"'s fastest times are:"+str(james.top3()));
print(julie.name+"'s fastest times are:"+str(julie.top3()));
print(mikey.name+"'s fastest times are:"+str(mikey.top3()));
print(sarah.name+"'s fastest times are:"+str(sarah.top3()));
except IOError as err:
print("File error"+str(err));
运行结果为:
相关文章推荐
- [Head First Python]6. 定制数据对象:打包代码与数据
- Python初入门(六)(Head First Python 第六章 打包代码与数据 ,字典,类与继承)
- 《Head First Python》笔记 第六章 定制数据对象
- 【Head First Python-第六章-定制数据对象】学习笔记
- Head First Python ch_6 定制数据对象
- 《head first python》——定制数据对象
- 六、定制数据对象(Python的面向对象) ----- 打包代码与数据
- Head First Python(定制数据对象)
- Head Frist Python 读书笔记 第六章 定制数据对象
- Head First Python 学习笔记-Chapter6:自定义数据对象:字典与类
- 定制数据对象--打包代码与数据
- Head First Python代码实践(第六章)有注释
- Python类的定义使用 《Head First Python》第六章
- 《Head First Programming》---python 4_文件和数组中的数据
- Head First Python 学习笔记-Chapter4:持久化--将数据写入文件
- Head First Python(如何向PYPI发布你的代码)学习笔记
- head first python 第二章代码
- 数据存储:从pickle到sqlite 《Head First Python》第九章
- 《Head First Programming》---python 2_文本数据
- Head First Python(推导数据)