forked from kfeuz/Python-Baseball
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
91 lines (69 loc) · 2.58 KB
/
utils.py
File metadata and controls
91 lines (69 loc) · 2.58 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
import ast
import inspect
import json
import os
import collections
def convert_ast(node, return_type='string', include_type=False, sep=':'):
count = 1
def _flatten_dict(d, parent_key='', sep=':'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(_flatten_dict(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
def _flatten_list(lst):
return sum(([x] if not isinstance(x, list) else _flatten_list(x) for x in lst), [])
def _format(node):
nonlocal count
if isinstance(node, ast.AST):
d = _flatten_dict({ key: _format(value) for key, value in ast.iter_fields(node) if key != 'ctx'})
if include_type:
d['type'] = node.__class__.__name__
return d
elif isinstance(node, list):
return sep.join(_flatten_list([value for list_node in node for value in _format(list_node).values() if value]))
return str(node)
if not isinstance(node, ast.AST):
raise TypeError('expected AST, got %r' % node.__class__.__name__)
if return_type == 'string':
return sep.join([value for value in _format(node).values() if value])
elif return_type == 'list':
return list(_format(node).values())
else:
return _format(node)
def get_calls(source, return_type='string', include_type=False):
calls = []
def visit_Call(node):
calls.append(convert_ast(node, return_type, include_type))
node_iter = ast.NodeVisitor()
node_iter.visit_Call = visit_Call
try:
node_iter.visit(ast.parse(inspect.getsource(source)))
except OSError:
return []
return calls
def get_assignments(source, return_type='string', include_type=False):
assignments = []
def visit_Assign(node):
assignments.append(convert_ast(node, return_type, include_type))
node_iter = ast.NodeVisitor()
node_iter.visit_Assign = visit_Assign
try:
node_iter.visit(ast.parse(inspect.getsource(source)))
except OSError:
return []
return assignments
def get_for_loops(source, return_type='string', include_type=False):
loops = []
def visit_For(node):
loops.append(convert_ast(node, return_type, include_type))
node_iter = ast.NodeVisitor()
node_iter.visit_For = visit_For
try:
node_iter.visit(ast.parse(inspect.getsource(source)))
except OSError:
return []
return loops