How to use the gast.Expr function in gast

To help you get started, we’ve selected a few gast 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 google / tangent / tests / test_template.py View on Github external
def test_statement_replace():
  def f(body):
    body

  body = [gast.Expr(value=gast.Name(id=var, ctx=gast.Load(), annotation=None))
          for var in 'xy']
  new_body = template.replace(f, body=body)
  assert len(new_body) == 2
  assert isinstance(new_body[0], gast.Expr)
  compile_.compile_function(_wrap(new_body))
github google / tangent / tests / test_template.py View on Github external
def test_statement_replace():
  def f(body):
    body

  body = [gast.Expr(value=gast.Name(id=var, ctx=gast.Load(), annotation=None))
          for var in 'xy']
  new_body = template.replace(f, body=body)
  assert len(new_body) == 2
  assert isinstance(new_body[0], gast.Expr)
  compile_.compile_function(_wrap(new_body))
github serge-sans-paille / pythran / pythran / transformations / remove_comprehension.py View on Github external
def visit_AnyComp(self, node, comp_type, *path):
        self.update = True
        node.elt = self.visit(node.elt)
        name = "{0}_comprehension{1}".format(comp_type, self.count)
        self.count += 1
        args = self.gather(ImportedIds, node)
        self.count_iter = 0

        starget = "__target"
        body = reduce(self.nest_reducer,
                      reversed(node.generators),
                      ast.Expr(
                          ast.Call(
                              reduce(lambda x, y: ast.Attribute(x, y,
                                                                ast.Load()),
                                     path[1:],
                                     ast.Name(path[0], ast.Load(), None, None)),
                              [ast.Name(starget, ast.Load(), None, None), node.elt],
                              [],
                              )
                          )
                      )
        # add extra metadata to this node
        metadata.add(body, metadata.Comprehension(starget))
        init = ast.Assign(
            [ast.Name(starget, ast.Store(), None, None)],
            ast.Call(
                ast.Attribute(
github serge-sans-paille / pythran / pythran / optimizations / dead_code_elimination.py View on Github external
have_body = any(not isinstance(x, ast.Pass) for x in node.body)
        have_else = any(not isinstance(x, ast.Pass) for x in node.orelse)
        # If the "body" is empty but "else content" is useful, switch branches
        # and remove else content
        if not have_body and have_else:
            test = ast.UnaryOp(op=ast.Not(), operand=node.test)
            self.update = True
            return ast.If(test=test, body=node.orelse, orelse=list())
        # if neither "if" and "else" are useful, keep test if it is not pure
        elif not have_body:
            self.update = True
            if node.test in self.pure_expressions:
                return ast.Pass()
            else:
                node = ast.Expr(value=node.test)
                self.generic_visit(node)
        return node
github salesforce / matchbox / matchbox / macro.py View on Github external
from collections import defaultdict

import astor
import gast

from .recompile import compile_function, code_to_ast

def pushmask(mask_expr):
    return gast.Expr(gast.Call(
        gast.Attribute(gast.Name('matchbox', gast.Load(), None),
                       gast.Name('push_execution_mask', gast.Load(), None),
                       gast.Load()),
        [mask_expr], []))

popmask = gast.Expr(gast.Call(
    gast.Attribute(gast.Name('matchbox', gast.Load(), None),
                   gast.Name('pop_execution_mask', gast.Load(), None),
                   gast.Load()),
    [], []))

def any_active(mask_expr):
    return gast.Call(gast.Attribute( # TODO any over dim 0
        mask_expr, gast.Name('any', gast.Load(), None), gast.Load()), [], [])

class FuseAttributes(gast.NodeTransformer):
    '''Transform foo.bar to foo_DOT_bar'''
    def visit_Attribute(self, node):
        self.generic_visit(node)
        if not isinstance(node.value, gast.Name):
            return node
        attrname = node.attr if isinstance(node.attr, str) else node.attr.id
github serge-sans-paille / pythran / pythran / transformations / remove_comprehension.py View on Github external
def visit_GeneratorExp(self, node):
        self.update = True
        node.elt = self.visit(node.elt)
        name = "generator_expression{0}".format(self.count)
        self.count += 1
        args = self.gather(ImportedIds, node)
        self.count_iter = 0

        body = reduce(self.nest_reducer,
                      reversed(node.generators),
                      ast.Expr(ast.Yield(node.elt))
                      )

        sargs = [ast.Name(arg, ast.Param(), None, None) for arg in args]
        fd = ast.FunctionDef(name,
                             ast.arguments(sargs, [], None, [], [], None, []),
                             [body], [], None, None)
        metadata.add(fd, metadata.Local())
        self.ctx.module.body.append(fd)
        return ast.Call(
            ast.Name(name, ast.Load(), None, None),
            [ast.Name(arg.id, ast.Load(), None, None) for arg in sargs],
            [],
            )  # no sharing !
github google / tangent / tangent / quoting.py View on Github external
This function returns a tree without enclosing `Module` or `Expr` nodes.

  Args:
    src_string: The source code to parse.
    return_expr: Whether or not to return a containing expression. This can be
        set to `True` if the result is to be part of a series of statements.

  Returns:
    An AST of the given source code.

  """
  node = parse_string(src_string)
  body = node.body
  if len(body) == 1:
    if isinstance(body[0], gast.Expr) and not return_expr:
      out = body[0].value
    else:
      out = body[0]
  else:
    out = node
  return out
github serge-sans-paille / pythran / pythran / optimizations / dead_code_elimination.py View on Github external
def visit_Assign(self, node):
        targets = [target for target in node.targets
                   if self.used_target(target)]
        if len(targets) == len(node.targets):
            return node
        node.targets = targets
        self.update = True
        if targets:
            return node
        if node.value in self.pure_expressions:
            return ast.Pass()
        else:
            return ast.Expr(value=node.value)

gast

Python AST that abstracts the underlying Python version

BSD-3-Clause
Latest version published 30 days ago

Package Health Score

82 / 100
Full package analysis

Similar packages