remove_double_thumbnails() iterates over self.poselib.pose_thumbnails using an inner generator function, which gets iterated over, and then it calls self.remove_thumbnail() which iterates over the same collection again, only to get the index number of the thumbnail to remove. This is quadratic in the number of thumbnails, and could be much easier. Also, it is kind of dangerous to iterate over a collection and delete things from it at the same time.
All of this code:
def get_unused_thumbnails():
for thumbnail in self.poselib.pose_thumbnails:
if not get_pose_from_thumbnail(thumbnail):
yield thumbnail
unused_thumbnails = get_unused_thumbnails()
for thumbnail in unused_thumbnails:
self.remove_thumbnail(thumbnail)
can be replaced by:
thumbs = self.poselib.pose_thumbnails
count = len(thumbs)
for i, thumbnail in enumerate(reversed(thumbs)):
if not get_pose_from_thumbnail(thumbnail):
thumbs.remove(count - i - 1)
This still removes things from the iterated-over collection, but doesn't use a generator and double iterations any more, and doesn't call into self.remove_thumbnail() to do even more iterations. It also uses reversed() to iterate over the thumbnails back to front, keeping the indices to remove valid.
https://github.com/jasperges/pose_thumbnails/blob/e80726b23d7ee9d5245ac7914c3c0d5fadf90833/pose_thumbnails.py#L785
remove_double_thumbnails()
iterates overself.poselib.pose_thumbnails
using an inner generator function, which gets iterated over, and then it callsself.remove_thumbnail()
which iterates over the same collection again, only to get the index number of the thumbnail to remove. This is quadratic in the number of thumbnails, and could be much easier. Also, it is kind of dangerous to iterate over a collection and delete things from it at the same time.All of this code:
can be replaced by:
This still removes things from the iterated-over collection, but doesn't use a generator and double iterations any more, and doesn't call into
self.remove_thumbnail()
to do even more iterations. It also usesreversed()
to iterate over the thumbnails back to front, keeping the indices to remove valid.