How to use astor - 10 common examples

To help you get started, we’ve selected a few astor examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pannous / angle / tests / ast_eval_inline.py View on Github external
print("\n--------MEDIUM-------- (ok)\n")
	x = ast.dump(my_ast, annotate_fields=True, include_attributes=False)
	print(x)
	print("\n--------SHORT-------- (view-only)\n")
	x = ast.dump(my_ast, annotate_fields=False, include_attributes=False)
	print(x)
	print("")
	if do_exec: exec(expr)
	exit(0)

try:
	inline_ast = "set in execfile:"
	if sys.version < '3':execfile(ast_file)
	else: exec(open(ast_file).read())
	try:
		source = codegen.to_source(inline_ast)
		print(source)  # => CODE
	except Exception as ex:
		print(ex)
	my_ast = ast.fix_missing_locations(inline_ast)
	code = compile(inline_ast, "out/inline", 'exec')
	# if do_exec:
	exec(code)
except Exception as e:
	raise
github fake-name / ChromeController / test_dump_ast.py View on Github external
def docstring_dbg():
	print(astor.dump_tree(astor.code_to_ast(func)))
github berkerpeksag / astor / tests / test_code_gen.py View on Github external
def assertAstEqual(self, ast1, ast2):
        dmp1 = astor.dump_tree(ast1)
        dmp2 = astor.dump_tree(ast2)
        self.assertEqual(dmp1, dmp2)
github berkerpeksag / astor / tests / test_code_gen.py View on Github external
def assertAstEqual(self, ast1, ast2):
        dmp1 = astor.dump_tree(ast1)
        dmp2 = astor.dump_tree(ast2)
        self.assertEqual(dmp1, dmp2)
github berkerpeksag / astor / tests / build_expressions.py View on Github external
def makelib():
    parse = ast.parse
    dump_tree = astor.dump_tree

    def default_value(): return 1000000, ''
    mydict = collections.defaultdict(default_value)

    allparams = [tuple('abcdefghijklmnop'[:x]) for x in range(13)]
    alltxt = itertools.chain(build(1, use_operands=True),
                             build(2, use_operands=True),
                             build(3, select_operators))

    yieldrepl = list(('yield %s %s' % (operator, operand),
                      'yield %s%s' % (operator, operand))
                     for operator in '+-' for operand in '(ab')
    yieldrepl.append(('yield[', 'yield ['))
    # alltxt = itertools.chain(build(1), build(2))
    badexpr = 0
    goodexpr = 0
github berkerpeksag / astor / tests / test_misc.py View on Github external
def test_aliases(self):
        self.assertIs(astor.parse_file, astor.code_to_ast.parse_file)
github berkerpeksag / astor / tests / test_misc.py View on Github external
def test_split_lines_unicode_support(self):
        source = [u'copy', '\n']
        self.assertEqual(split_lines(source), source)
github berkerpeksag / astor / tests / test_misc.py View on Github external
def test_auto_generated_attributes(self):
        # See #136 for more details.
        treewalk = astor.TreeWalk()
        self.assertIsInstance(treewalk.__dict__, dict)
        # Check that the inital state of the instance is empty.
        self.assertEqual(treewalk.__dict__['nodestack'], [])
        self.assertEqual(treewalk.__dict__['pre_handlers'], {})
        self.assertEqual(treewalk.__dict__['post_handlers'], {})
github berkerpeksag / astor / tests / test_transform.py View on Github external
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import astor

def TestMe(x, y, width=10, foo=None):
    from foo.bar.baz.murgatroyd import sally as bob

    a.b = c.d + x.y.z.a.b
    m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)

func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))

class ConsolidateAttributes(astor.TreeWalk):
    def post_Attribute(self):
        node = self.cur_node
        value = node.value
        value.id += '.' + node.attr
        self.replace(value)

ConsolidateAttributes(func_ast)

class FindNames(astor.TreeWalk):
    def init_Assign(self):
        self.assignments = []
        self.current_assign = None
    def pre_Assign(self):
        self.current_assign = [], []
    def post_Assign(self):
        self.assignments.append(self.current_assign)
github berkerpeksag / astor / tests / test_transform.py View on Github external
m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)

func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))

class ConsolidateAttributes(astor.TreeWalk):
    def post_Attribute(self):
        node = self.cur_node
        value = node.value
        value.id += '.' + node.attr
        self.replace(value)

ConsolidateAttributes(func_ast)

class FindNames(astor.TreeWalk):
    def init_Assign(self):
        self.assignments = []
        self.current_assign = None
    def pre_Assign(self):
        self.current_assign = [], []
    def post_Assign(self):
        self.assignments.append(self.current_assign)
        self.current_assign = None

    def pre_targets_name(self):
        self.in_targets = True
    def post_targets_name(self):
        self.in_targets = False
    def post_Name(self):
        if self.current_assign:
            self.current_assign[self.in_targets].append(self.cur_node.id)