案例:規(guī)范時(shí)間序列3-使用類與子類

[TOC]

使用類


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):
        #顯示最短的三個(gè)時(shí)間
        return(sorted(set([sanitize(t) for t in self.times]))[0:3])

    def add_time(self,time_value):
        #增加一個(gè)計(jì)時(shí)值
        self.times.append(time_value)
        
    def add_times(self,list_of_times):
        #增加一個(gè)或多個(gè)計(jì)時(shí)值
        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()))

運(yùn)行結(jié)果:

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):
        #顯示最短的三個(gè)時(shí)間
        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()))

運(yùn)行結(jié)果:

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']
>>> 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容