本文整理汇总了Python中codegen.to_source方法的典型用法代码示例。如果您正苦于以下问题:Python codegen.to_source方法的具体用法?Python codegen.to_source怎么用?Python codegen.to_source使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codegen
的用法示例。
在下文中一共展示了codegen.to_source方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_oauth_auth_example
# 需要导入模块: import codegen [as 别名]
# 或者: from codegen import to_source [as 别名]
def test_oauth_auth_example():
import ast
import _ast
import codegen
client_id = "n4mnzQGfDEfOhFixwBvLV2mZJJLvf86pzfMMiPF5"
client_secret = "40ON9IPJRDAngUkVbGBTEjCBAwc2wB7lV8e71jJUPKabdKq6KBTUBKb1xGkh82KtAI1AqISrL3Zi4sTfhCBVh27YvlV6Y5klpXXV5loUWvuhMSRiN3HRZzVDO0fLBibv"
with open("examples/oauth_auth_example.py", "r") as f:
data = f.read()
p = ast.parse(data)
for node in p.body:
if type(node) == _ast.Assign:
if node.targets[0].id == 'client_id':
node.value.s = client_id
if node.targets[0].id == 'client_secret':
node.value.s = client_secret
ls = {}
exec(codegen.to_source(p), ls)
assert ls['course']['courses'][0]['id'] == 67
示例2: ast_to_source
# 需要导入模块: import codegen [as 别名]
# 或者: from codegen import to_source [as 别名]
def ast_to_source(node: ast.AST, old_source: str = None, file: str = None) -> str:
"""
Generate code for node object
"""
if node and not isinstance(node, ast.AST):
raise TypeError('Unexpected type for node: {}'.format(str(type(node))))
if old_source and not isinstance(old_source, str):
raise TypeError('Unexpected type for old_src: {}'.format(str(type(old_source))))
return to_source(node) or old_source
示例3: warn
# 需要导入模块: import codegen [as 别名]
# 或者: from codegen import to_source [as 别名]
def warn(self, func, arg):
"""Warn about calls of bitbake APIs which pass a non-literal
argument for the variable name, as we're not able to track such
a reference.
"""
try:
funcstr = codegen.to_source(func)
argstr = codegen.to_source(arg)
except TypeError:
self.log.debug(2, 'Failed to convert function and argument to source form')
else:
self.log.debug(1, self.unhandled_message % (funcstr, argstr))
示例4: to_source
# 需要导入模块: import codegen [as 别名]
# 或者: from codegen import to_source [as 别名]
def to_source(self):
"""Construct predicate source code.
:return: predicate source code
"""
return codegen.to_source(self.ast())
示例5: Exec
# 需要导入模块: import codegen [as 别名]
# 或者: from codegen import to_source [as 别名]
def Exec(self, source):
self.execution_count += 1
try:
nodes = ast.parse(source, self.filename)
except IndentationError as e:
raise ParseError(e)
except (OverflowError, SyntaxError, ValueError,
TypeError, MemoryError) as e:
raise ParseError(e)
stdout = StringIO.StringIO()
stderr = StringIO.StringIO()
prev_stdout = sys.stdout
prev_stderr = sys.stderr
sys.stdout = stdout
sys.stderr = stderr
try:
if isinstance(nodes.body[-1], ast.Expr):
exec_nodes = nodes.body[:-1]
interactive_nodes = nodes.body[-1:]
else:
exec_nodes, interactive_nodes = nodes.body, []
for node in exec_nodes:
mod = ast.Module([node])
code = compile(mod, self.filename, "exec")
exec(code, self.global_context, self.local_context)
result = None
for node in interactive_nodes:
source = codegen.to_source(node)
new_node = ast.parse(source, self.filename, mode="eval")
mod = ast.Expression(new_node.body)
code = compile(mod, self.filename, "eval")
result = eval(code, self.global_context, self.local_context)
sys.stdout = prev_stdout
sys.stderr = prev_stderr
return stdout.getvalue(), stderr.getvalue(), result
except Exception as e:
raise ExecError(stdout.getvalue(), stderr.getvalue(), e)
finally:
sys.stdout = prev_stdout
sys.stderr = prev_stderr