Skip to content

Instantly share code, notes, and snippets.

@wil
Created October 24, 2011 11:49
Show Gist options
  • Save wil/1308847 to your computer and use it in GitHub Desktop.
Save wil/1308847 to your computer and use it in GitHub Desktop.
Look for the for/else construct in python code
#!/usr/bin/env python
# Parses a .py file and look for for-else constructs
# Usage: find_forelse.py some_python_file.py
import sys
import ast
def check_file(filename):
with open(filename, "r") as f:
module = ast.parse(f.read(), filename, 'exec')
for node in ast.walk(module):
if isinstance(node, ast.For) and node.orelse:
print "found for-else statement in %s:%d" % (filename, node.lineno)
if __name__ == '__main__':
check_file(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment