本文整理汇总了Python中ast.dump函数的典型用法代码示例。如果您正苦于以下问题:Python dump函数的具体用法?Python dump怎么用?Python dump使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dump函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: eval_left
def eval_left(self, input_line, line_holder):
"""Evaluate left side of the comparison argument line."""
left_holder = list()
left_bin = list()
left_holder.append(self.oper_clean(input_line.test.ops[0]))
if isinstance(input_line.test.comparators[0], ast.BinOp):
if ast.dump(input_line.test.comparators[0].op) == "Mod()":
left_holder.append(input_line.test.comparators[0].left.id)
left_holder.append(self.oper_clean(input_line.test
.comparators[0].op))
left_holder.append(self.var_clean(input_line.test
.comparators[0].right))
else:
left_holder.extend(self.binop_clean(input_line.test
.comparators[0], left_bin))
else:
left_holder.append(self.var_clean(input_line.test.comparators[0]))
if isinstance(input_line.test.left, ast.Name):
left_holder.insert(0, input_line.test.left.id)
line_holder.append(left_holder)
elif isinstance(input_line.test.left, ast.BinOp):
if ast.dump(input_line.test.left.op) == "Mod()":
left_holder.insert(0, input_line.test.left.left.id)
left_holder.insert(1, self.oper_clean(input_line.test.left.op))
left_holder.insert(2, self.var_clean(input_line.test
.left.right))
line_holder.append(left_holder)
else:
self.eval_binop(input_line.test.left, left_holder, line_holder)
return line_holder
示例2: prepare
def prepare(self, node, ctx):
self.env = {'__builtin__': __import__('__builtin__')}
class OperatorRenamer(ast.NodeTransformer):
def visit_Import(self, node):
for n in node.names:
if n.name == "operator_":
n.name = "operator"
return node
node = OperatorRenamer().visit(node)
for module_name in modules:
# module starting with "__" are pythran internal module and
# should not be imported in the Python interpreter
if not module_name.startswith('__'):
if module_name == "operator_":
module_name = "operator" # to import the python module
# operator instead of trying to import the module
# operator_ that does not exist
self.env[module_name] = __import__(module_name)
try:
eval(compile(node, '<constant_folding>', 'exec'), self.env)
except Exception as e:
print ast.dump(node)
print 'error in constant folding: ', e
pass
super(ConstantFolding, self).prepare(node, ctx)
示例3: parse_compare
def parse_compare(compare_node):
assert len(compare_node.ops) == 1, "multiple comparison ops?" + ast.dump(compare_node)
assert isinstance(compare_node.ops[0], ast.Eq), "comparison should be ==" + \
ast.dump(compare_node.ops[0])
lhs = compare_node.left
rhs = compare_node.comparators[0]
if isinstance(lhs, ast.Name) and isinstance(rhs, ast.Num):
var_name = lhs.id
val = rhs.n
elif isinstance(rhs, ast.Name) and isinstance(lhs, ast.Num):
var_name = rhs.id
val = lhs.n
elif isinstance(rhs, ast.Name) and isinstance(lhs, ast.Name):
# try to apply macro
if is_int_constant(rhs):
var_name = lhs.id
val = rhs.id
elif is_int_constant(lhs):
var_name = rhs.id
val = lhs.id
else:
assert False, "Unable to apply macro to fix comparator " + ast.dump(compare_node)
else:
assert False, "unexpected comparator" + ast.dump(compare_node)
return var_name, val
示例4: visit_Mod
def visit_Mod(self, node):
if DEBUG:
print "-----------start node %s -----------" % node.__class__.__name__
print ast.dump(node)
ast.NodeVisitor.generic_visit(self, node)
self.active.push(JavaMod())
if DEBUG: print "-----------end node %s -----------" % node.__class__.__name__
示例5: my_generic_visit
def my_generic_visit(self, node):
if DEBUG:
print "-----------node %s -----------" % node.__class__.__name__
print ast.dump(node)
#ast.NodeVisitor.generic_visit(self, node)
self.visit(node)
if DEBUG: print "-----------node-----------"
示例6: test_load_simple
def test_load_simple():
node = orchestra.ast_util.load('var')
print ast.dump(node)
assert isinstance(node, ast.Name)
assert node.id == 'var'
assert isinstance(node.ctx, ast.Load)
示例7: test_rewrite_notin_precedence
def test_rewrite_notin_precedence():
code1 = "a and b not in c"
code2 = "(a and b) not in c"
code3 = "a and (b not in c)"
code4 = "(b not in c) and a"
rw = query_processor.RewriteChangeNotInPrescedence()
tree1 = ast.parse(code1)
tree2 = ast.parse(code2)
tree3 = ast.parse(code3)
tree1_rw = ast.parse(code1)
tree2_rw = ast.parse(code2)
tree3_rw = ast.parse(code3)
rw.visit(tree1_rw)
rw.visit(tree2_rw)
rw.visit(tree3_rw)
assert_not_equal(ast.dump(tree1), ast.dump(tree2))
assert_equal(ast.dump(tree2), ast.dump(tree2_rw))
assert_equal(ast.dump(tree1_rw), ast.dump(tree2))
assert_equal(ast.dump(tree3), ast.dump(tree3_rw))
assert_equal(ast.dump(tree1), ast.dump(tree3_rw))
示例8: test_import_multi
def test_import_multi(self):
src = 'import mod1, mod2 as x, mod3'
expect_src = '_LX_import_module("mod1")\n_LX_import_module("mod2", asname="x")\n_LX_import_module("mod3")'
tree = ast.parse(src)
tree = TransformImportsAst().visit(tree)
expect_tree = ast.parse(expect_src)
self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
示例9: test_import_from_asname
def test_import_from_asname(self):
src = 'from mymodule import name as alias'
expect_src = '_LX_import_module("mymodule", froms=[("name","alias")])'
tree = ast.parse(src)
tree = TransformImportsAst().visit(tree)
expect_tree = ast.parse(expect_src)
self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
示例10: test_import_asname
def test_import_asname(self):
src = 'import mymodule as thatmodule'
expect_src = '_LX_import_module("mymodule", asname="thatmodule")'
tree = ast.parse(src)
tree = TransformImportsAst().visit(tree)
expect_tree = ast.parse(expect_src)
self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
示例11: test_import_dotted
def test_import_dotted(self):
src = 'import package.mymodule'
expect_src = '_LX_import_module("package.mymodule")'
tree = ast.parse(src)
tree = TransformImportsAst().visit(tree)
expect_tree = ast.parse(expect_src)
self.assertEquals(ast.dump(expect_tree), ast.dump(tree))
示例12: main
def main():
logging.basicConfig(level=logging.INFO)
argument_parser = argparse.ArgumentParser(
description='Translate PHP code from stdin to Python on stdout',
)
argument_parser.add_argument('--php-ast', action='store_true',
help='Dump PHP AST instead of translating to Python')
argument_parser.add_argument('--python-ast', action='store_true',
help='Dump Python AST instead of code')
argument_parser.add_argument('--input-file', help='Read the given file instead of stdin')
command_line_args = argument_parser.parse_args()
if command_line_args.input_file:
input_stream = open(command_line_args.input_file)
else:
input_stream = sys.stdin
parser = XmlPhpParseTreeReader()
statements = parser.parse_php(input_stream)
input_stream.close()
if command_line_args.php_ast:
formatter = PhpAstPrettyFormatter()
print formatter.pretty_format(statements)
return
translator = Translator()
translated_statements = translator.translate_statements(statements)
module = ast.Module(body=translated_statements)
if command_line_args.python_ast:
print ast.dump(module)
else:
unparse.Unparser(module)
示例13: _diff_ast
def _diff_ast(self, code_ast):
# Check if the code structure has changed and update
# the internal variables accordingly
diff_node_index = None
try:
nodes = itertools.izip_longest(code_ast.body, self._code_ast.body)
except AttributeError:
diff_node_index = -1
else:
for i, node in enumerate(nodes):
if (node[0] and not node[1] or
not node[0] and node[1] or
ast.dump(node[0]) != ast.dump(node[1])):
diff_node_index = i
break
if diff_node_index is not None:
self._code_ast = code_ast
try:
self._body_len = len(code_ast.body)
except AttributeError:
self._body_len = -1
if diff_node_index < len(self._compiled_cache):
self._compiled_cache = self._compiled_cache[:diff_node_index]
if diff_node_index < self._next_node_index:
self._reset_execution()
示例14: wrap
def wrap(func):
env=Env()
source=get_func_source(func)
print source
argnames=get_args(func)
print argnames
args={arg_name:arg_type for arg_name,arg_type in zip(argnames, argtypes)}
node=ast.parse(source)
env.node=node
InsertPass(env).run()
InsertReturn(env).run()
print ast.dump(node)
myControlFlowAnalysis(env).run()
clearUnreachedNode(env).run()
TypeInfer(env, args, func.func_globals).run()
print env.cf.blocks
InsertCoerceNode(env).run()
CoerceReturn(env).run()
print ast.dump(node)
NameRewrite(env).run()
SubscriptRewrite(env).run()
InsertArrayInfo(env).run()
InsertDefination(env).run()
print map_name_types
from astunparse import Unparser
from cStringIO import StringIO
buf=StringIO()
Unparser(node,buf)
print buf.getvalue()
print get_return_type(env.return_node_infos)
示例15: get_changes
def get_changes(self):
if not self.text_before: return
tree_before = ast.parse(self.text_before)
tree = ast.parse(self.text)
if ast.dump(tree)==ast.dump(tree_before): print('status','no changes to the script')
else:
print('status','executing changes to %s'%self.file)
# identify changed nodes in the tree and execute
# note that this feature reruns any changed child of the script parent
#! track line numbers are report to the user?
tree_before,tree = [[self.CodeChunk(i,index=ii) for ii,i in
enumerate(ast.iter_child_nodes(ast.parse(t)))]
for t in [self.text_before,self.text]]
intersect = set.intersection(set(tree),set(tree_before))
novel = list(set.difference(set(tree),intersect))
novel_linenos = set([i.this.lineno for i in novel])
class CodeSurgery(ast.NodeTransformer):
def visit(self, node):
if hasattr(node,'lineno') and node.lineno not in novel_linenos:
return ast.parse('last_lineno = %d'%node.lineno).body[0]
else: return ast.NodeTransformer.generic_visit(self,node)
code_ready = ast.fix_missing_locations(CodeSurgery().visit(ast.parse(self.text)))
# run the remainder
out = self.namespace
#! exec to eval for python <2.7.15
eval(compile(code_ready,filename='<ast>',mode='exec'),out,out)