style: use walrus for regexing · nedbat/coveragepy@f0c18f6 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -44,9 +44,7 @@ def parse_md(lines):
44 44 buffer = TextChunkBuffer()
45 45
46 46 for line in lines:
47 -header_match = re.search(r"^(#+) (.+)$", line)
48 -is_header = bool(header_match)
49 -if is_header:
47 +if header_match := re.search(r"^(#+) (.+)$", line):
50 48 yield from buffer.flush()
51 49 hashes, text = header_match.groups()
52 50 yield (f"h{len(hashes)}", text)
@@ -80,8 +78,7 @@ def sections(parsed_data):
80 78
81 79 def refind(regex, text):
82 80 """Find a regex in some text, and return the matched text, or None."""
83 -m = re.search(regex, text)
84 -if m:
81 +if m := re.search(regex, text):
85 82 return m.group()
86 83 else:
87 84 return None
Original file line number Diff line number Diff line change
@@ -300,8 +300,7 @@ def match(self, fpath: str) -> bool:
300 300
301 301 def sep(s: str) -> str:
302 302 """Find the path separator used in this string, or os.sep if none."""
303 -sep_match = re.search(r"[\\/]", s)
304 -if sep_match:
303 +if sep_match := re.search(r"[\\/]", s):
305 304 the_sep = sep_match[0]
306 305 else:
307 306 the_sep = os.sep
@@ -337,8 +336,7 @@ def _glob_to_regex(pattern: str) -> str:
337 336 pos = 0
338 337 while pos < len(pattern):
339 338 for rx, sub in G2RX_TOKENS: # pragma: always breaks
340 -m = rx.match(pattern, pos=pos)
341 -if m:
339 +if m := rx.match(pattern, pos=pos):
342 340 if sub is None:
343 341 raise ConfigError(f"File pattern can't include {m[0]!r}")
344 342 path_rx.append(m.expand(sub))
@@ -469,8 +467,7 @@ def map(self, path: str, exists:Callable[[str], bool] = source_exists) -> str:
469 467 self.pprinted = True
470 468
471 469 for original_pattern, regex, result in self.aliases:
472 -m = regex.match(path)
473 -if m:
470 +if m := regex.match(path):
474 471 new = path.replace(m[0], result)
475 472 new = new.replace(sep(path), sep(result))
476 473 if not self.relative:
Original file line number Diff line number Diff line change
@@ -61,8 +61,7 @@ def one_file(self, options, filename):
61 61 # `filename` can have a line number suffix. In that case, extract those
62 62 # lines, dedent them, and use that. This is for trying test cases
63 63 # embedded in the test files.
64 -match = re.search(r"^(.*):(\d+)-(\d+)$", filename)
65 -if match:
64 +if match := re.search(r"^(.*):(\d+)-(\d+)$", filename):
66 65 filename, start, end = match.groups()
67 66 start, end = int(start), int(end)
68 67 else: