本文整理汇总了Python中ast.dump方法的典型用法代码示例。如果您正苦于以下问题:Python ast.dump方法的具体用法?Python ast.dump怎么用?Python ast.dump使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ast
的用法示例。
在下文中一共展示了ast.dump方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _inferWaiter
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def _inferWaiter(gen):
f = gen.gi_frame
s = inspect.getsource(f)
s = _dedent(s)
root = ast.parse(s)
root.symdict = f.f_globals.copy()
root.symdict.update(f.f_locals)
# print ast.dump(root)
v = _YieldVisitor(root)
v.visit(root)
if v.kind == _kind.EDGE_TUPLE:
return _EdgeTupleWaiter(gen)
if v.kind == _kind.SIGNAL_TUPLE:
return _SignalTupleWaiter(gen)
if v.kind == _kind.DELAY:
return _DelayWaiter(gen)
if v.kind == _kind.EDGE:
return _EdgeWaiter(gen)
if v.kind == _kind.SIGNAL:
return _SignalWaiter(gen)
# default
return _Waiter(gen)
示例2: test_dump
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def test_dump(self):
node = ast.parse('spam(eggs, "and cheese")')
self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
"keywords=[], starargs=None, kwargs=None))])"
)
self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
"Str('and cheese')], [], None, None))])"
)
self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
"col_offset=11)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
)
示例3: test_increment_lineno
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def test_increment_lineno(self):
src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src, n=3), src)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
'col_offset=0))'
)
# issue10869: do not increment lineno of root twice
src = ast.parse('1 + 1', mode='eval')
self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
'col_offset=0))'
)
示例4: checksum
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def checksum(self):
"""Produce an md5 for the contents of this rule.
This logic applies to expressions within the function only. It does not take
into account: the function name, docstring, comments, or decorator arguments
"""
if not self._checksum:
try:
code = inspect.getsource(self.func)
root = ast.parse(code)
md5 = hashlib.md5() # nosec
for expression in root.body[0].body:
# This check is necessary to ensure changes to the docstring
# are allowed without altering the checksum
if not isinstance(expression, ast.Expr):
md5.update(ast.dump(expression).encode('utf-8'))
self._checksum = md5.hexdigest()
except (TypeError, IndentationError, IndexError):
LOGGER.exception('Could not checksum rule function')
self._checksum = self.CHECKSUM_UNKNOWN
return self._checksum
示例5: test_chained_function
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def test_chained_function(self):
chained_tree = ast.parse("\n".join([
"def a():",
" b = c.d(e).f(g).h(i).j(k)",
]))
separated_tree = ast.parse("\n".join([
"def a():",
" __chain_tmp_3 = c.d(e)",
" __chain_tmp_2 = __chain_tmp_3.f(g)",
" __chain_tmp_1 = __chain_tmp_2.h(i)",
" b = __chain_tmp_1.j(k)",
]))
transformed = PytTransformer().visit(chained_tree)
self.assertEqual(ast.dump(transformed), ast.dump(separated_tree))
示例6: visit_Call
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_Call(self, node):
# This will not visit Flask in Flask(__name__) but it will visit request in `request.args.get()
if not isinstance(node.func, ast.Name):
self.visit(node.func)
for arg_node in itertools.chain(node.args, node.keywords):
arg = arg_node.value if isinstance(arg_node, ast.keyword) else arg_node
if isinstance(arg, ast.Call):
if isinstance(arg.func, ast.Name):
# We can't just visit because we need to add 'ret_'
self.result.append('ret_' + arg.func.id)
elif isinstance(arg.func, ast.Attribute):
# e.g. html.replace('{{ param }}', param)
# func.attr is replace
# func.value.id is html
# We want replace
self.result.append('ret_' + arg.func.attr)
elif isinstance(arg.func, ast.Call):
self.visit_curried_call_inside_call_args(arg)
else:
raise Exception('Cannot visit vars of ' + ast.dump(arg))
else:
self.visit(arg)
示例7: exec_module
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def exec_module(self, module: ModuleType) -> None:
if not hasattr(self._loader, 'get_source'):
return self._loader.exec_module(module)
# get nodes with module-level contracts from the source code
source = self._loader.get_source(module.__name__)
if source is None:
return self._loader.exec_module(module)
tree = ast.parse(source)
nodes = self._get_contracts(tree=tree)
if not nodes:
return self._loader.exec_module(module)
# convert contracts nodes into real contracts
contracts = []
for node in nodes:
contract = self._exec_contract(node=node)
if contract is None:
msg = 'unsupported contract: {}'.format(ast.dump(node))
raise RuntimeError(msg)
contracts.append(contract)
# execute module with contracts
wrapped = _aliases.chain(contract)(self._loader.exec_module)
wrapped(module)
示例8: test_dump
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def test_dump(self):
node = ast.parse('spam(eggs, "and cheese")')
self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
"keywords=[]))])"
)
self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
"Str('and cheese')], []))])"
)
self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
"col_offset=11)], keywords=[], "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
)
示例9: visit_FunctionDef
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
print(f"Function: {node.name}")
print(ast.dump(node))
self.generic_visit(node)
# ADDED AND TESTED
################################################################################################
示例10: visit_AugAssign
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_AugAssign(self, node: ast.AugAssign) -> None:
# +=, -=, /=, *=
print(f"AugAssign: {node}")
print(ast.dump(node))
self.generic_visit(node)
示例11: visit_BinOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_BinOp(self, node: ast.BinOp) -> None:
# + - / * ^ %
print(f"BinOP: {node}")
print(ast.dump(node))
self.generic_visit(node)
示例12: visit_BoolOp
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_BoolOp(self, node: ast.BoolOp) -> None:
# and, or
print(f"Bool: {node}")
print(ast.dump(node))
self.generic_visit(node)
示例13: visit_Compare
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_Compare(self, node: ast.Compare) -> None:
# > < >= == <= !=, is is not
print(f"Compare: {node}")
print(ast.dump(node))
self.generic_visit(node)
示例14: visit_Index
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_Index(self, node: ast.Index) -> None:
# actual slicing
print(f"Index: {node}")
print(ast.dump(node))
示例15: visit_NameConstant
# 需要导入模块: import ast [as 别名]
# 或者: from ast import dump [as 别名]
def visit_NameConstant(self, node: ast.NameConstant) -> None:
# True, False, None
print(f"NameConstant: {node}")
print(ast.dump(node))
self.generic_visit(node)