Repository navigation
Expand file tree
/
Copy pathannotate.py
More file actions
67 lines (51 loc) · 1.92 KB
/
Copy pathannotate.py
File metadata and controls
67 lines (51 loc) · 1.92 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
# -*- coding: UTF-8 -*-
"""
numba --annotate
"""
from __future__ import print_function, division, absolute_import
import operator
from itertools import groupby
from collections import namedtuple
# ______________________________________________________________________
Program = namedtuple("Program", ["python_source", "intermediates"])
SourceIntermediate = namedtuple("SourceIntermediate", ["name", "linenomap",
"source"])
DotIntermediate = namedtuple("DotIntermediate", ["name", "dotcode"]) # graphviz
Source = namedtuple("Source", ["linemap", "annotations"])
Annotation = namedtuple("Annotation", ["type", "value"])
# ______________________________________________________________________
def build_linemap(func):
import inspect
import textwrap
source = inspect.getsource(func)
source = textwrap.dedent(source)
lines = source.split('\n')
if lines[-1] == '':
lines = lines[0:-1]
func_code = getattr(func, 'func_code', None)
if func_code is None:
func_code = getattr(func, '__code__')
lineno = func_code.co_firstlineno
linemap = {}
for line in lines:
linemap[lineno] = line
lineno += 1
return linemap
# ______________________________________________________________________
# Annotation types
A_type = "Types"
A_c_api = "Python C API"
A_numpy = "NumPy"
A_errcheck = "Error check"
A_objcoerce = "Coercion"
A_pycall = "Python call"
A_pyattr = "Python attribute"
# Annotation formatting
def format_annotations(annotations):
adict = groupdict(annotations, 'type')
for category, annotations in adict.items():
vals = u" ".join([str(a.value) for a in annotations])
yield u"%s: %s" % (category, vals)
groupdict = lambda xs, attr: dict(
(k, list(v)) for k, v in groupby(xs, operator.attrgetter(attr)))
# ______________________________________________________________________