Use tuples instead of lists, since those aren't to be modified

This commit is contained in:
Laurent Monin
2022-01-25 12:24:58 +01:00
committed by Philipp Wolfer
parent 6f9c70951d
commit 0761263c4d
2 changed files with 13 additions and 9 deletions

View File

@@ -48,6 +48,9 @@ RE_TOC_TABLE_LINE = re.compile(r"""
\s*$""", re.VERBOSE)
PREGAP_LENGTH = 150
class NotSupportedTOCError(Exception):
pass
@@ -75,20 +78,21 @@ def filter_toc_entries(lines):
def calculate_mb_toc_numbers(eac_entries):
"""
Take iterator of toc entries, return list of numbers for musicbrainz disc id
Take iterator of toc entries, return a tuple of numbers for musicbrainz disc id
"""
eac = list(eac_entries)
eac = tuple(eac_entries)
num_tracks = len(eac)
if not num_tracks:
raise NotSupportedTOCError("Empty track list: %s", eac)
tracknums = [int(e['num']) for e in eac]
if list(range(1, num_tracks+1)) != tracknums:
expected_tracknums = tuple(range(1, num_tracks+1))
tracknums = tuple(int(e['num']) for e in eac)
if expected_tracknums != tracknums:
raise NotSupportedTOCError("Non-standard track number sequence: %s", tracknums)
leadout_offset = int(eac[-1]['end_sector']) + 150 + 1
offsets = [(int(x['start_sector']) + 150) for x in eac]
return [1, num_tracks, leadout_offset] + offsets
leadout_offset = int(eac[-1]['end_sector']) + PREGAP_LENGTH + 1
offsets = tuple((int(x['start_sector']) + PREGAP_LENGTH) for x in eac)
return (1, num_tracks, leadout_offset) + offsets
def toc_from_file(path):

View File

@@ -79,7 +79,7 @@ class TestFilterTocEntries(PicardTestCase):
class TestCalculateMbTocNumbers(PicardTestCase):
def test_calculate_mb_toc_numbers(self):
self.assertEqual([1, 3, 60890, 150, 25064, 43611], calculate_mb_toc_numbers(test_entries))
self.assertEqual((1, 3, 60890, 150, 25064, 43611), calculate_mb_toc_numbers(test_entries))
def test_calculate_mb_toc_numbers_invalid_track_numbers(self):
entries = [{'num': '1'}, {'num': '3'}, {'num': '4'}]
@@ -96,7 +96,7 @@ class TestTocFromFile(PicardTestCase):
def _test_toc_from_file(self, logfile):
test_log = get_test_data_path(logfile)
toc = toc_from_file(test_log)
self.assertEqual([1, 8, 149323, 150, 25064, 43611, 60890, 83090, 100000, 115057, 135558], toc)
self.assertEqual((1, 8, 149323, 150, 25064, 43611, 60890, 83090, 100000, 115057, 135558), toc)
def test_toc_from_file_eac(self):
self._test_toc_from_file('eac.log')