Remove _try_int function - spurious

This commit is contained in:
Chris Hill 2016-02-07 09:27:28 +00:00
parent f23e2dfb46
commit 76e7faf1aa
2 changed files with 5 additions and 42 deletions

View File

@ -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):
"""

View File

@ -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):
"""