Skip to content

Instantly share code, notes, and snippets.

@wil wil/find_forelse.py
Created Oct 24, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.