forked from alchemyst/Dynamics-and-Control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_list.py
executable file
·84 lines (71 loc) · 2.16 KB
/
function_list.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
#!/usr/bin/env python3
import ast
import sys
import nbformat
import collections
from link import link
def validline(line):
return not (line.startswith('%')
or line.endswith('?')
or line.endswith('.')
or line.startswith('!'))
def namer(obj):
if isinstance(obj, ast.Name):
return obj.id
elif isinstance(obj, ast.Attribute):
return namer(obj.value) + '.' + obj.attr
else:
raise(ValueError)
class FunctionFinder(ast.NodeVisitor):
def __init__(self):
super().__init__()
self.functions = set()
def visit_Call(self, node):
try:
self.functions.add(namer(node.func))
except ValueError:
pass
function_index = collections.defaultdict(set)
all_functions = set()
for f in sys.argv[1:]:
finder = FunctionFinder()
nb = nbformat.read(f, as_version=4)
for cell in nb.cells:
if cell.cell_type == 'code':
block = '\n'.join([line for line in cell.source.split('\n')
if validline(line)])
# print(block)
try:
parsed = ast.parse(block)
except:
# print(f"Parse failed in {f} on this block:")
# print(block)
continue
finder.visit(parsed)
for func in finder.functions:
function_index[func].add(f)
all_functions.update(finder.functions)
known_prefixes = ['numpy', 'scipy',
'sympy', 'mpmath', 'pandas',
'plt',
'control',
'tclab',
'tbcontrol',
'blocksim'
]
sortedfunctions = list(all_functions)
sortedfunctions.sort()
def announce(func):
links = ", ".join(link(f) for f in sorted(function_index[func]))
print(f'* `{func}`: {links}')
for prefix in known_prefixes:
print(f"# {prefix}")
print()
for func in sortedfunctions:
if func.startswith(prefix + '.'):
announce(func)
all_functions.remove(func)
print()
print('# Other')
for func in sorted(all_functions):
announce(func)