本文整理汇总了Python中ast.Yield方法的典型用法代码示例。如果您正苦于以下问题:Python ast.Yield方法的具体用法?Python ast.Yield怎么用?Python ast.Yield使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.Yield方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_FunctionDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [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)
self.writeAlwaysHeader()
self.writeDeclarations()
# assert isinstance(w.body, astNode.Stmt)
for stmt in w.body[1:]:
self.writeline()
self.visit(stmt)
self.dedent()
self.writeline()
self.write("end")
self.writeline(2)
示例2: visit_While
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visit_While(self, node):
node.breakLabel = _Label("BREAK")
node.loopLabel = _Label("LOOP")
self.labelStack.append(node.breakLabel)
self.labelStack.append(node.loopLabel)
self.visit(node.test)
self.refStack.push()
for n in node.body:
self.visit(n)
self.refStack.pop()
y = node.body[0]
if isinstance(y, ast.Expr):
y = y.value
if node.test.obj == True and \
isinstance(y, ast.Yield) and \
not self.tree.hasYield > 1 and \
not isinstance(self.getObj(y.value), delay):
node.kind = _kind.ALWAYS
self.tree.senslist = y.senslist
self.require(node, not node.orelse, "while-else not supported")
self.labelStack.pop()
self.labelStack.pop()
示例3: visit_Expr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visit_Expr(self, node):
"""
Convert the `Yield` node into a Vyper-specific node type.
Vyper substitutes `yield` for non-pythonic statement such as `log`. Prior
to generating Vyper AST, we must annotate `Yield` nodes with their original
value.
Because `Yield` is an expression-statement, we also remove it from it's
enclosing `Expr` node.
"""
self.generic_visit(node)
if isinstance(node.value, python_ast.Yield):
node = node.value
node.ast_type = self._modification_offsets[(node.lineno, node.col_offset)]
return node
示例4: check_for_b901
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def check_for_b901(self, node):
if node.name == "__await__":
return
has_yield = False
return_node = None
for parent, x in self.walk_function_body(node):
# Only consider yield when it is part of an Expr statement.
if isinstance(parent, ast.Expr) and isinstance(
x, (ast.Yield, ast.YieldFrom)
):
has_yield = True
if isinstance(x, ast.Return) and x.value is not None:
return_node = x
if has_yield and return_node is not None:
self.errors.append(B901(return_node.lineno, return_node.col_offset))
break
示例5: visit_FunctionDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visit_FunctionDef(self, node):
self.generic_visit(node)
if not tree.function_has_inlinecallbacks_decorator(node):
return
if tree.function_is_empty(node):
return
results = []
def yield_statement_callback(inner_node):
if isinstance(inner_node, ast.Yield):
results.append(inner_node)
tree.walk_callback_same_scope(node, yield_statement_callback)
if not results:
self.results.append(
base.Flake8Result(
lineno=node.lineno,
col_offset=node.col_offset,
message=self._error_tmpl
)
)
示例6: visitSubExpr
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visitSubExpr (self, node, child):
def getPriority (exprNode):
if type (exprNode) in (ast.BinOp, ast.BoolOp):
return self.operators [type (exprNode.op)][1]
elif type (exprNode) == ast.Compare:
return self.operators [type (exprNode.ops [0])][1] # All ops have same priority
elif type (exprNode) == ast.Yield:
return -1000000
else:
return 1000000 # No need for parenthesis
if getPriority (child) <= getPriority (node):
self.emit ('(')
self.visit (child)
self.emit (')')
else:
self.visit (child)
示例7: visit_FunctionDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [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)
示例8: _visit_yield
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def _visit_yield(self: 'ASTTagger', node: ast.Yield):
self.symtable.cts.add(ContextType.Generator)
return node
示例9: _has_yield
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def _has_yield(fun): # type: (Union[ast.FunctionDef, ast.AsyncFunctionDef]) -> bool # noqa: E501
for node in ast.walk(fun):
if isinstance(node, ast.Yield) or isinstance(node, ast.YieldFrom):
return True
return False
示例10: visit_For
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visit_For(self, node: ast.For) -> None:
if (
not self._in_async_def and
len(node.body) == 1 and
isinstance(node.body[0], ast.Expr) and
isinstance(node.body[0].value, ast.Yield) and
node.body[0].value.value is not None and
targets_same(node.target, node.body[0].value.value) and
not node.orelse
):
offset = _ast_to_offset(node)
func_info = self._scope_stack[-1]
func_info.yield_from_fors.add(offset)
for target_node in ast.walk(node.target):
if (
isinstance(target_node, ast.Name) and
isinstance(target_node.ctx, ast.Store)
):
func_info.yield_from_names[target_node.id].add(offset)
# manually visit, but with target+body as a separate scope
self.visit(node.iter)
with self._scope():
self.visit(node.target)
for stmt in node.body:
self.visit(stmt)
assert not node.orelse
else:
self.generic_visit(node)
示例11: test_yield
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def test_yield(self):
self.expr(ast.Yield(ast.Name("x", ast.Store())), "must have Load")
self.expr(ast.YieldFrom(ast.Name("x", ast.Store())), "must have Load")
示例12: wrap_code
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def wrap_code(code: str, args: str = '') -> ast.Module:
"""
Compiles Python code into an async function or generator,
and automatically adds return if the function body is a single evaluation.
Also adds inline import expression support.
"""
user_code = import_expression.parse(code, mode='exec')
mod = import_expression.parse(CORO_CODE.format(args), mode='exec')
definition = mod.body[-1] # async def ...:
assert isinstance(definition, ast.AsyncFunctionDef)
try_block = definition.body[-1] # try:
assert isinstance(try_block, ast.Try)
try_block.body.extend(user_code.body)
ast.fix_missing_locations(mod)
KeywordTransformer().generic_visit(try_block)
last_expr = try_block.body[-1]
# if the last part isn't an expression, ignore it
if not isinstance(last_expr, ast.Expr):
return mod
# if the last expression is not a yield
if not isinstance(last_expr.value, ast.Yield):
# copy the value of the expression into a yield
yield_stmt = ast.Yield(last_expr.value)
ast.copy_location(yield_stmt, last_expr)
# place the yield into its own expression
yield_expr = ast.Expr(yield_stmt)
ast.copy_location(yield_expr, last_expr)
# place the yield where the original expression was
try_block.body[-1] = yield_expr
return mod
示例13: visit_Return
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def visit_Return(self, node):
# Do not modify valueless returns
if node.value is None:
return node
# Otherwise, replace the return with a yield & valueless return
return ast.If(
test=ast.NameConstant(
value=True, # if True; aka unconditional, will be optimized out
lineno=node.lineno,
col_offset=node.col_offset
),
body=[
# yield the value to be returned
ast.Expr(
value=ast.Yield(
value=node.value,
lineno=node.lineno,
col_offset=node.col_offset
),
lineno=node.lineno,
col_offset=node.col_offset
),
# return valuelessly
ast.Return(
value=None,
lineno=node.lineno,
col_offset=node.col_offset
)
],
orelse=[],
lineno=node.lineno,
col_offset=node.col_offset
)
示例14: wrap_code
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def wrap_code(code: str, args: str = '') -> ast.Module:
"""
Compiles Python code into an async function or generator,
and automatically adds return if the function body is a single evaluation.
Also adds inline import expression support.
"""
mod = import_expression.parse(CORO_CODE.format(args, textwrap.indent(code, ' ' * 8)), mode='exec')
definition = mod.body[-1] # async def ...:
assert isinstance(definition, ast.AsyncFunctionDef)
try_block = definition.body[-1] # try:
assert isinstance(try_block, ast.Try)
ast.increment_lineno(mod, -16) # bring line numbers back in sync with repl
ast.fix_missing_locations(mod)
KeywordTransformer().generic_visit(try_block)
last_expr = try_block.body[-1]
# if the last part isn't an expression, ignore it
if not isinstance(last_expr, ast.Expr):
return mod
# if the last expression is not a yield
if not isinstance(last_expr.value, ast.Yield):
# copy the value of the expression into a yield
yield_stmt = ast.Yield(last_expr.value)
ast.copy_location(yield_stmt, last_expr)
# place the yield into its own expression
yield_expr = ast.Expr(yield_stmt)
ast.copy_location(yield_expr, last_expr)
# place the yield where the original expression was
try_block.body[-1] = yield_expr
return mod
示例15: p_yield_expr_1
# 需要导入模块: import ast [as 别名]
# 或者: from ast import Yield [as 别名]
def p_yield_expr_1(p):
'''yield_expr : YIELD'''
# 1
p[0] = ast.Yield(None, rule=inspect.currentframe().f_code.co_name, **p[1][1])