4 ms·
> ag '(^|[^a-zA-Z0-9=])([a-zA-Z_][a-zA-Z0-9_]*), ?([a-zA-Z_][a-zA-Z0-9_]*) ?= ?\3, ?\2' Why are you checking `(^|[^a-zA-Z0-9=])` before the first variable? Why
by dotancohen 5y ago
> ag '(^|[^a-zA-Z0-9=])([a-zA-Z_][a-zA-Z0-9_]*), ?([a-zA-Z_][a-zA-Z0-9_]*) ?= ?\3, ?\2'
Why are you checking `(^|[^a-zA-Z0-9=])` before the first variable? Why are you storing it? Wouldn't `^\s*` (beginning of line, then 0 or more whitespace, not stored) be simpler? I ask to learn, not critically.
- eesmith 5y agoThe exclusion test is to reduce false positives like: return open(filename, mode, encoding=encoding, errors=errors) ^^^^^^^^^^^^^^^^^^^^^^^ This case matches because of the under-caret'ed e, encoding=encoding, e You are right that requiring only leading whitespace would be clearer. I didn't think of it. I only wanted to eliminate those false positives I was seeing with my original test, which only had \2, \1. In thinking more about it, your whitespace-only gives a false negative for constructs like: progressbar/widgets.py:306: if not self.fill_left: rpad, lpad = lpad, rpad matplotlib/lib/matplotlib/widgets.py:1093: if vmin>vmax: vmin, vmax = vmax, vmin The reason I stored \1 was simply to get the grouping. I didn't recall the syntax for non-capturing groups, and it wasn't worthwhile to look up.
- dotancohen 5y agoI see, thank you. Targeting the if statements, but not the open() methods, is an interesting corner case that I would not have though about.
- eesmith 5y agoI did mine because I thought some people might write it: (a, b) = (b, a) so the grouping stands out better. Turns out I didn't find any examples of that.