1. Victor Stinner
  2. astoptimizer

Commits

Victor Stinner  committed 1d9bb3b

doc

  • Participants
  • Parent commits ba9a225
  • Branches default

Comments (0)

Files changed (2)

File README

View file
  • Ignore whitespace
 AST Optimizer
 +++++++++++++
 
-AST optimizer implemented in Python.
+Experimental AST optimizer for Python.
+
+Options
+=======
 
 By default, the optimizer only enables portable optimizations. If you control
 your environment (platform, Python command line options, environment
  - "platform": optimizations specific to a platform.
 
 
+Example
+=======
+
+Example::
+
+    from astoptimizer import parse_ast, Optimizer
+    tree = parse_ast('print(1+1'))
+    tree = Optimizer(config).visit(tree)
+    code = compile(tree, "<ast>", "exec")
+    exec(code)  # or exec code with Python 2
+
+See also demo.py script.
+
+
 TODO
 ====
 

File demo.py

View file
  • Ignore whitespace
 import dis
 import sys
 from astoptimizer import (
-    CompilerConfig, parse_ast, PyCF_DISABLE_PEEPHOLE, AstOptimizer)
+    Config, parse_ast, PyCF_DISABLE_PEEPHOLE, Optimizer)
 
 def main():
     if len(sys.argv) < 2:
         sys.exit(1)
     code = '\n'.join(sys.argv[1:])
 
-    config = CompilerConfig()
+    config = Config()
     config.constants['sys.platform'] = sys.platform
     config.constants['sys.version'] = sys.version
     config.constants['sys.hexversion'] = sys.hexversion
     print(ast.dump(tree))
     print("")
 
-    tree = ast.parse(code)
-    tree = AstOptimizer(config).visit(tree)
+    tree = Optimizer(config).visit(tree)
     print("Optimised AST:")
     print(ast.dump(tree))
     print("")