forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_index.py
60 lines (45 loc) · 1.47 KB
/
module_index.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
from collections import defaultdict, Counter
import glob
import os
import re
from stdlib import is_std_lib
index = defaultdict(set)
import_regex = re.compile('^(?:from|import)\s(?P<module>\w+).*')
dirname = os.getcwd()
def get_dirs():
for path in glob.glob('{}/[0-9]*'.format(dirname)):
yield path
def get_files(path):
for fi in os.listdir(path):
if fi.endswith('.py'):
yield os.path.join(path, fi)
def get_lines(src):
with open(scr) as f:
for line in f:
yield line
if __name__ == '__main__':
for path in get_dirs():
day = os.path.basename(path)
for scr in get_files(path):
for line in get_lines(scr):
m = import_regex.match(line)
if m:
mod = m.groupdict()['module']
index[mod].add(day)
cnt = Counter()
for mod, scripts in sorted(index.items()):
if mod == 'common' or \
any(glob.glob(os.path.join(dirname, day, mod + '.py'))
for day in scripts):
source = 'own'
else:
source = 'stdlib' if is_std_lib(mod) else 'pypi'
cnt[source] += 1
appeared_in = ', '.join(sorted(scripts))
print(f'{mod:<12} | {source:<6} | {appeared_in}')
total = sum(cnt.values())
print()
for source, count in cnt.most_common():
print(f'{source:<10}: {count:>3} ({count/total*100:.1f}%)')
print('-' * 30)
print(f'Total: {total}')