本文整理匯總了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, [])]
示例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
示例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
示例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))
示例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)
示例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
示例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)
示例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)
示例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, [])]
示例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]))
示例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 )
示例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")
示例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))
示例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)
示例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