相同功能,演进实现

数据文件

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. ZOJ 3818 Pretty Poem

    暴力模拟 细节处理很重要... #include <iostream> #include <cstring> #include <cstdio> using nam ...

  2. 关于box-sizing

    http://www.zhangxinxu.com/css3/css3-box-sizing.php box-sizing:border-box; -o-box-sizing:border-box; ...

  3. DataGrid导出excel

    DAL://产品信息导出——LPH public DataTable ExportRelease(string type) { string sql = "SELECT [ProductID ...

  4. 统计维护<第四篇>

    SQL Server允许用户手工地控制单独数据库中的统计维护.SQL Server的4个主要的控制紫铜统计的维护的配置如下: 在无索引的列上新建统计(自动创建统计): 更新现有统计(自动更新统计): ...

  5. 【转】 i2c驱动调试经验

    原文网址:http://blog.csdn.net/cmm20071020/article/details/7179958 把一个i2c驱动从2.6.21升级到2.6.39 上网查到一篇帖子,讲了驱动 ...

  6. ASP.NET应用程序和ASP.NET网站所共有的文件: App_Browsers 等

    App_Browsers  包含 ASP.NET 用于标识个别浏览器并确定其功能的浏览器定义 (.browser) 文件.有关更多信息,请参见浏览器定义文件架构(browsers 元素)和如何:在 A ...

  7. Visual Assist X在Windows 8.1下出现中文乱码的解决方法

    这主要是输入法造成的,我的输入法中有US.中文.搜狗输入法三个输入法:通过搜狗输入法管理器把“中文”去掉,或者通过语言首选项把“中文”去掉就不会在出现乱码. 这个办法的思路来自于http://www. ...

  8. Codeforces243C-Colorado Potato Beetle(离散化+bfs)

    Old MacDonald has a farm and a large potato field, (1010 + 1) × (1010 + 1) square meters in size. Th ...

  9. windows10 离线包安装net3.5

    找到离线镜像: 管理员命令行运行:dism.exe /online /enable-feature /featurename:netfx3 /Source:E:\sources\sxs 路径根据实际情 ...

  10. nyoj 234 吃土豆

    描述 Bean-eating * grid. Now you want to eat the beans and collect the qualities, but everyone must ob ...