Skip to content

Commit

Permalink
generate_page: raise ValueError if no numeric part in start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Feb 19, 2021
1 parent c67a017 commit 7db6083
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
6 changes: 2 additions & 4 deletions ocrd_utils/ocrd_utils/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re
import json
from .constants import REGEX_FILE_ID
from .logging import getLogger

__all__ = [
'assert_file_grp_cardinality',
Expand Down Expand Up @@ -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))))
Expand Down
3 changes: 2 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 7db6083

Please sign in to comment.