forked from snychka/python-decoding-sensor-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
182 lines (164 loc) · 4.54 KB
/
Copy pathnodes.py
File metadata and controls
182 lines (164 loc) · 4.54 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os.path
import warnings
import ast
from collections import OrderedDict
nodes = {
# mod
ast.Module: ["body"],
ast.Interactive: ["body"],
ast.Expression: ["body"],
ast.Suite: ["body"],
# stmt
ast.FunctionDef: ["name", "args", "body", "decorator_list", "returns"],
ast.AsyncFunctionDef: ["name", "args", "body", "decorator_list", "returns"],
ast.ClassDef: ["name", "bases", "keywords", "body", "decorator_list"],
ast.Return: ["value"],
ast.Delete: ["targets"],
ast.Assign: ["targets", "value"],
ast.AugAssign: ["target", "op", "value"],
ast.AnnAssign: ["target", "annotation", "value", "simple"],
ast.For: ["target", "iter", "body", "orelse"],
ast.AsyncFor: ["target", "iter", "body", "orelse"],
ast.While: ["test", "body", "orelse"],
ast.If: ["test", "body", "orelse"],
ast.With: ["items", "body"],
ast.AsyncWith: ["items", "body"],
ast.Raise: ["exc", "cause"],
ast.Try: ["body", "handlers", "orelse", "finalbody"],
ast.Assert: ["test", "msg"],
ast.Import: ["names"],
ast.ImportFrom: ["module", "names", "level"],
ast.Global: ["names"],
ast.Nonlocal: ["names"],
ast.Expr: ["value"],
ast.Pass: [],
ast.Break: [],
ast.Continue: [],
# expr
ast.BoolOp: ["op", "values"],
ast.BinOp: ["left", "op", "right"],
ast.UnaryOp: ["op", "operand"],
ast.Lambda: ["args", "body"],
ast.IfExp: ["test", "body", "orelse"],
ast.Dict: ["keys", "values"],
ast.Set: ["elts"],
ast.ListComp: ["elt", "generators"],
ast.SetComp: ["elt", "generators"],
ast.DictComp: ["key", "value", "generators"],
ast.GeneratorExp: ["elt", "generators"],
ast.Await: ["value"],
ast.Yield: ["value"],
ast.YieldFrom: ["value"],
ast.Compare: ["left", "ops", "comparators"],
ast.Call: ["func", "args", "keywords"],
ast.Num: ["n"],
ast.Str: ["s"],
ast.FormattedValue: ["value", "conversion", "format_spec"],
ast.JoinedStr: ["values"],
ast.Bytes: ["s"],
ast.NameConstant: ["value"],
ast.Ellipsis: [],
ast.Constant: ["value"],
ast.Attribute: ["value", "attr"],
ast.Subscript: ["value", "slice"],
ast.Starred: ["value"],
ast.Name: ["id"],
ast.List: ["elts"],
ast.Tuple: ["elts"],
# expr_context
ast.Load: [],
ast.Store: [],
ast.Del: [],
ast.AugLoad: [],
ast.AugStore: [],
ast.Param: [],
# slice
ast.Slice: ["lower", "upper", "step"],
ast.ExtSlice: ["dims"],
ast.Index: ["value"],
# boolop
ast.And: [],
ast.Or: [],
# operator
ast.Sub: [],
ast.Mult: [],
ast.MatMult: [],
ast.Div: [],
ast.Mod: [],
ast.Pow: [],
ast.LShift: [],
ast.RShift: [],
ast.BitOr: [],
ast.BitXor: [],
ast.BitAnd: [],
ast.FloorDiv: [],
# unaryop
ast.Invert: [],
ast.Not: [],
ast.UAdd: [],
ast.USub: [],
# cmpop
ast.Eq: [],
ast.NotEq: [],
ast.Lt: [],
ast.LtE: [],
ast.Gt: [],
ast.GtE: [],
ast.Is: [],
ast.IsNot: [],
ast.In: [],
ast.NotIn: [],
# comprehension
ast.comprehension: ["target", "iter", "ifs", "is_async"],
# excepthandler
ast.ExceptHandler: ["type", "name", "body"],
# arguments
ast.arguments: [
"args",
"vararg",
"kwonlyargs",
"kw_defaults",
"kwarg",
"defaults",
],
# arg
ast.arg: ["arg", "annotation"],
# keyword
ast.keyword: ["arg", "value"],
# alias
ast.alias: ["name", "asname"],
# withitem
ast.withitem: ["context_expr", "optional_vars"],
}
def convert_node(node):
t = type(node)
if t is str or t is int:
return node
if t is list:
return [convert_node(child) for child in node]
if node is None:
return "nil"
tname = t.__qualname__
d = {"type": tname}
if t not in nodes:
return f"#<{tname}>"
for name in nodes[t]:
d[name] = convert_node(getattr(node, name))
return d
def flatten(d, sep="_"):
obj = OrderedDict()
def recurse(t, parent_key=""):
if isinstance(t, list):
for i in range(len(t)):
recurse(t[i], parent_key + sep + str(i) if parent_key else str(i))
elif isinstance(t, dict):
for k, v in t.items():
if k == "n" or k == "s":
k = "value"
if v == "Str" or v == "NameConstant" or v == "Num":
v = "Constant"
recurse(v, parent_key + sep + k if parent_key else k)
else:
obj[parent_key] = t
recurse(d)
return obj