本文整理汇总了Python中ast.stmt方法的典型用法代码示例。如果您正苦于以下问题:Python ast.stmt方法的具体用法?Python ast.stmt怎么用?Python ast.stmt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.stmt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_statement_startend2
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def get_statement_startend2(lineno, node):
import ast
# flatten all statements and except handlers into one lineno-list
# AST's line numbers start indexing at 1
values = []
for x in ast.walk(node):
if isinstance(x, (ast.stmt, ast.ExceptHandler)):
values.append(x.lineno - 1)
for name in ("finalbody", "orelse"):
val = getattr(x, name, None)
if val:
# treat the finally/orelse part as its own statement
values.append(val[0].lineno - 1 - 1)
values.sort()
insert_index = bisect_right(values, lineno)
start = values[insert_index - 1]
if insert_index >= len(values):
end = None
else:
end = values[insert_index]
return start, end
示例2: __exit_scope
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def __exit_scope(self) -> ast.stmt:
"""Create the appropriate scope exiting statement.
The documentation only shows one level and always uses
'return False' in examples.
'raise AltFalse()' within a try.
'break' within a loop.
'return False' otherwise.
"""
if self.in_optional:
return ast.Pass()
if self.in_try:
return ast.Raise(
ast.Call(ast.Name('AltFalse', ast.Load()), [], [], None, None),
None)
if self.in_loop:
return ast.Break()
return ast.Return(ast.Name('False', ast.Load()))
#TODO(bps): find a better name to describe what this does
示例3: visit_Rep0N
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [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, [])]
示例4: get_statement_startend2
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]:
# flatten all statements and except handlers into one lineno-list
# AST's line numbers start indexing at 1
values = [] # type: List[int]
for x in ast.walk(node):
if isinstance(x, (ast.stmt, ast.ExceptHandler)):
values.append(x.lineno - 1)
for name in ("finalbody", "orelse"):
val = getattr(x, name, None) # type: Optional[List[ast.stmt]]
if val:
# treat the finally/orelse part as its own statement
values.append(val[0].lineno - 1 - 1)
values.sort()
insert_index = bisect_right(values, lineno)
start = values[insert_index - 1]
if insert_index >= len(values):
end = None
else:
end = values[insert_index]
return start, end
示例5: test_classdef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def test_classdef(self):
def cls(bases=None, keywords=None, body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords,
body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例6: test_try
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def test_try(self):
p = ast.Pass()
t = ast.Try([], [], [], [p])
self.stmt(t, "empty body on Try")
t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p])
self.stmt(t, "must have Load context")
t = ast.Try([p], [], [], [])
self.stmt(t, "Try has neither except handlers nor finalbody")
t = ast.Try([p], [], [p], [p])
self.stmt(t, "Try has orelse but no except handlers")
t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], [])
self.stmt(t, "empty body on ExceptHandler")
e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])]
self.stmt(ast.Try([p], e, [], []), "must have Load context")
e = [ast.ExceptHandler(None, "x", [p])]
t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p])
self.stmt(t, "must have Load context")
t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))])
self.stmt(t, "must have Load context")
示例7: test_classdef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def test_classdef(self):
def cls(bases=None, keywords=None, starargs=None, kwargs=None,
body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
keywords = []
if body is None:
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
return ast.ClassDef("myclass", bases, keywords, starargs,
kwargs, body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
self.stmt(cls(starargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(kwargs=ast.Name("x", ast.Store())),
"must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
"must have Load context")
示例8: visit
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def visit(self, node):
if isinstance(node, (ast.expr, ast.stmt)):
self.last_line = node.lineno
# do nothing for root
if not self.stack:
return self._visit_children(node)
annotation = self.stack[-1]
# this is a standalone string, may be an annotation
if type(node) is ast.Expr and type(node.value) is ast.Str:
# must not annotate an annotation string
assert annotation is None, 'Annotating an annotation'
return self._visit_string(node)
if annotation is not None: # this expression is annotated
self.stack[-1] = None # so next expression is not
if annotation.startswith('nni.variable'):
return replace_variable_node(node, annotation)
if annotation.startswith('nni.function_choice'):
return replace_function_node(node, annotation)
return self._visit_children(node)
示例9: visit_Module
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def visit_Module(self, node):
for stmt in node.body:
self.visit(stmt)
示例10: visit_stmt
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def visit_stmt(self, body):
for stmt in body:
self.writeline()
self.visit(stmt)
# ugly hack to detect an orphan "task" call
if isinstance(stmt, ast.Call) and hasattr(stmt, 'tree'):
self.write(';')
示例11: visit_FunctionDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def visit_FunctionDef(self, node):
self.writeDoc(node)
w = node.body[-1]
y = w.body[0]
if isinstance(y, ast.Expr):
y = y.value
assert isinstance(y, ast.Yield)
senslist = y.senslist
senslist = self.manageEdges(w.body[1], senslist)
singleEdge = (len(senslist) == 1) and isinstance(senslist[0], _WaiterList)
self.write("%s: process (" % self.tree.name)
if singleEdge:
self.write(senslist[0].sig)
else:
for e in senslist[:-1]:
self.write(e)
self.write(', ')
self.write(senslist[-1])
self.write(") is")
self.indent()
self.writeDeclarations()
self.dedent()
self.writeline()
self.write("begin")
self.indent()
if singleEdge:
self.writeline()
self.write("if %s then" % senslist[0]._toVHDL())
self.indent()
# assert isinstance(w.body, ast.stmt)
for stmt in w.body[1:]:
self.writeline()
self.visit(stmt)
self.dedent()
if singleEdge:
self.writeline()
self.write("end if;")
self.dedent()
self.writeline()
self.write("end process %s;" % self.tree.name)
self.writeline(2)
示例12: getLineNo
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def getLineNo(self, node):
lineno = 0
if isinstance(node, (ast.stmt, ast.expr)):
lineno = node.lineno
return lineno
示例13: _is_ast_stmt
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def _is_ast_stmt(node):
return isinstance(node, ast.stmt)
示例14: visit_Module
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def visit_Module(self, mod):
for stmt in mod.body:
self.visit(stmt)
示例15: default
# 需要导入模块: import ast [as 别名]
# 或者: from ast import stmt [as 别名]
def default(self, node, *args):
if isinstance(node, ast.stmt):
self.visitSimpleStatement(node)
else:
super(PathGraphingAstVisitor, self).default(node, *args)