How to use the gast.List 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 tensorflow / tensorflow / tensorflow / contrib / autograph / converters / lists.py View on Github external
def visit_Assign(self, node):
    node = self.generic_visit(node)

    # Only convert lists when they are assigned to a variable, e.g.:
    #   l = []
    # TODO(mdan): This rule should be improved.
    if len(node.targets) != 1:
      return node
    if not isinstance(node.value, gast.List):
      return node
    if not isinstance(node.value.ctx, gast.Load):
      return node

    if node.value.elts:
      node.value = self._pre_populated_list(node.value)
    else:
      node.value = self._empty_list(node.value)
    return node
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 serge-sans-paille / pythran / pythran / optimizations / range_loop_unfolding.py View on Github external
def visit_For(self, node):
        if isinstance(node.iter, (ast.List, ast.Tuple)):
            range_params = self.isrange(node.iter.elts)
            if range_params:
                node.iter = ast.Call(ast.Attribute(
                    ast.Name('__builtin__', ast.Load(), None, None),
                    'xrange',
                    node.iter.ctx),
                    [ast.Constant(param, None) for param in range_params],
                    [])
                self.update = True
        return self.generic_visit(node)
github tensorflow / probability / tensorflow_probability / python / experimental / auto_batching / frontend.py View on Github external
def _to_reference_list(self, names):
    return gast.List(
        [self._to_reference(name) for name in names], ctx=gast.Load())
github serge-sans-paille / pythran / pythran / transformations / remove_comprehension.py View on Github external
def visit_DictComp(self, node):
        # this is a quickfix to match visit_AnyComp signature
        # potential source of improvement there!
        node.elt = ast.List(
            [ast.Tuple([node.key, node.value], ast.Load())],
            ast.Load()
            )
        return self.visit_AnyComp(node, "dict", "__dispatch__", "update")
github pfnet-research / chainer-compiler / chainer_compiler / elichika / parser / utils.py View on Github external
if isinstance(node, gast.UnaryOp):
        return "{}{}".format(unaryop_to_str(node.op), expr_to_str(node.operand))
    if isinstance(node, gast.Call):
        return "{}({})".format(expr_to_str(node.func),
                intercalate([expr_to_str(arg) for arg in node.args], ", "))
    if isinstance(node, gast.Num):
        return str(node.n)
    if isinstance(node, gast.Str):
        return "\"...\""  # sometimes it is too long
    if isinstance(node, gast.Attribute):
        return "{}.{}".format(expr_to_str(node.value), node.attr)
    if isinstance(node, gast.Subscript):
        return "{}[{}]".format(expr_to_str(node.value), slice_to_str(node.slice))
    if isinstance(node, gast.Name):
        return node.id
    if isinstance(node, gast.List):
        return "[" + intercalate([expr_to_str(e) for e in node.elts], ", ") + "]"
    if isinstance(node, gast.Tuple):
        return "(" + intercalate([expr_to_str(e) for e in node.elts], ", ") + ")"
    return ""
github pfnet-research / chainer-compiler / chainer_compiler / elichika / typing / utils.py View on Github external
return "{}({})".format(expr_to_str(node.func), intercalate(args, ", "))
    if isinstance(node, gast.Num):
        return str(node.n)
    if isinstance(node, gast.Str):
        if len(node.s) < 20:
            return "\'" + node.s + "\'"
        return "\"...\""  # sometimes it is too long
    if isinstance(node, gast.Attribute):
        return "{}.{}".format(expr_to_str(node.value), node.attr)
    if isinstance(node, gast.Subscript):
        return "{}[{}]".format(expr_to_str(node.value), slice_to_str(node.slice))
    if isinstance(node, gast.NameConstant):
        return str(node.value)
    if isinstance(node, gast.Name):
        return node.id
    if isinstance(node, gast.List):
        return "[" + intercalate([expr_to_str(e) for e in node.elts], ", ") + "]"
    if isinstance(node, gast.Tuple):
        return "(" + intercalate([expr_to_str(e) for e in node.elts], ", ") + ")"
    return ""
github serge-sans-paille / pythran / pythran / optimizations / comprehension_patterns.py View on Github external
r = ast.Call(ast.Attribute(ast.Name('__builtin__', ast.Load(),
                                                    None, None),
                                           'list', ast.Load()),
                             [r], [])
            return r

        if isinstance(node.elt, ast.Constant) and len(node.generators) == 1:
            gen = node.generators[0]
            if not gen.ifs and isinstance(gen.iter, ast.Call):
                try:
                    path = attr_to_path(gen.iter.func)[1]
                    range_path = 'pythonic', '__builtin__', 'functor', 'range'
                    if path == range_path and len(gen.iter.args) == 1:
                        self.update = True
                        return ast.BinOp(
                            ast.List([node.elt], ast.Load()),
                            ast.Mult(),
                            ast.Call(path_to_attr(('__builtin__', 'len')),
                                     [gen.iter],
                                     []))
                except TypeError:
                    pass

        return self.visitComp(node, makeattr)
github serge-sans-paille / pythran / pythran / transformations / expand_imports.py View on Github external
Examples
        --------
        >> from numpy.linalg import det

        >> det(a)

        Becomes

        >> numpy.linalg.det(a)
        """
        if node.id in self.symbols:
            symbol = path_to_node(self.symbols[node.id])
            if not getattr(symbol, 'isliteral', lambda: False)():
                parent = self.ancestors[node][-1]
                blacklist = (ast.Tuple,
                             ast.List,
                             ast.Set,
                             ast.Return)
                if isinstance(parent, blacklist):
                    raise PythranSyntaxError(
                        "Unsupported module identifier manipulation",
                        node)
            new_node = path_to_attr(self.symbols[node.id])
            new_node.ctx = node.ctx
            ast.copy_location(new_node, node)
            return new_node
        return node

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