Last active
November 17, 2022 20:05
-
-
Save JRiggles/d621cc2bee3c2ddb564c03a23c996d9b to your computer and use it in GitHub Desktop.
RegEx for tracking down Python 'if' and 'while' statements that might be refactorable to use the 'walrus' assignment operator ':='
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex = r'^\h*(\w+) = .*\n+\h*(if|while) \1:' | |
# example | |
from os import environ | |
# before := | |
env = environ.get('PATH', None) | |
if env: | |
print(env) | |
# becomes... | |
if env := environ.get('PATH', None) | |
print(env) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment