Debian Codegraph @

docs/api.rst

raw
1 Using radon programmatically
2 ============================
3
4 Radon has a set of functions and classes that you can call from within your
5 program to analyze files.
6
7 Radon's API is composed of three layers:
8
9 * at the very bottom (the lowest level) there are the **Visitors**: with these
10 classes one can build an AST out of the code and get basic metrics.
11 Currently, there are two available visitors:
12 :class:`~radon.visitors.ComplexityVisitor` and
13 :class:`~radon.visitors.HalsteadVisitor`. With the former one analyzes the
14 cyclomatic complexity of the code, while the latter gathers the so-called
15 Halstead metrics. With those and other raw metrics one can compute the
16 Maintainability Index. Example:
17
18 .. code-block:: python
19
20 >>> from radon.visitors import ComplexityVisitor
21 >>> v = ComplexityVisitor.from_code('''
22 def factorial(n):
23 if n < 2: return 1
24 return n * factorial(n - 1)
25
26 def foo(bar):
27 return sum(i for i in range(bar ** 2) if bar % i)
28 ''')
29 >>> v.functions
30 [Function(name='factorial', lineno=2, col_offset=0, endline=4, is_method=False,
31 classname=None, closures=[], complexity=2),
32 Function(name='foo', lineno=6, col_offset=0, endline=7, is_method=False, classname=None,
33 closures=[], complexity=3)]
34
35 * at a higher level, there are helper functions residing in separate modules.
36 For cyclomatic complexity, one can use those inside :mod:`radon.complexity`.
37 For Halstead metrics and MI index those inside :mod:`radon.metrics`. Finally,
38 for raw metrics (that includes SLOC, LLOC, LOC, &c.) one can use the function
39 :func:`~radon.raw.analyze` inside the :mod:`radon.raw` module. With the
40 majority of these functions the result is an object (``Module`` object in the
41 case of raw metrics) or a list of objects (``Function`` or ``Class`` objects
42 for cyclomatic complexity). Example:
43
44 .. code-block:: python
45
46 >>> from radon.complexity import cc_rank, cc_visit
47 >>> cc_rank(4), cc_rank(9), cc_rank(14), cc_rank(23)
48 ('A', 'B', 'C', 'D')
49 >>> cc_visit('''
50 class A(object):
51 def meth(self):
52 return sum(i for i in range(10) if i - 2 < 5)
53
54 def fib(n):
55 if n < 2: return 1
56 return fib(n - 1) + fib(n - 2)
57 ''')
58
59 [Function(name='fib', lineno=6, col_offset=0, endline=8, is_method=False, classname=None,
60 closures=[], complexity=2), Class(name='A', lineno=2, col_offset=0, endline=4,
61 methods=[Function(name='meth', lineno=3, col_offset=4, endline=4, is_method=True,
62 classname='A', closures=[], complexity=3)], real_complexity=3),
63 Function(name='meth', lineno=3, col_offset=4, endline=4, is_method=True, classname='A',
64 closures=[], complexity=3)]
65
66 >>> from radon.raw import analyze
67 >>> analyze("""def _split_tokens(tokens, token, value):
68 '''Split a list of tokens on the specified token pair (token, value),
69 where *token* is the token type (i.e. its code) and *value* its actual
70 value in the code.
71 '''
72 res = [[]]
73 for token_values in tokens:
74 if (token, value) == token_values[:2]:
75 res.append([])
76 continue
77 res[-1].append(token_values)
78 return res
79 """)
80 >>> Module(loc=12, lloc=9, sloc=12, comments=0, multi=4, blank=0)
81
82 * at the highest level there are the **Harvesters**. A Harvester implements all
83 the business logic of the CLI interface. To use a Harvester, it's sufficient
84 to create a :class:`~radon.cli.Config` object (which contains all the config
85 values) and pass it to the Harvester instance along with a list of paths to
86 analyze. An Harvester can then export its result to various formats (for
87 cyclomatic complexity both JSON and XML are available). It's possible to find
88 an example for this in the `Xenon project
89 <https://github.com/rubik/xenon/blob/master/xenon/core.py>`_.
90
91
92 Cyclomatic Complexity
93 ---------------------
94
95 .. py:module:: radon.complexity
96 :synopsis: Complexity-related helper functions.
97
98 .. autofunction:: cc_visit
99
100 .. autofunction:: cc_visit_ast
101
102 .. autofunction:: cc_rank
103
104 .. autofunction:: sorted_results(blocks, order=SCORE)
105
106 Raw metrics
107 -----------
108
109 .. py:module:: radon.raw
110 :synopsis: Raw metrics functions.
111
112 .. autofunction:: analyze
113
114 Other metrics
115 -------------
116
117 .. py:module:: radon.metrics
118 :synopsis: Halstead and Maintainability Index functions.
119
120 .. autofunction:: h_visit
121
122 .. autofunction:: h_visit_ast
123
124 .. autofunction:: mi_visit
125
126 .. autofunction:: mi_rank
127
128 .. autofunction:: mi_parameters
129
130 .. autofunction:: mi_compute
131
132
133 Visitors
134 --------
135
136 .. py:module:: radon.visitors
137 :synopsis: AST visitors (they compute cyclomatic complexity and Halstead metrics)
138
139 .. autoclass:: ComplexityVisitor
140
141 .. autoclass:: HalsteadVisitor
142
143
144 Harvesters
145 ----------
146
147 .. py:module:: radon.cli.harvest
148 :synopsis: Direct interface to Radon's CLI capabilities
149
150 .. autoclass:: Harvester
151 :members:
152
153 .. automethod:: __init__
154
155 .. autoclass:: CCHarvester
156
157 .. autoclass:: RawHarvester
158
159 .. autoclass:: MIHarvester