How to use the astunparse.Unparser function in astunparse

To help you get started, we’ve selected a few astunparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github psychopy / psychopy / psychopy / experiment / py2js.py View on Github external
namesJS['rand'] = 'Math.random'
namesJS['random'] = 'Math.random'


class TupleTransformer(ast.NodeTransformer):
    """ An ast subclass that walks the abstract syntax tree and
    allows modification of nodes.

    This class transforms a tuple to a list.

    :returns node
    """
    def visit_Tuple(self, node):
        return ast.List(node.elts, node.ctx)

class Unparser(astunparse.Unparser):
    """astunparser had buried the future_imports option underneath its init()
    so we need to override that method and change it."""

    def __init__(self, tree, file):
        """Unparser(tree, file=sys.stdout) -> None.
         Print the source for tree to file."""
        self.f = file
        self.future_imports = ['unicode_literals']
        self._indent = 0
        self.dispatch(tree)
        self.f.flush()


def unparse(tree):
    v = StringIO()
    Unparser(tree, file=v)
github luci / recipes-py / recipe_engine / internal / commands / doc / cmd.py View on Github external
def _unparse(node):
  """Prints Python code which could parse to `node`.

  Args:
    * node (ast.AST) - The AST to produce Python code for.

  Returns a str with the formatted code.
  """
  assert isinstance(node, ast.AST), type(node)
  buf = StringIO()
  astunparse.Unparser(node, buf)
  return buf.getvalue()
github liuzhenhai / cyjit / control_flow.py View on Github external
print env.cf.path
    
    TypeInfer(env, args, func.func_globals).run()
    print env.cf.blocks
    InsertCoerceNode(env).run()
    #CoerceReturn(env).run()
    print ast.dump(node)
    RewriteName(env).run()
    RewriteSubscript(env).run()
    InsertArrayInfo(env).run()
    InsertDefination(env).run()

    

    buf=StringIO()
    Unparser(node,buf)
    print buf.getvalue()      
    #print get_return_type(env.return_node_infos)    

astunparse

An AST unparser for Python

BSD-2-Clause
Latest version published 5 years ago

Package Health Score

67 / 100
Full package analysis