当前位置: 首页>>代码示例>>Python>>正文


Python ast.While方法代码示例

本文整理汇总了Python中ast.While方法的典型用法代码示例。如果您正苦于以下问题:Python ast.While方法的具体用法?Python ast.While怎么用?Python ast.While使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ast的用法示例。


在下文中一共展示了ast.While方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: visit_Rep0N

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def visit_Rep0N(self, node: parsing.Rep0N) -> [ast.stmt]:
        """Generates python code for a clause repeated 0 or more times.

        #If all clauses can be inlined
        while clause:
            pass

        while True:
            <code for the clause>
        """
        cl_ast = self.visit(node.pt)
        if isinstance(cl_ast, ast.expr):
            return [ast.While(cl_ast, [ast.Pass()], [])]
        self.in_loop += 1
        clause = self._clause(self.visit(node.pt))
        self.in_loop -= 1
        return [ast.While(ast.Name('True', ast.Load()), clause, [])] 
开发者ID:LionelAuroux,项目名称:pyrser,代码行数:19,代码来源:topython.py

示例2: get_source

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
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 
开发者ID:coetaur0,项目名称:staticfg,代码行数:20,代码来源:model.py

示例3: process_While

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def process_While(self, block: "Block", node: ast.While) -> "Block":
        """Process a while loop.

        A while loop will generate 3 functions: The test, the body, and the
        continuation.

        """
        with About(block.graph.debug, "while_header"):
            header_block = self.new_block(auxiliary=True)
        with About(block.graph.debug, "while_body"):
            body_block = self.new_block(auxiliary=True)
        with About(block.graph.debug, "while_after"):
            after_block = self.new_block(auxiliary=True)
        body_block.preds.append(header_block)
        after_block.preds.append(header_block)
        block.jump(header_block)
        cond = self.process_node(header_block, node.test)
        body_block.mature()
        header_block.cond(cond, body_block, after_block)
        after_body = self.process_statements(body_block, node.body)
        if not after_body.graph.return_:
            after_body.jump(header_block)
        header_block.mature()
        after_block.mature()
        return after_block 
开发者ID:mila-iqia,项目名称:myia,代码行数:27,代码来源:parser.py

示例4: check_for_b012

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
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)) 
开发者ID:PyCQA,项目名称:flake8-bugbear,代码行数:18,代码来源:bugbear.py

示例5: make_while_loop

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def make_while_loop(test_and_body_instrs, else_body_instrs, context):
    """
    Make an ast.While node.

    Parameters
    ----------
    test_and_body_instrs : deque
        Queue of instructions forming the loop test expression and body.
    else_body_instrs : deque
        Queue of instructions forming the else block of the loop.
    context : DecompilationContext
    """
    top_of_loop = test_and_body_instrs[0]

    # The popped elements are the stack_builders for the loop test expression.
    # The top of the loop_body_instrs is either a POP_JUMP_IF_TRUE or a
    # POP_JUMP_IF_FALSE.
    test, body_instrs = make_while_loop_test_expr(test_and_body_instrs)
    body, orelse_body = make_loop_body_and_orelse(
        top_of_loop, body_instrs, else_body_instrs, context,
    )

    # while-else blocks are not yet supported or handled.
    return ast.While(test=test, body=body, orelse=orelse_body) 
开发者ID:llllllllll,项目名称:codetransformer,代码行数:26,代码来源:_343.py

示例6: translate_while

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def translate_while(self, exp):
        if not (len(exp) >= 3):
            raise SyntaxError(exp, self.filename)

        test_exp = exp[1]
        body_exps = exp[2:]

        pre = []

        test_pre, test_value = self.translate(test_exp, False)
        pre.extend(test_pre)

        body = self._translate_sequence(body_exps)
        pre.append(ast.While(test=test_value,
                             body=body,
                             orelse=[],
                             lineno=exp[0].lineno,
                             col_offset=0))

        _, ref = self.translate_ref(NONE_SYM)
        return pre, ref 
开发者ID:i2y,项目名称:mochi,代码行数:23,代码来源:translation.py

示例7: handleNodeDelete

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def handleNodeDelete(self, node):

        def on_conditional_branch():
            """
            Return `True` if node is part of a conditional body.
            """
            current = getattr(node, 'parent', None)
            while current:
                if isinstance(current, (ast.If, ast.While, ast.IfExp)):
                    return True
                current = getattr(current, 'parent', None)
            return False

        name = getNodeName(node)
        if not name:
            return

        if on_conditional_branch():
            # We cannot predict if this conditional branch is going to
            # be executed.
            return

        if isinstance(self.scope, FunctionScope) and name in self.scope.globals:
            self.scope.globals.remove(name)
        else:
            try:
                del self.scope[name]
            except KeyError:
                self.report(messages.UndefinedName, node, name) 
开发者ID:AtomLinter,项目名称:linter-pylama,代码行数:31,代码来源:checker.py

示例8: while_stmt_rewrite

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def while_stmt_rewrite(test, body, orelse):
    orelse = orelse or []
    return ast.While(test, body, orelse) 
开发者ID:Xython,项目名称:YAPyPy,代码行数:5,代码来源:helper.py

示例9: visit_Rep1N

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def visit_Rep1N(self, node: parsing.Rep0N) -> [ast.stmt]:
        """Generates python code for a clause repeated 1 or more times.

        <code for the clause>
        while True:
            <code for the clause>
        """
        clause = self.visit(node.pt)
        if isinstance(clause, ast.expr):
            return (self._clause(clause) + self.visit_Rep0N(node))
        self.in_loop += 1
        clause = self._clause(self.visit(node.pt))
        self.in_loop -= 1
        return self._clause(self.visit(node.pt)) + [
            ast.While(ast.Name('True', ast.Load()), clause, [])] 
开发者ID:LionelAuroux,项目名称:pyrser,代码行数:17,代码来源:topython.py

示例10: visit_For

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def visit_For(self, node):
        # Turn for+range loop into a while loop:
        #   i = start
        #   while i < stop:
        #       body
        #       i = i + step
        assert isinstance(node.iter, ast.Call) and \
            node.iter.func.id == 'range', \
            'for can only be used with range()'
        range_args = node.iter.args
        if len(range_args) == 1:
            start = ast.Num(n=0)
            stop = range_args[0]
            step = ast.Num(n=1)
        elif len(range_args) == 2:
            start, stop = range_args
            step = ast.Num(n=1)
        else:
            start, stop, step = range_args
            if (isinstance(step, ast.UnaryOp) and
                    isinstance(step.op, ast.USub) and
                    isinstance(step.operand, ast.Num)):
                # Handle negative step
                step = ast.Num(n=-step.operand.n)
            assert isinstance(step, ast.Num) and step.n != 0, \
                'range() step must be a nonzero integer constant'
        self.visit(ast.Assign(targets=[node.target], value=start))
        test = ast.Compare(
            left=node.target,
            ops=[ast.Lt() if step.n > 0 else ast.Gt()],
            comparators=[stop],
        )
        incr = ast.Assign(
            targets=[node.target],
            value=ast.BinOp(left=node.target, op=ast.Add(), right=step),
        )
        self.visit(ast.While(test=test, body=node.body + [incr])) 
开发者ID:benhoyt,项目名称:pyast64,代码行数:39,代码来源:pyast64.py

示例11: visit_FunctionDef

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def visit_FunctionDef( self, node ):
    for stmt in node.body:
      self.only_loop_at_top |= isinstance( stmt, (ast.For, ast.While) )

    for stmt in node.body:
      self.visit( stmt )

    if node.returns:
      for expr in node.returns:
        self.visit( expr ) 
开发者ID:pymtl,项目名称:pymtl3,代码行数:12,代码来源:HeuristicTopoPass.py

示例12: test_while

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def test_while(self):
        self.stmt(ast.While(ast.Num(3), [], []), "empty body on While")
        self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []),
                  "must have Load context")
        self.stmt(ast.While(ast.Num(3), [ast.Pass()],
                             [ast.Expr(ast.Name("x", ast.Store()))]),
                             "must have Load context") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:9,代码来源:test_ast.py

示例13: skip_node

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def skip_node(node):
    """Whether to skip a step in the traceback based on ast node type."""
    return isinstance(node, (ast.If, ast.While, ast.For)) 
开发者ID:mila-iqia,项目名称:myia,代码行数:5,代码来源:traceback.py

示例14: handleNodeDelete

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def handleNodeDelete(self, node):

        def on_conditional_branch():
            """
            Return `True` if node is part of a conditional body.
            """
            current = getattr(node, '_pyflakes_parent', None)
            while current:
                if isinstance(current, (ast.If, ast.While, ast.IfExp)):
                    return True
                current = getattr(current, '_pyflakes_parent', None)
            return False

        name = getNodeName(node)
        if not name:
            return

        if on_conditional_branch():
            # We cannot predict if this conditional branch is going to
            # be executed.
            return

        if isinstance(self.scope, FunctionScope) and name in self.scope.globals:
            self.scope.globals.remove(name)
        else:
            try:
                del self.scope[name]
            except KeyError:
                self.report(messages.UndefinedName, node, name) 
开发者ID:PyCQA,项目名称:pyflakes,代码行数:31,代码来源:checker.py

示例15: _missing__While

# 需要导入模块: import ast [as 别名]
# 或者: from ast import While [as 别名]
def _missing__While(self, node):
        body_nodes = self.find_non_missing_node(NodeList(node.body))
        if not body_nodes:
            return None
        # Make a synthetic While-true node.
        new_while = ast.While()
        new_while.lineno = body_nodes.lineno
        new_while.test = ast.Name()
        new_while.test.lineno = body_nodes.lineno
        new_while.test.id = "True"
        new_while.body = body_nodes.body
        new_while.orelse = None
        return new_while 
开发者ID:nedbat,项目名称:coveragepy,代码行数:15,代码来源:parser.py


注:本文中的ast.While方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。