Page 322 -
P. 322

data to dictionary




                             Let’s create a new method in your AthleteList class. Called to_dict(), your new
                             method needs to convert the class’s attribute data (name, DOB, and top3) into a dictionary. Be
                             sure to decorate your new method with @property, so that it appears to be a new attribute to
                             users of your class.


            Decorate your       @property
            new method with
           “@property”.        def as_dict(self):   Create a new method.
                                   return({‘Name’:  self.name,

                                           ‘DOB’:   self.dob,   Return a dictionary of the object’s
                                                                data attributes.
                                           ‘Top3’:  self.top3})


                               Did you remember to use “self”?














                                                         Do this!





                                         As well as updating your AthleteList
                                         class code, be sure to change cgi-bin/
                                         generate-data.py to return a
                                         dictionary, rather than the object instance,
                                         when servicing its web request.

                                         While you’re making changes, adjust the
                                         coachapp.py app code to include the
                                         athlete’s name and DOB values in the
                                         second dialog’s title.






           286    Chapter 8
   317   318   319   320   321   322   323   324   325   326   327