From 7db6083922c5c77780b7710a644af2a5b711450d Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Fri, 19 Feb 2021 11:52:02 +0100 Subject: [PATCH] generate_page: raise ValueError if no numeric part in start/end --- ocrd_utils/ocrd_utils/str.py | 6 ++---- tests/test_utils.py | 3 ++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ocrd_utils/ocrd_utils/str.py b/ocrd_utils/ocrd_utils/str.py index f65a7c2781..1135480075 100644 --- a/ocrd_utils/ocrd_utils/str.py +++ b/ocrd_utils/ocrd_utils/str.py @@ -5,7 +5,6 @@ import re import json from .constants import REGEX_FILE_ID -from .logging import getLogger __all__ = [ 'assert_file_grp_cardinality', @@ -184,10 +183,9 @@ def generate_range(start, end): Generate a list of strings by incrementing the number part of ``start`` until including ``end``. """ ret = [] - start_num, end_num = re.search('\d+', start), re.search('\d+', end) + start_num, end_num = re.search(r'\d+', start), re.search(r'\d+', end) if not (start_num and end_num): - getLogger('ocrd_utils.generate_range').error("Unable to generate range %s .. %s, could not detect number part" % (start, end)) - return [start, end] + raise ValueError("Unable to generate range %s .. %s, could not detect number part" % (start, end)) start_num, end_num = start_num.group(0), end_num.group(0) for i in range(int(start_num), int(end_num) + 1): ret.append(start.replace(start_num, str(i).zfill(len(start_num)))) diff --git a/tests/test_utils.py b/tests/test_utils.py index e77a3cfa22..7eedf9a061 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -300,7 +300,8 @@ def test_make_file_id_605(self): def test_generate_range(self): assert generate_range('PHYS_0001', 'PHYS_0005') == ['PHYS_0001', 'PHYS_0002', 'PHYS_0003', 'PHYS_0004', 'PHYS_0005'] - assert generate_range('NONUMBER', 'ALSO_NONUMBER') == ['NONUMBER', 'ALSO_NONUMBER'] + with self.assertRaisesRegex(ValueError, 'Unable to generate range'): + generate_range('NONUMBER', 'ALSO_NONUMBER') if __name__ == '__main__':