Source

astoptimizer / demo.py

#!/usr/bin/env python
import ast
import dis
import sys
from astoptimizer import (
    Config, parse_ast, PyCF_DISABLE_PEEPHOLE, Optimizer)

def main():
    if len(sys.argv) < 2:
        print("usage: %s code ..." % sys.argv[0])
        print("example: %s '1+1'" % sys.argv[0])
        sys.exit(1)
    code = '\n'.join(sys.argv[1:])

    config = Config()
    config.constants['sys.platform'] = sys.platform
    config.constants['sys.version'] = sys.version
    config.constants['sys.hexversion'] = sys.hexversion

    tree = parse_ast(code)
    print("Original AST:")
    print(ast.dump(tree))
    print("")

    tree = Optimizer(config).visit(tree)
    print("Optimised AST:")
    print(ast.dump(tree))
    print("")

    code = compile(tree, "<ast>", "exec", flags=PyCF_DISABLE_PEEPHOLE)
    print("Bytecode:")
    dis.dis(code)
    print("")

    print("Exec:")
    exec(code)

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.