Python ast.For() Examples
The following are 30
code examples of ast.For().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: bugbear.py From flake8-bugbear with MIT License | 6 votes |
def check_for_b012(self, node): def _loop(node, bad_node_types): if isinstance(node, (ast.AsyncFunctionDef, ast.FunctionDef)): return if isinstance(node, (ast.While, ast.For)): bad_node_types = (ast.Return,) elif isinstance(node, bad_node_types): self.errors.append(B012(node.lineno, node.col_offset)) for child in ast.iter_child_nodes(node): _loop(child, bad_node_types) for child in node.finalbody: _loop(child, (ast.Return, ast.Continue, ast.Break))
Example #2
Source File: test_checker.py From pyflakes with MIT License | 6 votes |
def test_node_types(self): """ Test that the typeable node types are collected """ visitor = self._run_visitor( """\ x = 1 # assignment for x in range(1): pass # for loop def f(): pass # function definition with a as b: pass # with statement """ ) self.assertEqual(visitor.typeable_lines, [1, 2, 3, 4]) self.assertIsInstance(visitor.typeable_nodes[1], ast.Assign) self.assertIsInstance(visitor.typeable_nodes[2], ast.For) self.assertIsInstance(visitor.typeable_nodes[3], ast.FunctionDef) self.assertIsInstance(visitor.typeable_nodes[4], ast.With)
Example #3
Source File: python2ir.py From ppci with BSD 2-Clause "Simplified" License | 6 votes |
def gen_aug_assign(self, statement): """ Compile augmented assign. For example: 'a += 2' """ target = statement.target if isinstance(target, ast.Name): name = target.id assert isinstance(name, str) var = self.get_variable(target, name) assert var.lvalue lhs = self.builder.emit_load(var.value, var.ty) rhs = self.gen_expr(statement.value) op = self.binop_map[type(statement.op)] value = self.emit(ir.Binop(lhs, op, rhs, "augassign", var.ty)) self.emit(ir.Store(value, var.value)) else: # pragma: no cover self.not_impl(statement)
Example #4
Source File: tracer.py From pyrs with MIT License | 6 votes |
def visit_Name(self, node): var = node.scopes.find(get_id(node)) if not var: #TODO why no scopes found for node id? return get_id(node) # if isinstance(var, object): # return "o_b_j_e_c_t" if isinstance(var.assigned_from, ast.For): it = var.assigned_from.iter return "std::declval<typename decltype({0})::value_type>()".format( self.visit(it)) elif isinstance(var.assigned_from, ast.FunctionDef): return get_id(var) else: return self.visit(var.assigned_from.value)
Example #5
Source File: var_decl.py From training_results_v0.6 with Apache License 2.0 | 6 votes |
def visit_Name(self, node): # If it is from the argument list or loop variable, we do not worry about it! if node.id in self._args.keys(): return fors = [loop.target.id for loop in self.scope_level if isinstance(loop, ast.For)] if node.id in fors: return # The loop variable cannot be overwritten when iteration if isinstance(node.ctx, ast.Store) and node.id in fors: raise ValueError("Iter var cannot be overwritten") if node.id not in self.status.keys(): if not isinstance(node.ctx, ast.Store): raise ValueError('In Python, "first store" indicates "declaration"') self.status[node.id] = (node, self.scope_level[-1], set()) else: decl, loop, usage = self.status[node.id] usage.add(type(node.ctx)) self.status[node.id] = (decl, loop, usage)
Example #6
Source File: HeuristicTopoPass.py From pymtl3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def visit_IfExp( self, node ): self.only_loop_at_top &= (self.loop_stack > 0) # Special case "if s.reset:" -- it's only high for a cycle if isinstance( node.test, ast.Attribute ) and \ node.test.attr == 'reset' and \ isinstance( node.test.value, ast.Name ) and \ node.test.value.id == 's': pass else: self.num_br += 1 self.visit( node.test ) self.visit( node.body ) self.visit( node.orelse ) # For/while is fine
Example #7
Source File: model.py From staticfg with Apache License 2.0 | 6 votes |
def get_source(self): """ Get a string containing the Python source code corresponding to the statements in the block. Returns: A string containing the source code of the statements. """ src = "" for statement in self.statements: if type(statement) in [ast.If, ast.For, ast.While]: src += (astor.to_source(statement)).split('\n')[0] + "\n" elif type(statement) == ast.FunctionDef or\ type(statement) == ast.AsyncFunctionDef: src += (astor.to_source(statement)).split('\n')[0] + "...\n" else: src += astor.to_source(statement) return src
Example #8
Source File: syntax.py From fiasko_bro with MIT License | 6 votes |
def indent_not_multiple_of_tab_size(project_folder, tab_size, *args, **kwargs): """ Since there are cases for which col_offset is computed incorrectly, this validator must be nothing more than a simple warning. It compliments the pep8 validator which tends to fail in cases when the indent is incorrect. """ node_types_to_validate = (ast.For, ast.If, ast.FunctionDef, ast.With) for parsed_file in project_folder.get_parsed_py_files(): lines_offsets = file_helpers.get_line_offsets(parsed_file.content) for node in ast.walk(parsed_file.ast_tree): if not ast_helpers.is_node_offset_fine( node, lines_offsets, node_types_to_validate, tab_size, ): return parsed_file.get_name_with_line(node.lineno)
Example #9
Source File: name_nodes.py From wemake-python-styleguide with MIT License | 6 votes |
def get_variables_from_node(node: ast.AST) -> List[str]: """ Gets the assigned names from the list of nodes. Can be used with any nodes that operate with ``ast.Name`` or ``ast.Tuple`` as targets for the assignment. Can be used with nodes like ``ast.Assign``, ``ast.Tuple``, ``ast.For``, ``ast.With``, etc. """ names: List[str] = [] naive_attempt = extract_name(node) if naive_attempt: names.append(naive_attempt) elif isinstance(node, ast.Tuple): for subnode in node.elts: extracted_name = get_variables_from_node(subnode) if extracted_name: names.extend(extracted_name) return names
Example #10
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_for(self): x = ast.Name("x", ast.Store()) y = ast.Name("y", ast.Load()) p = ast.Pass() self.stmt(ast.For(x, y, [], []), "empty body on For") self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []), "must have Store context") self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []), "must have Load context") e = ast.Expr(ast.Name("x", ast.Store())) self.stmt(ast.For(x, y, [e], []), "must have Load context") self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
Example #11
Source File: liveness.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def visit_For(self, for_stmt: ast.For) -> None: assert not for_stmt.orelse self.visit_stmt_list(for_stmt.body) self._live_vars -= find_variables_by_usage(for_stmt.target)[ast.Store] self._live_vars |= find_variables_by_usage(for_stmt.iter)[ast.Load]
Example #12
Source File: yacc.py From yaksok with BSD 3-Clause "New" or "Revised" License | 5 votes |
def p_loop_stmt(t): #'''stmt : expression IDENTIFIER IDENTIFIER IDENTIFIER LOOP suite''' #use_loop_before = False '''stmt : LOOP expression IDENTIFIER IDENTIFIER IDENTIFIER suite''' use_loop_before = True if use_loop_before: container, 의, variable, 마다, suite = t[2], t[3], t[4], t[5], t[6] container_idx = 2 variable_idx = 4 else: container, 의, variable, 마다, suite = t[1], t[2], t[3], t[4], t[6] container_idx = 1 variable_idx = 3 if 의 != '의' or 마다 != '마다': if use_loop_before: report_error(t, "구문 오류입니다: 반복 {} {} {}".format(의, variable, 마다)) else: report_error(t, "구문 오류입니다: {} {} {} 반복".format(의, variable, 마다)) report_error(t, "\t'~ 의 ~ 마다 반복' 꼴이어야 합니다.") raise SyntaxError for_var = ast.Name(variable, ast.Store()) for_var.lineno = t.lineno(variable_idx) for_var.col_offset = -1 # XXX t[0] = ast.For(for_var, container, suite, []) t[0].lineno = t.lineno(container_idx) t[0].col_offset = -1 # XXX
Example #13
Source File: python2ir.py From ppci with BSD 2-Clause "Simplified" License | 5 votes |
def gen_statement(self, statement): """ Generate code for a statement """ if isinstance(statement, list): for inner_statement in statement: self.gen_statement(inner_statement) else: with self.use_location(statement): if isinstance(statement, ast.Pass): pass # No comments :) elif isinstance(statement, ast.Return): self.gen_return(statement) elif isinstance(statement, ast.If): self.gen_if(statement) elif isinstance(statement, ast.While): self.gen_while(statement) elif isinstance(statement, ast.Break): self.gen_break(statement) elif isinstance(statement, ast.Continue): self.gen_continue(statement) elif isinstance(statement, ast.For): self.gen_for(statement) elif isinstance(statement, ast.Assign): self.gen_assign(statement) elif isinstance(statement, ast.Expr): self.gen_expr(statement.value) elif isinstance(statement, ast.AugAssign): self.gen_aug_assign(statement) else: # pragma: no cover self.not_impl(statement)
Example #14
Source File: ast_helpers.py From fiasko_bro with MIT License | 5 votes |
def get_iter_vars_from_for_loops(tree): for_nodes = get_nodes_of_type(tree, (ast.For, ast.comprehension)) try: iter_var_names = {n.target.id for n in for_nodes} except AttributeError: iter_var_names = {} return iter_var_names
Example #15
Source File: preprocessor.py From incubator-tvm with Apache License 2.0 | 5 votes |
def visit_For(self, node): _internal_assert(isinstance(node.target, ast.Name), \ "For's iterator should be an id") self.visit(node.iter) self.scope_level.append(node) for i in node.body: self.visit(i) self.scope_level.pop()
Example #16
Source File: cps.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def visit_For(self, for_stmt: ast.For, ctx: CPSTransformerContext) -> VisitReturnT: # The treatment here is similar to `visit_While`. if for_stmt.orelse: raise NodeNotSupportedError(for_stmt, "For statement with orelse not supported") transformed_for = clone_node(for_stmt) body_ctx = ctx.clone() # Because the iterable wrapper keeps state on the iteration position, it is possible to resume a for loop by # running the same for loop on the same wrapped iterable. body_ctx.enter_loop(transformed_for) transformed_body, body_extras = self.visit_list(for_stmt.body, body_ctx) transformed_for.body = transformed_body return transformed_for, body_extras
Example #17
Source File: loops.py From wemake-python-styleguide with MIT License | 5 votes |
def visit_For(self, node: ast.For) -> None: """ Checks for hidden patterns in sync loops. Raises: ImplicitItemsIteratorViolation """ self._check_implicit_items(node) self.generic_visit(node)
Example #18
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def handleNodeStore(self, node): name = getNodeName(node) if not name: return # if the name hasn't already been defined in the current scope if isinstance(self.scope, FunctionScope) and name not in self.scope: # for each function or module scope above us for scope in self.scopeStack[:-1]: if not isinstance(scope, (FunctionScope, ModuleScope)): continue # if the name was defined in that scope, and the name has # been accessed already in the current scope, and hasn't # been declared global used = name in scope and scope[name].used if used and used[0] is self.scope and name not in self.scope.globals: # then it's probably a mistake self.report(messages.UndefinedLocal, scope[name].used[1], name, scope[name].source) break parent_stmt = self.getParent(node) if isinstance(parent_stmt, (ast.For, ast.comprehension)) or ( parent_stmt != node.parent and not self.isLiteralTupleUnpacking(parent_stmt)): binding = Binding(name, node) elif name == '__all__' and isinstance(self.scope, ModuleScope): binding = ExportBinding(name, node.parent, self.scope) else: binding = Assignment(name, node) self.addBinding(node, binding)
Example #19
Source File: cps.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def visit_While(self, while_stmt: ast.While, ctx: CPSTransformerContext) -> VisitReturnT: if while_stmt.orelse: raise NodeNotSupportedError(while_stmt, "While statement with orelse not supported") # For now `transformed_while` is just a copy of the original `While`. Its loop body will be updated to the # transformed version in the end, in turn updating each continuation that refers to it. transformed_while = clone_node(while_stmt) body_ctx = ctx.clone() body_ctx.enter_loop(transformed_while) transformed_body, body_extras = self.visit_list(while_stmt.body, body_ctx) transformed_while.body = transformed_body # Now the continuation contains the transformed loop body. return transformed_while, body_extras
Example #20
Source File: test_ast.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_base_classes(self): self.assertTrue(issubclass(ast.For, ast.stmt)) self.assertTrue(issubclass(ast.Name, ast.expr)) self.assertTrue(issubclass(ast.stmt, ast.AST)) self.assertTrue(issubclass(ast.expr, ast.AST)) self.assertTrue(issubclass(ast.comprehension, ast.AST)) self.assertTrue(issubclass(ast.Gt, ast.AST))
Example #21
Source File: ast_helpers.py From flake8-variables-names with MIT License | 5 votes |
def extract_all_variable_names(ast_tree: ast.AST) -> List[Tuple[str, ast.AST]]: var_info: List[Tuple[str, ast.AST]] = [] assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.Assign)] var_info += flat([get_var_names_from_assignment(a) for a in assignments]) ann_assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.AnnAssign)] var_info += flat([get_var_names_from_assignment(a) for a in ann_assignments]) funcdefs = [n for n in ast.walk(ast_tree) if isinstance(n, ast.FunctionDef)] var_info += flat([get_var_names_from_funcdef(f) for f in funcdefs]) fors = [n for n in ast.walk(ast_tree) if isinstance(n, ast.For)] var_info += flat([get_var_names_from_for(f) for f in fors]) return var_info
Example #22
Source File: ast_helpers.py From flake8-variables-names with MIT License | 5 votes |
def get_var_names_from_for(for_node: ast.For) -> List[Tuple[str, ast.AST]]: if isinstance(for_node.target, ast.Name): return [(for_node.target.id, for_node.target)] elif isinstance(for_node.target, ast.Tuple): return [(n.id, n) for n in for_node.target.elts if isinstance(n, ast.Name)] return []
Example #23
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_for_stmt_2(p): '''for_stmt : FOR exprlist IN testlist COLON suite ELSE COLON suite''' # 1 2 3 4 5 6 7 8 9 ctx_to_store(p[2]) p[0] = ast.For(p[2], p[4], p[6], p[9], rule=inspect.currentframe().f_code.co_name, **p[1][1]) # try_stmt: ('try' ':' suite # ((except_clause ':' suite)+ # ['else' ':' suite] # ['finally' ':' suite] | # 'finally' ':' suite))
Example #24
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_for_stmt_1(p): '''for_stmt : FOR exprlist IN testlist COLON suite''' # 1 2 3 4 5 6 ctx_to_store(p[2]) p[0] = ast.For(p[2], p[4], p[6], [], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #25
Source File: test_ast.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_base_classes(self): self.assertTrue(issubclass(ast.For, ast.stmt)) self.assertTrue(issubclass(ast.Name, ast.expr)) self.assertTrue(issubclass(ast.stmt, ast.AST)) self.assertTrue(issubclass(ast.expr, ast.AST)) self.assertTrue(issubclass(ast.comprehension, ast.AST)) self.assertTrue(issubclass(ast.Gt, ast.AST))
Example #26
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def make_loop_body_and_orelse(top_of_loop, body_instrs, else_instrs, context): """ Make body and orelse lists for a for/while loop whose first instruction is `top_of_loop`. Parameters ---------- top_of_loop : Instruction The first body of the loop. For a for-loop, this should always be a FOR_ITER. For a while loop, it's the first instruction of the stack builders for the loop test expression body_instrs : deque Queue of Instructions that form the body of the loop. The last two elements of body_instrs should be a JUMP_ABSOLUTE to `top_of_loop` and a POP_BLOCK. else_instrs : deque Queue of Instructions that form the else block of the loop. Should be an empty deque if there is no else block. context : DecompilationContext Returns ------- body : list[ast.AST] List of ast nodes forming the loop body. orelse_body : list[ast.AST] List of ast nodes forming the else-block body. """ # Remove the JUMP_ABSOLUTE and POP_BLOCK instructions at the bottom of the # loop. body_instrs.pop() body_instrs.pop() body = instrs_to_body(body_instrs, context.update(top_of_loop=top_of_loop)) if else_instrs: else_body = instrs_to_body(else_instrs, context) else: else_body = [] return body, else_body
Example #27
Source File: preprocessor.py From incubator-tvm with Apache License 2.0 | 5 votes |
def visit_Name(self, node): # If it is True or False, we do not worry about it! if sys.version_info[0] == 2 and node.id in ['True', 'False']: return # If it is from the argument list or loop variable, we do not worry about it! if node.id in self._args.keys(): return fors = [loop.target.id for loop in self.scope_level if isinstance(loop, ast.For)] if node.id in fors: return # The loop variable cannot be overwritten when iteration _internal_assert(not isinstance(node.ctx, ast.Store) or node.id not in fors, \ "Iter var cannot be overwritten") if node.id not in self.status.keys(): # It is a captured value in closure if node.id in self.closure_vars: try: ast.literal_eval(str(self.closure_vars[node.id])) except ValueError: raise ValueError("Only support capturing constant values in closure") return _internal_assert(isinstance(node.ctx, ast.Store), \ 'Undeclared variable %s' % node.id) if self.aug_assign_: raise ValueError('"First store" cannot be an AugAssign') self.status[node.id] = (node, self.scope_level[-1], set()) else: decl, loop, usage = self.status[node.id] usage.add(type(node.ctx)) _internal_assert(loop in self.scope_level, "%s is used out of the scope it is defined!" % node.id) self.status[node.id] = (decl, loop, usage)
Example #28
Source File: _343.py From codetransformer with GNU General Public License v2.0 | 5 votes |
def make_for_loop(loop_body_instrs, else_body_instrs, context): """ Make an ast.For node. """ # Instructions from start until GET_ITER are the builders for the iterator # expression. iterator_expr = make_expr( popwhile(not_a(instrs.GET_ITER), loop_body_instrs, side='left') ) # Next is the GET_ITER instruction, which we don't need. loop_body_instrs.popleft() # Next is FOR_ITER, which is the jump target for Continue nodes. top_of_loop = loop_body_instrs.popleft() # This can be a STORE_* or an UNPACK_SEQUENCE followed by some number of # stores. target = make_assign_target( loop_body_instrs.popleft(), loop_body_instrs, stack=[], ) body, orelse_body = make_loop_body_and_orelse( top_of_loop, loop_body_instrs, else_body_instrs, context ) return ast.For( target=target, iter=iterator_expr, body=body, orelse=orelse_body, )
Example #29
Source File: ast3.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_For(self, node): new_node = ast.For( self._visit(node.target), self._visit(node.iter), self._visit(node.body), self._visit(node.orelse), ) ast.copy_location(new_node, node) return new_node
Example #30
Source File: python2ir.py From ppci with BSD 2-Clause "Simplified" License | 5 votes |
def common_type(self, ty1, ty2): """ Determine the best target type for two input types. For example, (float, int) -> float (int, int) -> int """ type_ranks = { float: 10, int: 5, } pass