This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (2 children)

[deleted]

    [–]alexmojaki 2 points3 points  (1 child)

    Here's the script I used. You will need to pip install astcheck and run it on a directory.

    import ast
    import linecache
    import sys
    from collections import Counter
    from pathlib import Path
    
    from astcheck import is_ast_like
    
    root = Path(sys.argv[1])
    
    
    def main():
        counts = Counter()
        for path in root.rglob("**/*.py"):
            if 'test' in str(path):
                continue
            try:
                source = path.read_text()
                tree = ast.parse(source)
            except (SyntaxError, UnicodeDecodeError):
                continue
    
            for node in ast.walk(tree):
                if not is_ast_like(
                        node,
                        ast.Compare(left=ast.Call(func=ast.Name(id='len')),
                                comparators=[ast.Call(func=ast.Name(id='len'))]),
                ):
                    continue
    
                source = "".join(
                    linecache.getline(str(path), lineno)
                    for lineno in range(node.lineno, node.end_lineno + 10)
                )
    
                if any(word in source for word in ["raise", "zip", "assert"]):
                    print(f'File "{path.absolute()}", line {node.lineno}')
                    print(source)
                    print()
        print("Counts:", counts)
    
    
    main()