Skip to content

Commit

Permalink
refactor(metacyc): trying new walrus operator in Python 3.8 for regex
Browse files Browse the repository at this point in the history
  • Loading branch information
y1zhou committed Mar 31, 2022
1 parent 6943f09 commit 7066e4d
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions metabolike/parser/metacyc.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,7 @@ def _parse_reaction_layout(s: str) -> Tuple[str, Dict[str, List[str]]]:
r"\(:DIRECTION :(L2R|R2L)\) " # reaction direction
r"\(:RIGHT-PRIMARIES ([^\)]+)\)\)"
)
res = m.fullmatch(s)
if not res:
if not (res := m.fullmatch(s)):
# Sometimes there's no left/right primaries and only a direction
# when the rxn_id is actually a pathway ID
return "", {}
Expand Down Expand Up @@ -690,8 +689,7 @@ def _parse_pathway_links(s: str) -> Tuple[str, List[str], str]:
"""
# Extract the compound ID at the beginning of the string
cpd_id_rgx = re.compile(r"^\(([^\s]+) ")
res = cpd_id_rgx.match(s)
if not res:
if not (res := cpd_id_rgx.match(s)):
logging.warning(f"Pathway links string doesn't have a compound: {s}")
return "", [], ""
cpd = res.group(1)
Expand All @@ -707,15 +705,13 @@ def _parse_pathway_links(s: str) -> Tuple[str, List[str], str]:
while s:
# Directed pathway links wrapped in parentheses
if s[0] == "(":
m = directed_pw_rgx.match(s)
if not m:
if not (m := directed_pw_rgx.match(s)):
raise ValueError(f"Invalid pathway links string: {s}")
pathways.append(m.group(1))
direction = m.group(2)
# Regular pathway links separated by spaces
else:
m = pw_rgx.match(s)
if not m:
if not (m := pw_rgx.match(s)):
raise ValueError(f"Invalid pathway links string: {s}")
pathways.append(m.group(0))
s = s[m.end() :].strip()
Expand Down

0 comments on commit 7066e4d

Please sign in to comment.