Dedent these loops via comprehensions of questionable clarity.

This commit is contained in:
voussoir 2020-09-27 13:50:35 -07:00
parent fa512eff28
commit 06340d03da

View file

@ -40,16 +40,21 @@ def album_as_directory_map(album, once_each=True, recursive=True):
else: else:
directories[album] = [root_folder] directories[album] = [root_folder]
if recursive: if not recursive:
for child_album in album.get_children(): return directories
child_directories = album_as_directory_map(child_album, once_each=once_each, recursive=True)
for (child_album, child_directory) in child_directories.items(): descendants = (
if once_each: pair
child_directory = os.path.join(root_folder, child_directory) for child in album.get_children()
directories[child_album] = child_directory for pair in album_as_directory_map(child, once_each=once_each, recursive=True).items()
else: )
child_directory = [os.path.join(root_folder, d) for d in child_directory] for (child_album, child_directory) in descendants:
directories.setdefault(child_album, []).extend(child_directory) if once_each:
child_directory = os.path.join(root_folder, child_directory)
directories[child_album] = child_directory
else:
child_directory = [os.path.join(root_folder, d) for d in child_directory]
directories.setdefault(child_album, []).extend(child_directory)
return directories return directories
@ -63,17 +68,21 @@ def album_photos_as_filename_map(album, once_each=True, recursive=True):
If a photo appears in multiple albums, only one instance is used. If a photo appears in multiple albums, only one instance is used.
''' '''
arcnames = {} arcnames = {}
directories = album_as_directory_map(album, once_each=once_each, recursive=recursive) directories = album_as_directory_map(album, once_each=once_each, recursive=recursive)
for (album, directory) in directories.items(): photos = (
photos = album.get_photos() (photo, directory)
for photo in photos: for (album, directory) in directories.items()
photo_name = f'{photo.id} - {photo.basename}' for photo in album.get_photos()
if once_each: )
arcname = os.path.join(directory, photo_name) for (photo, directory) in photos:
arcnames[photo] = arcname photo_name = f'{photo.id} - {photo.basename}'
else: if once_each:
arcname = [os.path.join(d, photo_name) for d in directory] arcname = os.path.join(directory, photo_name)
arcnames.setdefault(photo, []).extend(arcname) arcnames[photo] = arcname
else:
arcname = [os.path.join(d, photo_name) for d in directory]
arcnames.setdefault(photo, []).extend(arcname)
return arcnames return arcnames