Skip to content

Commit 5711d8c

Browse files
author
Theofilos Manitaras
committed
Address PR comments (version 2)
1 parent d9d1e16 commit 5711d8c

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

reframe/utility/sanity.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,11 @@ def _callable_name(fn):
536536
return fn_name
537537

538538

539-
def _extractiter_tag(patt, filename, tag, conv, encoding):
539+
def _extractiter_singletag(patt, filename, tag, conv, encoding):
540540
if isinstance(conv, collections.Iterable):
541-
conv = conv[0]
541+
raise SanityError(
542+
f'multiple conversion functions given for single group: {tag}'
543+
)
542544

543545
for m in finditer(patt, filename, encoding):
544546
try:
@@ -551,7 +553,8 @@ def _extractiter_tag(patt, filename, tag, conv, encoding):
551553
except ValueError:
552554
fn_name = _callable_name(conv)
553555
raise SanityError(
554-
f'could not convert value {val!r} using {fn_name}()')
556+
f'could not convert value {val!r} using {fn_name}()'
557+
)
555558

556559

557560
def _extractiter_multitag(patt, filename, tags, conv, encoding):
@@ -569,16 +572,16 @@ def _extractiter_multitag(patt, filename, tags, conv, encoding):
569572
elif builtins.len(conv) > builtins.len(val):
570573
conv = conv[:builtins.len(val)]
571574

572-
# Here we use the last conversion function for the remaining
573-
# tags which don't have a corresponding one, if length of the
574-
# conversion function iterable is less that the one of tags
575+
# Use the last function in case we have less conversion functions than
576+
# tags
575577
for v, c in itertools.zip_longest(val, conv, fillvalue=conv[-1]):
576578
try:
577579
converted_vals.append(c(v) if callable(c) else v)
578580
except ValueError:
579581
fn_name = _callable_name(conv)
580582
raise SanityError(
581-
f'could not convert value {v!r} using {fn_name}()')
583+
f'could not convert value {v!r} using {fn_name}()'
584+
)
582585

583586
yield tuple(converted_vals)
584587

@@ -595,7 +598,7 @@ def extractiter(patt, filename, tag=0, conv=None, encoding='utf-8'):
595598
if isinstance(tag, collections.Iterable) and not isinstance(tag, str):
596599
yield from _extractiter_multitag(patt, filename, tag, conv, encoding)
597600
else:
598-
yield from _extractiter_tag(patt, filename, tag, conv, encoding)
601+
yield from _extractiter_singletag(patt, filename, tag, conv, encoding)
599602

600603

601604
@deferrable
@@ -618,24 +621,22 @@ def extractall(patt, filename, tag=0, conv=None, encoding='utf-8'):
618621
returns the whole line that was matched.
619622
:arg conv: A callable or iterable of callables taking a single argument
620623
and returning a new value.
621-
If provided, and is not an iterable it will be used to convert
622-
the extracted values for all the capturing groups of ``tag``
623-
returning the converted values.
624-
If an iterable of callables is provided, each one will be used to
625-
convert the corresponding extracted capturing group of `tag`.
626-
If more callables functions than the corresponding capturing groups of
627-
``tag`` are provided, the last conversion function is used for the
628-
remaining capturing groups.
629-
:returns: A list of the extracted values from the matched regex if ``tag``
630-
converted using the ``conv`` callable if ``tag`` is a single value.
631-
In case of multiple capturing groups, a list of tuples where each one
632-
contains the extracted capturing converted using the corresponding
633-
callable of ``conv``.
624+
If not an iterable it will be used to convert the extracted values for
625+
all the capturing groups specified in ``tag``.
626+
Otherwise, each conversion function will be used to convert the value
627+
extracted from the corresponding capturing group in ``tag``.
628+
If more conversion functions are supplied than the corresponding
629+
capturing groups in ``tag``, the last conversion function will be used
630+
for the additional capturing groups.
631+
:returns: A list of tuples of converted values extracted from the
632+
capturing groups specified in ``tag``, if ``tag`` is an iterable.
633+
Otherwise, a list of the converted values extracted from the single
634+
capturing group specified in ``tag``.
634635
:raises reframe.core.exceptions.SanityError: In case of errors.
635636
636637
.. versionchanged:: 3.1
637638
Multiple regex capturing groups are now supporetd via ``tag`` and
638-
multiple callables can be used in ``conv``.
639+
multiple conversion functions can be used in ``conv``.
639640
'''
640641
return list(evaluate(x)
641642
for x in extractiter(patt, filename, tag, conv, encoding))

unittests/test_sanity_functions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -685,20 +685,14 @@ def test_extractall_multiple_tags(self):
685685
assert 2 * expected == v[1]
686686
assert isinstance(v[1], float)
687687

688-
# Check more convert functions than tags
688+
# Check more conversion functions than tags
689689
res = sn.evaluate(sn.extractall(r'Number: (?P<no1>\d+) (?P<no2>\d+)',
690690
self.tempfile, ('no1', 'no2'),
691691
[int, float, float, float]))
692692
for expected, v in enumerate(res, start=1):
693693
assert expected == v[0]
694694
assert 2 * expected == v[1]
695695

696-
# Check multiple convert functions and single tag
697-
res = sn.evaluate(sn.extractall(
698-
r'Number: (?P<no>\d+) \d+', self.tempfile, 'no', [int, float]))
699-
for expected, v in enumerate(res, start=1):
700-
assert expected == v
701-
702696
# Check fewer convert functions than tags
703697
res = sn.evaluate(sn.extractall(r'Number: (?P<no1>\d+) (?P<no2>\d+)',
704698
self.tempfile, ('no1', 'no2'),
@@ -707,6 +701,12 @@ def test_extractall_multiple_tags(self):
707701
assert expected == v[0]
708702
assert 2 * expected == v[1]
709703

704+
# Check multiple conversion functions and a single tag
705+
with pytest.raises(SanityError):
706+
res = sn.evaluate(sn.extractall(
707+
r'Number: (?P<no>\d+) \d+', self.tempfile, 'no', [int, float])
708+
)
709+
710710
def test_extractall_encoding(self):
711711
res = sn.evaluate(
712712
sn.extractall('Odyssey', self.utf16_file, encoding='utf-16')

0 commit comments

Comments
 (0)