-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
/
file_sizes.py
208 lines (154 loc) · 4.83 KB
/
file_sizes.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from __future__ import print_function
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
from pandas import DataFrame
from pandas.util.testing import set_trace
from pandas import compat
dirs = []
names = []
lengths = []
if len(sys.argv) > 1:
loc = sys.argv[1]
else:
loc = '.'
walked = os.walk(loc)
def _should_count_file(path):
return path.endswith('.py') or path.endswith('.pyx')
def _is_def_line(line):
"""def/cdef/cpdef, but not `cdef class`"""
return (line.endswith(':') and not 'class' in line.split() and
(line.startswith('def ') or
line.startswith('cdef ') or
line.startswith('cpdef ') or
' def ' in line or ' cdef ' in line or ' cpdef ' in line))
class LengthCounter(object):
"""
should add option for subtracting nested function lengths??
"""
def __init__(self, lines):
self.lines = lines
self.pos = 0
self.counts = []
self.n = len(lines)
def get_counts(self):
self.pos = 0
self.counts = []
while self.pos < self.n:
line = self.lines[self.pos]
self.pos += 1
if _is_def_line(line):
level = _get_indent_level(line)
self._count_function(indent_level=level)
return self.counts
def _count_function(self, indent_level=1):
indent = ' ' * indent_level
def _end_of_function(line):
return (line != '' and
not line.startswith(indent) and
not line.startswith('#'))
start_pos = self.pos
while self.pos < self.n:
line = self.lines[self.pos]
if _end_of_function(line):
self._push_count(start_pos)
return
self.pos += 1
if _is_def_line(line):
self._count_function(indent_level=indent_level + 1)
# end of file
self._push_count(start_pos)
def _push_count(self, start_pos):
func_lines = self.lines[start_pos:self.pos]
if len(func_lines) > 300:
set_trace()
# remove blank lines at end
while len(func_lines) > 0 and func_lines[-1] == '':
func_lines = func_lines[:-1]
# remove docstrings and comments
clean_lines = []
in_docstring = False
for line in func_lines:
line = line.strip()
if in_docstring and _is_triplequote(line):
in_docstring = False
continue
if line.startswith('#'):
continue
if _is_triplequote(line):
in_docstring = True
continue
self.counts.append(len(func_lines))
def _get_indent_level(line):
level = 0
while line.startswith(' ' * level):
level += 1
return level
def _is_triplequote(line):
return line.startswith('"""') or line.startswith("'''")
def _get_file_function_lengths(path):
lines = [x.rstrip() for x in open(path).readlines()]
counter = LengthCounter(lines)
return counter.get_counts()
# def test_get_function_lengths():
text = """
class Foo:
def foo():
def bar():
a = 1
b = 2
c = 3
foo = 'bar'
def x():
a = 1
b = 3
c = 7
pass
"""
expected = [5, 8, 7]
lines = [x.rstrip() for x in text.splitlines()]
counter = LengthCounter(lines)
result = counter.get_counts()
assert(result == expected)
def doit():
for directory, _, files in walked:
print(directory)
for path in files:
if not _should_count_file(path):
continue
full_path = os.path.join(directory, path)
print(full_path)
lines = len(open(full_path).readlines())
dirs.append(directory)
names.append(path)
lengths.append(lines)
result = DataFrame({'dirs': dirs, 'names': names,
'lengths': lengths})
def doit2():
counts = {}
for directory, _, files in walked:
print(directory)
for path in files:
if not _should_count_file(path) or path.startswith('test_'):
continue
full_path = os.path.join(directory, path)
counts[full_path] = _get_file_function_lengths(full_path)
return counts
counts = doit2()
# counts = _get_file_function_lengths('pandas/tests/test_series.py')
all_counts = []
for k, v in compat.iteritems(counts):
all_counts.extend(v)
all_counts = np.array(all_counts)
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111)
ax.hist(all_counts, bins=100)
n = len(all_counts)
nmore = (all_counts > 50).sum()
ax.set_title('%s function lengths, n=%d' % ('pandas', n))
ax.set_ylabel('N functions')
ax.set_xlabel('Function length')
ax.text(100, 300, '%.3f%% with > 50 lines' % ((n - nmore) / float(n)),
fontsize=18)
plt.show()