import pickle
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(t) for t in self]))[0:3])
def readdata(file):
try:
with open(file) as datafile:
data = datafile.readline()
data = data.strip().split(',')
templ = AthleteList(data.pop(0), data.pop(0), data)
return templ
except IOError as ioerr:
print('File error:' + str(ioerr))
return (None)
def put_to_store(files_list):
all_athletes = {}
for each_file in files_list:
ath = readdata(each_file)
all_athletes[ath.name] = ath
try:
with open('athletes.pickle', 'wb') as athf:
pickle.dump(all_athletes, athf)
except IOError as ioerr:
print('File error(put_and_store):' + str(ioerr))
return (all_athletes)
def get_from_store():
all_athletes = {}
try:
with open('athletes.pickle', 'rb') as athf:
all_athletes = pickle.load(athf)
except IOError as ioerr:
print('File error(put_and_store):' + str(ioerr))
return (all_athletes)
print(dir())
the_files = ['.\\hfpy_ch6_data\\james2.txt',
'.\\hfpy_ch6_data\\julie2.txt',
'.\\hfpy_ch6_data\\mikey2.txt',
'.\\hfpy_ch6_data\\sarah2.txt']
data = put_to_store(the_files)
print(data)
for each_athlete in data:
print(data[each_athlete].name + ' ' + data[each_athlete].dob)
data_copy = get_from_store()
print(data_copy)
for each_athlete in data_copy:
print(data_copy[each_athlete].name + ' ' + data_copy[each_athlete].dob)