【Head First Python-第六章-定制数据对象】学习笔记

1.字典-内置的数据结构,允许数据值与键关联,有两种方法创建

2.class关键字定义一个类,类方法参数第一个必须为self

代码:

用到的txt文件:sarah.txt

Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55


类:Athlete.py

import sanitize
#11111111111111原始方法
"""
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([sanitize.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)
"""
#2222222222通过继承列表类
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.sanitize(t) for t in self]))[0:3]) #数据本身就是计时数据   

数据获取函数:getData.py

#from Athlete import Athlete  #导入从零构建的类包
from Athlete import AthleteList #导入进程类包  
#1111111数据字典来获得数据
"""
import sanitize
def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
        templ = data.strip().split(',')  #临时列表存放数据
        return({'Name':templ.pop(0),
                'DOB':templ.pop(0),
                'Times':str(sorted(set([sanitize.sanitize(t) for t in templ]))[0:3])})
    except IOError as ioerr:
        print('File error:' + str(ioerr))
        return(None)
"""

#2222222通过类来获得数据
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))  #返回实例化对象
        return(AthleteList(templ.pop(0), templ.pop(0), templ)) #使用继承类实例化对象
    except IOError as ioerr:
        print('File error:' + str(ioerr))
        return(None)

主函数:dataProce.py

import getData
import sanitize
import os
os.chdir('C:/Users/Administrator/Desktop/HeadFirstPython/chapter6')
sarah = getData.get_coach_data('sarah.txt')
#11111111原始方法
"""
(sarah_name, sarah_dob) = sarah.pop(0), sarah.pop(0)
print(sarah_name + "'s fastest times are:" +
      str(sorted(set([sanitize.sanitize(t) for t in sarah]))[0:3]))
"""
#22222222字典来存储数据访问
""""
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([sanitize.sanitize(t) for t in sarah_data['Times']]))[0:3]))
"""
#33333333在读取文件的时候就建立字典
"""
print(sarah['Name'] + "'s fastest times are:" + sarah['Times'])
"""
#44444444使用Athlete类来实例化对象
print(sarah.name + "'s fastest times are:" + str(sarah.top3()))

数据格式处理函数:sanitize.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)
    



你可能感兴趣的:(Python)