Extracted lambdas from sorts in song plugin - improve performance

This commit is contained in:
Chris Hill 2016-04-19 20:00:45 +01:00
parent a38daf9779
commit d76965f8fd
4 changed files with 30 additions and 18 deletions

View File

@ -112,7 +112,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties):
Generically load a set of objects into a cache and a combobox.
"""
objects = self.manager.get_all_objects(cls)
objects.sort(key=lambda object: get_natural_key(object.name))
get_key = lambda object: get_natural_key(object.name)
objects.sort(key=get_key)
combo.clear()
combo.addItem('')
for obj in objects:
@ -346,7 +347,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties):
Load the authors from the database into the combobox.
"""
authors = self.manager.get_all_objects(Author)
authors.sort(key=lambda author: get_natural_key(author.display_name))
get_author_key = lambda author: get_natural_key(author.display_name)
authors.sort(key=get_author_key)
self.authors_combo_box.clear()
self.authors_combo_box.addItem('')
self.authors = []
@ -384,7 +386,8 @@ class EditSongForm(QtWidgets.QDialog, Ui_EditSongDialog, RegistryProperties):
self.theme_combo_box.clear()
self.theme_combo_box.addItem('')
self.themes = theme_list
self.themes.sort(key=lambda theme: get_natural_key(theme))
get_theme_key = lambda theme: get_natural_key(theme)
self.themes.sort(key=get_theme_key)
self.theme_combo_box.addItems(theme_list)
set_case_insensitive_completer(self.themes, self.theme_combo_box)

View File

@ -213,7 +213,8 @@ class SongExportForm(OpenLPWizard):
# Load the list of songs.
self.application.set_busy_cursor()
songs = self.plugin.manager.get_all_objects(Song)
songs.sort(key=lambda song: song.sort_key)
get_song_key = lambda song: song.sort_key
songs.sort(key=get_song_key)
for song in songs:
# No need to export temporary songs.
if song.temporary:

View File

@ -123,7 +123,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP
"""
self.authors_list_widget.clear()
authors = self.manager.get_all_objects(Author)
authors.sort(key=lambda author: get_natural_key(author.display_name))
get_author_key = lambda author: get_natural_key(author.display_name)
authors.sort(key=get_author_key)
for author in authors:
if author.display_name:
author_name = QtWidgets.QListWidgetItem(author.display_name)
@ -138,7 +139,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP
"""
self.topics_list_widget.clear()
topics = self.manager.get_all_objects(Topic)
topics.sort(key=lambda topic: get_natural_key(topic.name))
get_topic_key = lambda topic: get_natural_key(topic.name)
topics.sort(key=get_topic_key)
for topic in topics:
topic_name = QtWidgets.QListWidgetItem(topic.name)
topic_name.setData(QtCore.Qt.UserRole, topic.id)
@ -150,7 +152,8 @@ class SongMaintenanceForm(QtWidgets.QDialog, Ui_SongMaintenanceDialog, RegistryP
"""
self.song_books_list_widget.clear()
books = self.manager.get_all_objects(Book)
books.sort(key=lambda book: get_natural_key(book.name))
get_book_key = lambda book: get_natural_key(book.name)
books.sort(key=get_book_key)
for book in books:
book_name = QtWidgets.QListWidgetItem('%s (%s)' % (book.name, book.publisher))
book_name.setData(QtCore.Qt.UserRole, book.id)

View File

@ -261,7 +261,8 @@ class SongMediaItem(MediaManagerItem):
log.debug('display results Song')
self.save_auto_select_id()
self.list_view.clear()
search_results.sort(key=lambda song: song.sort_key)
get_song_key = lambda song: song.sort_key
search_results.sort(key=get_song_key)
for song in search_results:
# Do not display temporary songs
if song.temporary:
@ -285,9 +286,11 @@ class SongMediaItem(MediaManagerItem):
"""
log.debug('display results Author')
self.list_view.clear()
search_results.sort(key=lambda author: get_natural_key(author.display_name))
get_author_key = lambda author: get_natural_key(author.display_name)
search_results.sort(key=get_author_key)
for author in search_results:
author.songs.sort(key=lambda song: song.sort_key)
get_song_key = lambda song: song.sort_key
author.songs.sort(key=get_song_key)
for song in author.songs:
# Do not display temporary songs
if song.temporary:
@ -306,8 +309,8 @@ class SongMediaItem(MediaManagerItem):
"""
log.debug('display results Book')
self.list_view.clear()
search_results.sort(key=lambda songbook_entry:
(get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry)))
get_songbook_key = lambda songbook_entry:(get_natural_key(songbook_entry.songbook.name), get_natural_key(songbook_entry.entry))
search_results.sort(key=get_songbook_key)
for songbook_entry in search_results:
# Do not display temporary songs
if songbook_entry.song.temporary:
@ -326,9 +329,11 @@ class SongMediaItem(MediaManagerItem):
"""
log.debug('display results Topic')
self.list_view.clear()
search_results.sort(key=lambda topic: get_natural_key(topic.name))
get_topic_key = lambda topic: get_natural_key(topic.name)
search_results.sort(key=get_topic_key)
for topic in search_results:
topic.songs.sort(key=lambda song: song.sort_key)
get_song_key = lambda song: song.sort_key
topic.songs.sort(key=get_song_key)
for song in topic.songs:
# Do not display temporary songs
if song.temporary:
@ -347,8 +352,8 @@ class SongMediaItem(MediaManagerItem):
"""
log.debug('display results Themes')
self.list_view.clear()
search_results.sort(key=lambda song: (get_natural_key(song.theme_name),
song.sort_key))
get_theme_key = lambda song: (get_natural_key(song.theme_name), song.sort_key)
search_results.sort(key=get_theme_key)
for song in search_results:
# Do not display temporary songs
if song.temporary:
@ -367,8 +372,8 @@ class SongMediaItem(MediaManagerItem):
"""
log.debug('display results CCLI number')
self.list_view.clear()
search_results.sort(key=lambda song: (get_natural_key(song.ccli_number),
song.sort_key))
get_cclinumber_key = lambda song: (get_natural_key(song.ccli_number), song.sort_key)
search_results.sort(key=get_cclinumber_key)
for song in search_results:
# Do not display temporary songs
if song.temporary: