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 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 readdata(file):
try:
with open(file) as datafile:
data = datafile.readline()
data = data.strip().split(',')
templ = Athlete(data.pop(0), data.pop(0), data)
return templ
except IOError as ioerr:
print('File error:' + str(ioerr))
return (None)
james = readdata('.\\hfpy_ch6_data\\james2.txt')
julie = readdata('.\\hfpy_ch6_data\\julie2.txt')
mikey = readdata('.\\hfpy_ch6_data\\mikey2.txt')
sarah = readdata('.\\hfpy_ch6_data\\sarah2.txt')
print(james.name + "'s fastest times are:" + str(james.top3()))
print(julie.name + "'s fastest times are:" + str(julie.top3()))
print(mikey.name + "'s fastest times are:" + str(mikey.top3()))
print(sarah.name + "'s fastest times are:" + str(sarah.top3()))
saeah = Athlete('Sarah Sweeney', '2002-6-17', ['2:58', '2.58', '2:39'])
james = Athlete('James Jones')
print(type(saeah))
print(type(james))
print(saeah)
print(james)
print(saeah.name)
print(james.name)
print(saeah.dob)
print(james.dob)
print(saeah.times)
print(james.times)
vera = Athlete('Vera Vi')
vera.add_time('1.31')
print(vera.top3())
vera.add_times(['2.22', '1-22', '2:22'])
print(vera.top3())