Page 211 -
P. 211

custom data objects


                           Code Magnets


                           Let’s look at the code to implement the strategy outlined at the bottom of the previous page. For
                           now, let’s concentrate on Sarah’s data. Rearrange the code magnets at the bottom of this page to
                           implement the list processing required to extract and process Sarah’s three fastest times from Coach
                           Kelly’s raw data.
                           Hint: the pop() method removes and returns a data item from the specified list location.


                           def sanitize(time_string):
                                                               The “sanitize()” function is as
                               if '-' in time_string:
                                                               it was in Chapter 5.
                                   splitter = '-'
                               elif ':' in time_string:
                                   splitter = ':'
                               else:
                                   return(time_string)
                               (mins, secs) = time_string.split(splitter)
                               return(mins + '.' + secs)
                                                                     The “get_coach_data()” function is
                           def get_coach_data(filename):             also from the last chapter.
                               try:
                                   with open(filename) as f:
                                       data = f.readline()
                                   return(data.strip().split(','))
                               except IOError as ioerr:
                                   print('File error: ' + str(ioerr))
                                   return(None)
             Rearrange the
              magnets here.












                                                                      (sarah_name, sarah_dob)
                               sarah       "'s fastest times are: " +                                 =
                                                                              print(sarah_name +
                   get_coach_data('sarah2.txt')          =
                                                                     sarah.pop(0), sarah.pop(0)
        str(sorted(set([sanitize(t) for t in sarah]))[0:3]))
                                                                                      you are here 4    175
   206   207   208   209   210   211   212   213   214   215   216