本文整理匯總了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])