[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])})
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()
data_list = data.strip().split(',')
return({'Name':data_list.pop(0),
'DOB':data_list.pop(0),
'Time':str( sorted( set([sanitize(t) for t in data_list] ) )[0:3])})
except IOError as err:
print("file err:" + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print( julie['Name'] + "'s faster time are:" + julie['Time'])
print( james['Name'] + "'s faster time are:" + james['Time'])
print( sarah['Name'] + "'s faster time are:" + sarah['Time'])
print( mikey['Name'] + "'s faster time are:" + mikey['Time'])
2- 返回类 return ( Athlete(datalist.pop(0), datalist.pop(0), datalist))
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 Athlete:
def __init__(self, a_name, a_dob = None, a_time = []):
self.name = a_name
self.dob = a_dob
self.times = a_time def top3(self):
return( sorted(set([sanitize(t) for t in self.times]))[0:3] ) def add_time(self,time_value):
self.times.append(time_value) def add_times(self,list_of_times):
self.times.extend(list_of_times) def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline();
datalist = data.strip().split(',')
return ( Athlete(datalist.pop(0), datalist.pop(0), datalist))
except IOError as err:
print('file err:' + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print(julie.name + "'s faster times are:" + str(julie.top3()))
print(james.name + "'s faster times are:" + str(james.top3()))
print(sarah.name + "'s faster times are:" + str(sarah.top3()))
print(mikey.name + "'s faster times are:" + str(mikey.top3())) vera = Athlete('vera vi')
vera.add_time('1.30')
print(vera.top3())
vera.add_times(['1.00','0.3','1.2'])
print(vera.top3())
3- 继承python内置list
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 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) #vera = Athletelist("vera vi")
#vera.append('1.31')
#print(vera.top3())
#vera.extend(["1","2","0"])
#print(vera.top3()) def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline();
datalist = data.strip().split(',')
return ( Athletelist(datalist.pop(0), datalist.pop(0), datalist))
except IOError as err:
print('file err:' + str(err))
return(none) julie = get_coach_data('julie2.txt')
james = get_coach_data('james2.txt')
sarah = get_coach_data('sarah2.txt')
mikey = get_coach_data('mikey2.txt') print(julie.name + "'s faster times are:" + str(julie.top3()))
print(james.name + "'s faster times are:" + str(james.top3()))
print(sarah.name + "'s faster times are:" + str(sarah.top3()))
print(mikey.name + "'s faster times are:" + str(mikey.top3()))
[Head First Python]6. 定制数据对象:打包代码与数据的更多相关文章
- ch6-定制数据对象(打包代码和数据)
为了看出数据属于哪个选手,教练向各个选手的数据文件中添加了标识数据:选手全名,出生日期,计时数据. 例如:sarah文件的数据更新为: Sarah Sweeney,2002-6-17,2:58,2.5 ...
- Tcp 数据对象传输接口对象设计
输入是一个对象inputObj,接口对象.Send(inputObj),对端接收之后解包成outputObj(与inputObj应相同),触发onPackageReceive事件 事件 public ...
- Oracle 常用的SQL语法和数据对象
一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSE ...
- Oracle---常用SQL语法和数据对象
1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字 ...
- mysql数据对象
学习目标: 了解掌握常见的几种数据库对象 学会如何创建具体的数据对象 mysql 常见的数据对象有哪些: DataBase/Schema Table Index View/Trigger/ ...
- 用python pickle库来存储数据对象
pickling有一个更常用的叫法是serialization,它是指把python对象转化成字节流byte stream, unpickling就是把byte stream转换成对象.python的 ...
- python学习_应用pickle模块封装和拆封数据对象
学习文件数据处理的时候了解到有pickle模块,查找官方文档学习了一些需要用到的pickle内容. 封装是一个将Python数据对象转化为字节流的过程,拆封是封装的逆操作,将字节文件或字节对象中的字节 ...
- python pickle模块的使用/将python数据对象序列化保存到文件中
# Python 使用pickle/cPickle模块进行数据的序列化 """Python序列化的概念很简单.内存里面有一个数据结构, 你希望将它保存下来,重用,或者发送 ...
- Python学习笔记 | 关于python数据对象 hashable & unhashable 的理解
文章目录 写在前面 hashable & unhashable mutable & immutable 实例检测 后续思考 参考文章 写在前面 Hash(哈希.散列)是一个将大体量数据 ...
随机推荐
- [Mugeda HTML5技术教程之13]链接的添加方式
在广告主的需求中,有很多情况下需要在动画中添加一些外部链接.这份文档就在Mugeda动画中添加外部链接的方式,做一下梳理. 1.通过点击触发的链接 就是要用户点击屏幕来触发链接的情况,这是推荐使用的方 ...
- redhat 5下源码安装nginx服务
首先确保机器中已安装 gcc c++,libtool等工具,保证可执行源码安装 A.为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Co ...
- 养成代码注释习惯,帮助你更好使用NetBeans导航器
在使用NetBeans编写php代码时,为了在一个类中,或者在方法库文件中快速找到你想要找的函数或方法,通常我们会使用NetBeans的导航器. 我们看一个导航器的事例: 大家知道,在php中代码习惯 ...
- 粗窥STARTUP.A51和INIT.A51
也许大家曾经注意过使用Keil C51来编译链接生成目标代码之后,在我们的主程序之前有些代码不是我们写的,它们从哪里来的? Keil C51的\C51\LIB目录下有STARTUP.A51和INIT. ...
- 使用 C# 编写简易 ASP.NET Web 服务器
原文 http://www.cnblogs.com/lcomplete/p/use-csharp-write-aspnet-web-server.html 如果你想获得更好的阅读体验,可以前往我在 g ...
- VS2010中使用QtOpenGL出现 unresolved external symbol __imp__glClear@4 referenced in function之类的错误
描述: 链接了QtOpenGL4.lib QtOpend4.lib的库啊,居然还是发生此错误. 原因是没有链接OpenGL32.lib这个库.所以,要添加这个lib 重新rebuild的一下,此类的错 ...
- 【转】【Android】HAL分析
原文网址:http://www.cnblogs.com/lcw/p/3335505.html HAL概述 以下是基于android4.0.3,对应其他低版本的代码,可能有所差异,但基本大同小异. An ...
- 《Algorithms 4th Edition》读书笔记——3.1 符号表(Elementary Symbol Tables)-Ⅳ
3.1.4 无序链表中的顺序查找 符号表中使用的数据结构的一个简单选择是链表,每个结点存储一个键值对,如以下代码所示.get()的实现即为遍历链表,用equals()方法比较需被查找的键和每个节点中的 ...
- [TI DLP Buglist]data type error in illum_EnableIllumination function
I am debuging my code today, I find when my code is running, it's stop at illum_EnableIllumination() ...
- RTP/RTCP/RTSP/RSVP/SDP
RTP Real-time Transport Protocol)是用于Internet上针对多媒体数据流的一种传输层协议.RTP协议详细说明了在互联网上传递音频和视频的标准数据包格式.RTP协议常用 ...