-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_nodes.py
114 lines (90 loc) · 3.71 KB
/
get_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
A pretty-printing dump function for the ast module. The code was copied from
the ast.dump function and modified slightly to pretty-print.
Alex Leone (acleone ~AT~ gmail.com), 2010-01-30
From http://alexleone.blogspot.co.uk/2010/01/python-ast-pretty-printer.html
"""
from ast import *
def dump(node, annotate_fields=True, include_attributes=False, indent=' '):
"""
Return a formatted dump of the tree in *node*. This is mainly useful for
debugging purposes. The returned string will show the names and the values
for fields. This makes the code impossible to evaluate, so if evaluation is
wanted *annotate_fields* must be set to False. Attributes such as line
numbers and column offsets are not dumped by default. If this is wanted,
*include_attributes* can be set to True.
"""
def _format(node, level=0):
if isinstance(node, AST):
fields = [(a, _format(b, level)) for a, b in iter_fields(node)]
if include_attributes and node._attributes:
fields.extend([(a, _format(getattr(node, a), level))
for a in node._attributes])
return ''.join([
node.__class__.__name__,
'(',
', '.join(('%s=%s' % field for field in fields)
if annotate_fields else
(b for a, b in fields)),
')'])
elif isinstance(node, list):
lines = ['[']
lines.extend((indent * (level + 2) + _format(x, level + 2) + ','
for x in node))
if len(lines) > 1:
lines.append(indent * (level + 1) + ']')
else:
lines[-1] += ']'
return '\n'.join(lines)
return repr(node)
if not isinstance(node, AST):
raise TypeError('expected AST, got %r' % node.__class__.__name__)
return _format(node)
def parseprint(code, filename="<string>", mode="exec", **kwargs):
"""Parse some code from a string and pretty-print it."""
node = parse(code, mode=mode) # An ode to the code
print(dump(node, **kwargs))
# Short name: pdp = parse, dump, print
pdp = parseprint
def load_ipython_extension(ip):
from IPython.core.magic import Magics, magics_class, cell_magic
from IPython.core import magic_arguments
@magics_class
class AstMagics(Magics):
@magic_arguments.magic_arguments()
@magic_arguments.argument(
'-m', '--mode', default='exec',
help="The mode in which to parse the code. Can be exec (the default), "
"eval or single."
)
@cell_magic
def dump_ast(self, line, cell):
"""Parse the code in the cell, and pretty-print the AST."""
args = magic_arguments.parse_argstring(self.dump_ast, line)
parseprint(cell, mode=args.mode)
ip.register_magics(AstMagics)
if __name__ == '__main__':
parseprint('''WdkTtCRGPojeik = {}
class Pet:
def __init__(self, name='', animal_type='', age=0):
self.__name = name
self.__animal_type = animal_type
self.__age = age
def set_name(self, name):
self.__name = name
def set_animal_type(self, animal_type):
self.__animal_type = animal_type
def set_age(self, age):
self.__age = age
def get_name(self):
return self.__name
def get_animal_type(self):
return self.__animal_type
def get_age(self):
return self.__age
name = input('Enter name: ')
animal_type = input('Enter type: ')
age = int(input('Enter age: '))
new_pet = Pet(name, animal_type, age)
print('Pet: ', new_pet.get_name(), new_pet.get_animal_type(), new_pet.get_age())
''')