相同功能,演进实现

数据文件

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. 定制数据对象:打包代码与数据的更多相关文章

  1. ch6-定制数据对象(打包代码和数据)

    为了看出数据属于哪个选手,教练向各个选手的数据文件中添加了标识数据:选手全名,出生日期,计时数据. 例如:sarah文件的数据更新为: Sarah Sweeney,2002-6-17,2:58,2.5 ...

  2. Tcp 数据对象传输接口对象设计

    输入是一个对象inputObj,接口对象.Send(inputObj),对端接收之后解包成outputObj(与inputObj应相同),触发onPackageReceive事件 事件 public ...

  3. Oracle 常用的SQL语法和数据对象

    一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……);  INSE ...

  4. Oracle---常用SQL语法和数据对象

    1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字 ...

  5. mysql数据对象

      学习目标:   了解掌握常见的几种数据库对象 学会如何创建具体的数据对象   mysql 常见的数据对象有哪些: DataBase/Schema Table Index View/Trigger/ ...

  6. 用python pickle库来存储数据对象

    pickling有一个更常用的叫法是serialization,它是指把python对象转化成字节流byte stream, unpickling就是把byte stream转换成对象.python的 ...

  7. python学习_应用pickle模块封装和拆封数据对象

    学习文件数据处理的时候了解到有pickle模块,查找官方文档学习了一些需要用到的pickle内容. 封装是一个将Python数据对象转化为字节流的过程,拆封是封装的逆操作,将字节文件或字节对象中的字节 ...

  8. python pickle模块的使用/将python数据对象序列化保存到文件中

    # Python 使用pickle/cPickle模块进行数据的序列化 """Python序列化的概念很简单.内存里面有一个数据结构, 你希望将它保存下来,重用,或者发送 ...

  9. Python学习笔记 | 关于python数据对象 hashable & unhashable 的理解

    文章目录 写在前面 hashable & unhashable mutable & immutable 实例检测 后续思考 参考文章 写在前面 Hash(哈希.散列)是一个将大体量数据 ...

随机推荐

  1. CC2530 PWM波形产生。

    1.使用TIM3_CC1,相关联引脚P1_7 #define GPIOPWM() do{P1SEL |= 0x80;}while(0);#define GPIOCLOSEPWM() do{P1SEL  ...

  2. Java 学习 第六篇;接口

    1: 接口定义修饰符 interface 接口名{ 常量定义: 抽象方法定义:}修饰符 interface 接口名 extends 父接口表{ 常量定义: 抽象方法定义:}-> 修饰符可以是pu ...

  3. Scala开发环境搭建

    Ubuntu14.04下Scala开发环境搭建. 1:安装JDK,jdk-6u45-linux-x64.bin 将其安装到/usr/lib/jvm/jdk1.6.0_45/. 2:安装Scala,下载 ...

  4. LeetCode_Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  5. 微软在MSDN中更新了Win8.1批量授权版镜像(中文版更新完毕&版本说明)

    微软在MSDN中更新了Win8.1大客户专业版和企业版镜像,零售版镜像(即专业版+核心版二合一镜像)没有更新,依然是9月份发布的版本.已证实,新的批量授权版镜像是集成了GA Rollup A更新,并且 ...

  6. Android 中的接口回调

    http://blog.csdn.net/wangjinyu501/article/details/22052187   在Android中到处可见接口回调机制,尤其是UI事件处理方面.举一个最常见的 ...

  7. Grok 正则捕获

    Grok 正则捕获: \s+(?<request_time>\d+(?:\.\d+)?)\s+ 回顾下: (?:pattern) 匹 配 pattern 但不获取匹配结果,也就是说这是一个 ...

  8. 如何允许外网可以连接mysql数据库

    1.首先检查mysql所在服务器的防火墙,如果限制了外网对3306端口的连接,那么放开限制Linux服务器中执行 iptables -L   可以查看当前的防火墙规则iptables -F   可以清 ...

  9. 【转】Android源码下载过程的一些注意事项

    原文网址:http://www.360doc.com/content/14/0113/11/11948835_344809459.shtml 其它一些事项说明: 1.在源代码下载过程中,我们在源代码下 ...

  10. STL适配器的初步理解

    c++中的适配器有三种:容器适配器,迭代器适配器,函数适配器.下面一一介绍: 1.容器适配器:因为这些容器都是基于其他标准容器实现的所以叫做容器的适配器,具体的有stack,queue,priorit ...