How to use the gast.Subscript 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 serge-sans-paille / beniget / beniget / beniget.py View on Github external
def visit_Destructured(self, node):
        dnode = self.chains.setdefault(node, Def(node))
        tmp_store = ast.Store()
        for elt in node.elts:
            if isinstance(elt, ast.Name):
                tmp_store, elt.ctx = elt.ctx, tmp_store
                self.visit(elt)
                tmp_store, elt.ctx = elt.ctx, tmp_store
            elif isinstance(elt, ast.Subscript):
                self.visit(elt)
            elif isinstance(elt, (ast.List, ast.Tuple)):
                self.visit_Destructured(elt)
        return dnode
github google / tangent / tangent / anf.py View on Github external
def visit_Assign(self, node):
    self.src = quoting.unquote(node)
    self.mark(node)
    self.trivializing = True
    self.namer.target = node.targets[0]
    if isinstance(node.targets[0], (gast.Subscript, gast.Attribute)):
      node.value = self.trivialize(node.value)
      node.targets[0] = self.visit(node.targets[0])
    elif isinstance(node.targets[0], gast.Tuple):
      node.value = self.visit(node.value)
      name = self.namer.name(node.targets[0])
      target = gast.Name(id=name, ctx=gast.Store(), annotation=None)
      for i, elt in enumerate(node.targets[0].elts):
        stmt = gast.Assign(
            targets=[elt],
            value=gast.Subscript(
                value=gast.Name(id=name, ctx=gast.Load(),
                                annotation=None),
                slice=gast.Index(value=gast.Num(n=i)),
                ctx=gast.Load()))
        self.mark(stmt)
        self.append(stmt)
github serge-sans-paille / pythran / pythran / analyses / aliases.py View on Github external
'''
        if isinstance(node.slice, ast.Index):
            aliases = set()
            self.visit(node.slice)
            value_aliases = self.visit(node.value)
            for alias in value_aliases:
                if isinstance(alias, ContainerOf):
                    if isinstance(node.slice.value, ast.Slice):
                        continue
                    if isnum(node.slice.value):
                        if node.slice.value.value != alias.index:
                            continue
                    # FIXME: what if the index is a slice variable...
                    aliases.add(alias.containee)
                elif isinstance(getattr(alias, 'ctx', None), ast.Param):
                    aliases.add(ast.Subscript(alias, node.slice, node.ctx))
            if not aliases:
                aliases = None
        else:
            # could be enhanced through better handling of containers
            aliases = None
            self.generic_visit(node)
        return self.add(node, aliases)
github tensorflow / tensorflow / tensorflow / contrib / autograph / converters / slices.py View on Github external
def _process_single_assignment(self, target, value):
    if not isinstance(target, gast.Subscript):
      return None

    template = """
      target = ag__.set_item(target, key, item)
    """
    return templates.replace(
        template, target=target.value, key=target.slice, item=value)
github pfnet-research / chainer-compiler / chainer_compiler / elichika / typing / type_checker.py View on Github external
# Compare(expr left, cmpop* ops, expr* comparators)
            self.nodetype[node] = TyBool()
        elif isinstance(node, gast.Call):
            self.nodetype[node] = self.infer_Call(node)
        elif isinstance(node, gast.Num):
            # Num(object n)
            self.nodetype[node] = type_of_value(node.n)
        elif isinstance(node, gast.Str):
            # Str(string s)
            self.nodetype[node] = TyString(value=node.s)
        elif isinstance(node, gast.NameConstant):
            # NameConstant(singleton value)
            self.nodetype[node] = type_of_value(node.value)
        elif isinstance(node, gast.Attribute):
            self.nodetype[node] = self.infer_Attribute(node)
        elif isinstance(node, gast.Subscript):
            self.nodetype[node] = self.infer_Subscript(node)
        elif isinstance(node, gast.Name):
            self.nodetype[node] = self.infer_Name(node)
        elif isinstance(node, gast.List):
            # List(expr* elts, expr_context ctx)
            elts_ty = [self.infer_expr(e) for e in node.elts]
            self.nodetype[node] = TyList(elts_ty)
        elif isinstance(node, gast.Tuple):
            # Tuple(expr* elts, expr_context ctx)
            elts_ty = [self.infer_expr(e) for e in node.elts]
            self.nodetype[node] = TyTuple(elts_ty)

        assert node in self.nodetype.keys() and \
                self.nodetype[node] is not None, type(node).__name__
        self.stack.pop()
        if self.is_debug:
github serge-sans-paille / pythran / pythran / transformations / normalize_tuples.py View on Github external
                lambda x, y: ast.Subscript(
                    x,
                    ast.Index(ast.Constant(y, None)),
                    ast.Load()),
                self.renamings[node.id],
github serge-sans-paille / pythran / pythran / analyses / argument_read_once.py View on Github external
def argument_index(self, node):
        while isinstance(node, ast.Subscript):
            node = node.value
        if node in self.aliases:
            for n_alias in self.aliases[node]:
                try:
                    return self.current_function.func.args.args.index(n_alias)
                except ValueError:
                    pass
        return -1
github pfnet-research / chainer-compiler / chainer_compiler / elichika / typing / type_checker.py View on Github external
# Compare(expr left, cmpop* ops, expr* comparators)
            self.nodetype[node] = TyBool()
        elif isinstance(node, gast.Call):
            self.nodetype[node] = self.infer_Call(node)
        elif isinstance(node, gast.Num):
            # Num(object n)
            self.nodetype[node] = type_of_value(node.n)
        elif isinstance(node, gast.Str):
            # Str(string s)
            self.nodetype[node] = TyString(value=node.s)
        elif isinstance(node, gast.NameConstant):
            # NameConstant(singleton value)
            self.nodetype[node] = type_of_value(node.value)
        elif isinstance(node, gast.Attribute):
            self.nodetype[node] = self.infer_Attribute(node)
        elif isinstance(node, gast.Subscript):
            self.nodetype[node] = self.infer_Subscript(node)
        elif isinstance(node, gast.Name):
            self.nodetype[node] = self.infer_Name(node)
        elif isinstance(node, gast.List):
            # List(expr* elts, expr_context ctx)
            elts_ty = [self.infer_expr(e) for e in node.elts]
            self.nodetype[node] = TyList(elts_ty)
        elif isinstance(node, gast.Tuple):
            # Tuple(expr* elts, expr_context ctx)
            elts_ty = [self.infer_expr(e) for e in node.elts]
            self.nodetype[node] = TyTuple(elts_ty)

        assert node in self.nodetype.keys() and \
                self.nodetype[node] is not None, type(node).__name__
        self.stack.pop()
        return self.nodetype[node]
github serge-sans-paille / pythran / pythran / transformations / normalize_method_calls.py View on Github external
# A getattr !
        else:
            self.update = True
            call = ast.Call(
                ast.Attribute(
                    ast.Name('__builtin__', ast.Load(), None, None),
                    'getattr',
                    ast.Load()),
                [node.value, ast.Constant(node.attr, None)],
                [])
            if isinstance(node.ctx, ast.Store):
                # the only situation where this arises is for real/imag of
                # a ndarray. As a call is not valid for a store, add a slice
                # to ends up with a valid lhs
                assert node.attr in ('real', 'imag'), "only store to imag/real"
                return ast.Subscript(call,
                                     ast.Slice(None, None, None),
                                     node.ctx)
            else:
                return call

gast

Python AST that abstracts the underlying Python version

BSD-3-Clause
Latest version published 6 months ago

Package Health Score

78 / 100
Full package analysis