Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt str conversion when matching in column from regex, capture group optional #76

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions annotator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,10 @@ def substituteColumnValues(referenceList, mod):
return referenceList


def colFromRegex(referenceList, regex):
def colFromRegex(referenceList, regex, silent=False):
""" Return a list created by mapping a regular expression to another list.
The regular expression must contain at least one capture group.
If a capture group is included, contents matching the capture group will
be used to derive a new value. Otherwise the entire expression is used.

Parameters
----------
Expand All @@ -364,12 +365,19 @@ def colFromRegex(referenceList, regex):
The list resulting from mapping `regex` to `referenceList`.
"""
p = re.compile(regex)
if not p.groups:
raise RuntimeError("`regex` must have at least one capture group.")
newCol = []
if not all([isinstance(i, str) for i in referenceList]):
print("Warning: Attempting to convert values to str.")
print("May cause unexpected results.")
for s in referenceList:
m = p.search(s) if isinstance(s, str) else None
if not m and isinstance(s, str):
print("{} does not match regex.".format(s))
newCol.append(m.group(1)) if m else newCol.append(None)
m = p.search(str(s))
if not m:
if not silent:
print("{} does not match regex.".format(str(s)))
newCol.append(None)
else:
if p.groups:
newCol.append(m.group(1))
else:
newCol.append(m.group())
return newCol
9 changes: 9 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,12 @@ def test_colFromRegex(self, values):
# gives a list of the first vowel of each word
result = annotator.utils.colFromRegex(values, r"([aeiou])")
assert result == ['u', 'u', 'e', 'e', 'o']

def test_colFromRegex_non_str(self):
values = [[1,2,3], {4,5,6}]
result = annotator.utils.colFromRegex(values, r"(\d)")
assert result == ["1", "4"]

def test_colFromRegex_no_capture_group(self, values):
result = annotator.utils.colFromRegex(values, r"\we\w")
assert result == [None, None, 'red', 'ree', None]