案例:规范时间序列3-使用类与子类

文章目录

使用类


import os
os.chdir('C:\\Users\\alibaba\\Desktop\\Headfirstpython\\handledata2')

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_times=[]):
        self.name = a_name
        self.dob = a_dob
        self.times = a_times
        
    
    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,separator = ','):
    try:
        with open(filename,"r") as file:
            data = file.readline()
        temp = data.strip().split(separator)
        return (Athlete(temp.pop(0),temp.pop(0),temp))

    except IOError as err:
        print('File Error:' + str(ioerr))
        return(None)


james = get_coach_data('james2.txt')
print(james.name+"'s fastest times are:"+str(james.top3()))

运行结果:

James Lee's fastest times are:['2.01', '2.16', '2.22']

使用子类

import os
os.chdir('C:\\Users\\alibaba\\Desktop\\Headfirstpython\\handledata2')

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.times = a_times
        self.extend(a_times)
        
    def top3(self):
        #显示最短的三个时间
        return(sorted(set([sanitize(t) for t in self]))[0:3])
        
    
def get_coach_data(filename,separator = ','):
    try:
        with open(filename,"r") as file:
            data = file.readline()
        temp = data.strip().split(separator)
        return (AthleteList(temp.pop(0),temp.pop(0),temp))

    except IOError as err:
        print('File Error:' + str(ioerr))
        return(None)


james = get_coach_data('james2.txt')
print(james.name+"'s fastest times are:"+str(james.top3()))

运行结果:

James Lee's fastest times are:['2.01', '2.16', '2.22']
>>> james
['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22', '2-01', '2.01', '2:16']
>>> james.name
'James Lee'
>>> james.dob
'2002-3-14'
>>> james.extend(["1","1.2"])
>>> james
['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22', '2-01', '2.01', '2:16', '1', '1.2']
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值