python - Efficiently select the best string from keys in a dict -


i'm trying read exif data file, title of image. there multiple fields need checked see if title set. right now, i'm doing bunch of if statements, trying 1 @ time until result not none , if is, applying default title.

#set title   m.title = exif.get('xmp.dc.subject')   if m.title none:     m.title = exif.get('xmp.dc.title')   if m.title none:     m.title = original_filename_noextension 

my question is, there easier way this? searching can quite long if there lot of keys need checked.

m.title = exif.get('xmp.dc.subject') or exif.get('xmp.dc.title') or original_filename_noextension 

should work. note 'coalesce' falsey values, not none.

alternatively, can write function pretty easily:

def coalesce(*args):    in args:       if not none:           return 

usage:

m.title = coalesce(exif.get('xmp.dc.subject'), exif.get('xmp.dc.title'), original_filename_noextension) 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -