Created
October 24, 2011 11:49
-
-
Save wil/1308847 to your computer and use it in GitHub Desktop.
Look for the for/else construct in python code
This file contains 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
#!/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