From 76e7faf1aa0714691faf62a38dfdce2e5394c656 Mon Sep 17 00:00:00 2001 From: Chris Hill Date: Sun, 7 Feb 2016 09:27:28 +0000 Subject: [PATCH] Remove _try_int function - spurious --- openlp/plugins/songs/lib/mediaitem.py | 15 ++------- .../openlp_plugins/songs/test_mediaitem.py | 32 ++----------------- 2 files changed, 5 insertions(+), 42 deletions(-) diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 187dbfa05..87a3d81d1 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -328,6 +328,7 @@ class SongMediaItem(MediaManagerItem): """ log.debug('display results Topic') self.list_view.clear() + search_results = sorted(search_results, key=lambda topic: self._natural_sort_key(topic.name)) for topic in search_results: songs = sorted(topic.songs, key=lambda song: song.sort_key) for song in songs: @@ -693,23 +694,13 @@ class SongMediaItem(MediaManagerItem): # List must be empty at the end return not author_list - def _try_int(self, s): - """ - Convert string s to an integer if possible. Fail silently and return - the string as-is if it isn't an integer. - :param s: The string to try to convert. - """ - try: - return int(s) - except (TypeError, ValueError): - return s - def _natural_sort_key(self, s): """ Return a tuple by which s is sorted. :param s: A string value from the list we want to sort. """ - return list(map(self._try_int, re.findall(r'(\d+|\D+)', s))) + return [int(text) if text.isdecimal() else text + for text in re.split('(\d+)', s)] def search(self, string, show_error): """ diff --git a/tests/functional/openlp_plugins/songs/test_mediaitem.py b/tests/functional/openlp_plugins/songs/test_mediaitem.py index e9dd3e018..2b586a96a 100644 --- a/tests/functional/openlp_plugins/songs/test_mediaitem.py +++ b/tests/functional/openlp_plugins/songs/test_mediaitem.py @@ -416,46 +416,18 @@ class TestMediaItem(TestCase, TestMixin): # THEN: They should not match self.assertFalse(result, "Authors should not match") - def try_int_with_string_integer_test(self): - """ - Test the _try_int function with a string containing an integer - """ - # GIVEN: A string that is an integer - string_integer = '123' - - # WHEN: We "convert" it to an integer - integer_result = self.media_item._try_int(string_integer) - - # THEN: We should get back an integer - self.assertIsInstance(integer_result, int, 'The result should be an integer') - self.assertEqual(integer_result, 123, 'The result should be 123') - - def try_int_with_string_noninteger_test(self): - """ - Test the _try_int function with a string not containing an integer - """ - # GIVEN: A string that is not an integer - string_noninteger = 'abc' - - # WHEN: We "convert" it to an integer - noninteger_result = self.media_item._try_int(string_noninteger) - - # THEN: We should get back the original string - self.assertIsInstance(noninteger_result, type(string_noninteger), 'The result type should be the same') - self.assertEqual(noninteger_result, string_noninteger, 'The result value should be the same') - def natural_sort_key_test(self): """ Test the _natural_sort_key function """ # GIVEN: A string to be converted into a sort key - string_sort_key = 'A1B12C123' + string_sort_key = 'A1B12C' # WHEN: We attempt to create a sort key sort_key_result = self.media_item._natural_sort_key(string_sort_key) # THEN: We should get back a tuple split on integers - self.assertEqual(sort_key_result, ['A', 1, 'B', 12, 'C', 123]) + self.assertEqual(sort_key_result, ['A', 1, 'B', 12, 'C']) def build_remote_search_test(self): """