Skip to content

Instantly share code, notes, and snippets.

@JRiggles
Last active November 17, 2022 20:05
Show Gist options
  • Save JRiggles/d621cc2bee3c2ddb564c03a23c996d9b to your computer and use it in GitHub Desktop.
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 ':='
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