From: Rob H. <rob...@gm...> - 2011-08-30 22:23:16
|
Greetings: Could anyone help me with a code simplification for this section of code? medialist = [] for phandle in self.photo_keys: photo = db.get_object_from_handle(phandle) if photo: mime_type = photo.get_mime_type() if mime_type.startswith("image"): photo_title = photo.get_description() medialist.append( (photo_title, photo) ) if not medialist: return medialist.sort() Thank you for the help, and your quick response.... -- Sincerely yours, Rob G. Healey "Always surround yourself with people that inspire you to greatness!" |
From: Gerald B. <ger...@gm...> - 2011-08-31 13:08:58
|
Maybe a little: On Tue, Aug 30, 2011 at 6:23 PM, Rob Healey <rob...@gm...> wrote: > Greetings: > > Could anyone help me with a code simplification for this section of code? > > medialist = [] > for phandle in self.photo_keys: > photo = db.get_object_from_handle(phandle) > if photo: > mime_type = photo.get_mime_type() > if mime_type.startswith("image"): > photo_title = photo.get_description() > medialist.append( (photo_title, photo) ) You can unnest the ifs here: if photo and photo.get_mime_type().startswith("image"): medialist.append( (photo.get_description(), photo) ) thanks to Python's short-circuiting. I put the tuple to be appended on a separate line for (my) readability, but this is really a matter of taste. I don't expect all would agree with that choice. > if not medialist: > return > medialist.sort() > > Thank you for the help, and your quick response.... > > -- > Sincerely yours, > Rob G. Healey > "Always surround yourself with people that inspire you to > greatness!" > -- Gerald Britton |
From: Jon C. <jo...@go...> - 2011-08-31 15:37:07
|
On 31/08/11 14:08, Gerald Britton wrote: > Maybe a little: > > On Tue, Aug 30, 2011 at 6:23 PM, Rob Healey<rob...@gm...> wrote: >> Greetings: >> >> Could anyone help me with a code simplification for this section of code? >> >> medialist = [] >> for phandle in self.photo_keys: >> photo = db.get_object_from_handle(phandle) >> if photo: >> mime_type = photo.get_mime_type() >> if mime_type.startswith("image"): >> photo_title = photo.get_description() >> medialist.append( (photo_title, photo) ) Untested (and not sure if it's "simpler" - bear in mind you have to come back to it later, or someone else does...!): media_list = sorted( (p.get_description(), p) for p in (db.get_object_from_handle(photo) for photo in self.photo_keys if photo and photo.get_mime_type() == 'image')) Written in a rush, as I'm a tiny bit busy, but think that's as simple as it can get - mind you I do prefer your version as it's more readable. I would suggest though sticking to practiced naming conventions which encourage using "media_list" instead of "medialist". All the best! > You can unnest the ifs here: > > if photo and photo.get_mime_type().startswith("image"): > medialist.append( > (photo.get_description(), photo) > ) > > thanks to Python's short-circuiting. I put the tuple to be appended > on a separate line for (my) readability, but this is really a matter > of taste. I don't expect all would agree with that choice. > >> if not medialist: >> return >> medialist.sort() >> Thank you for the help, and your quick response.... >> >> -- >> Sincerely yours, >> Rob G. Healey >> "Always surround yourself with people that inspire you to >> greatness!" >> > > |
From: Jon C. <jo...@go...> - 2011-08-31 15:44:32
|
On 31/08/11 16:37, Jon Clements wrote: > On 31/08/11 14:08, Gerald Britton wrote: >> Maybe a little: >> >> On Tue, Aug 30, 2011 at 6:23 PM, Rob Healey<rob...@gm...> >> wrote: >>> Greetings: >>> >>> Could anyone help me with a code simplification for this section of >>> code? >>> >>> medialist = [] >>> for phandle in self.photo_keys: >>> photo = db.get_object_from_handle(phandle) >>> if photo: >>> mime_type = photo.get_mime_type() >>> if mime_type.startswith("image"): >>> photo_title = photo.get_description() >>> medialist.append( (photo_title, photo) ) > > Untested (and not sure if it's "simpler" - bear in mind you have to > come back to it later, or someone else does...!): > > media_list = sorted( (p.get_description(), p) for p in > (db.get_object_from_handle(photo) for photo in self.photo_keys if > photo and photo.get_mime_type() == 'image')) That should obviously be photo.get_mime_type().startswith(...) as Gerald put... > > Written in a rush, as I'm a tiny bit busy, but think that's as simple > as it can get - mind you I do prefer your version as it's more readable. > > I would suggest though sticking to practiced naming conventions which > encourage using "media_list" instead of "medialist". > > All the best! > > >> You can unnest the ifs here: >> >> if photo and photo.get_mime_type().startswith("image"): >> medialist.append( >> (photo.get_description(), photo) >> ) >> >> thanks to Python's short-circuiting. I put the tuple to be appended >> on a separate line for (my) readability, but this is really a matter >> of taste. I don't expect all would agree with that choice. >> >>> if not medialist: >>> return >>> medialist.sort() >>> Thank you for the help, and your quick response.... >>> >>> -- >>> Sincerely yours, >>> Rob G. Healey >>> "Always surround yourself with people that inspire you to >>> greatness!" >>> >> >> > |