Skip to content

Commit

Permalink
style: use walrus for regexing
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Aug 29, 2023
1 parent 6afcdc3 commit f0c18f6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
7 changes: 2 additions & 5 deletions ci/parse_relnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def parse_md(lines):
buffer = TextChunkBuffer()

for line in lines:
header_match = re.search(r"^(#+) (.+)$", line)
is_header = bool(header_match)
if is_header:
if header_match := re.search(r"^(#+) (.+)$", line):
yield from buffer.flush()
hashes, text = header_match.groups()
yield (f"h{len(hashes)}", text)
Expand Down Expand Up @@ -80,8 +78,7 @@ def sections(parsed_data):

def refind(regex, text):
"""Find a regex in some text, and return the matched text, or None."""
m = re.search(regex, text)
if m:
if m := re.search(regex, text):
return m.group()
else:
return None
Expand Down
9 changes: 3 additions & 6 deletions coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ def match(self, fpath: str) -> bool:

def sep(s: str) -> str:
"""Find the path separator used in this string, or os.sep if none."""
sep_match = re.search(r"[\\/]", s)
if sep_match:
if sep_match := re.search(r"[\\/]", s):
the_sep = sep_match[0]
else:
the_sep = os.sep
Expand Down Expand Up @@ -337,8 +336,7 @@ def _glob_to_regex(pattern: str) -> str:
pos = 0
while pos < len(pattern):
for rx, sub in G2RX_TOKENS: # pragma: always breaks
m = rx.match(pattern, pos=pos)
if m:
if m := rx.match(pattern, pos=pos):
if sub is None:
raise ConfigError(f"File pattern can't include {m[0]!r}")
path_rx.append(m.expand(sub))
Expand Down Expand Up @@ -469,8 +467,7 @@ def map(self, path: str, exists:Callable[[str], bool] = source_exists) -> str:
self.pprinted = True

for original_pattern, regex, result in self.aliases:
m = regex.match(path)
if m:
if m := regex.match(path):
new = path.replace(m[0], result)
new = new.replace(sep(path), sep(result))
if not self.relative:
Expand Down
3 changes: 1 addition & 2 deletions lab/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def one_file(self, options, filename):
# `filename` can have a line number suffix. In that case, extract those
# lines, dedent them, and use that. This is for trying test cases
# embedded in the test files.
match = re.search(r"^(.*):(\d+)-(\d+)$", filename)
if match:
if match := re.search(r"^(.*):(\d+)-(\d+)$", filename):
filename, start, end = match.groups()
start, end = int(start), int(end)
else:
Expand Down

0 comments on commit f0c18f6

Please sign in to comment.