案例:规范时间序列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):
        #显示最短的三个时间
        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']
>>> 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 父类引用指向子类对象指的是: 例如父类Animal,子类Cat,Dog。其中Animal可以是类也可以是接口,Ca...
    木有鱼丸啦阅读 987评论 0 4
  • 本页包含内容: [TOC] 类里面的所有存储型属性——包括所有继承自父类的属性——都必须在构造过程中设置初始值。 ...
    伍哥___阅读 552评论 0 0
  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 4,144评论 1 10
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,572评论 1 32
  • 整理来自互联网 1,JDK:Java Development Kit,java的开发和运行环境,java的开发工具...
    Ncompass阅读 1,612评论 0 6

友情链接更多精彩内容