Page 231 -
P. 231

custom data objects



                                             Here’s your code (except for the santize() function, which doesn’t
                                             need to change). With your pencil, write code to define the Athlete
                                             class. In addition to the __init__() method, define a new method
                                             called top3() that, when invoked, returns the top three times.
                                             Be sure to adjust the get_coach_data() function to return an
                                             Athlete object as opposed to a dictionary, and don’t forget to amend
                                             your print() statements, too.
                                                                                      Write your Athlete
                                                                                      class code here.

























                def get_coach_data(filename):
                    try:
                        with open(filename) as f:                  What needs to change here to ensure
                            data = f.readline()                    this function returns an Athlete object
                        templ = data.strip().split(',')            as opposed to a dictionary?
                        return({'Name' : templ.pop(0),
                                'DOB'  : templ.pop(0),
                                'Times': str(sorted(set([sanitize(t) for t in templ]))[0:3])})
                    except IOError as ioerr:
                        print('File error: ' + str(ioerr))
                        return(None)
                                                                                This line of code

                                                                                needs to change, too.
                james = get_coach_data('james2.txt')

                print(james['Name'] + "'s fastest times are: " + james['Times'])


                                                                                      you are here 4    195
   226   227   228   229   230   231   232   233   234   235   236