-
Notifications
You must be signed in to change notification settings - Fork 10
/
stats.py
executable file
·89 lines (71 loc) · 2.84 KB
/
stats.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
#!/usr/bin/env python
# Author: Rodolfo Henrique Carvalho
# This is in public domain.
import os
import sys
from collections import defaultdict
def dirname_to_dojo(dirname):
"""Return a dojo from a string representation.
`dirname': "YYYYMMDD[ - problem name - language name]"
`dojo': dict(date) or dict(date, problem, language)
"""
parts = dirname.split(' - ', 2)
if len(parts) == 3:
date, problem, language = parts
return dict(date=date, problem=problem, language=language)
else:
return dict(date=parts[0])
def dojos_from_dir(dirpath):
"""Return a list of dojos in dirpath.
A `dojo' is a directory contained in dirpath that starts
with a digit (for the date).
"""
pth = dirpath
dtd = dirname_to_dojo
ls, isdir, jn = os.listdir, os.path.isdir, os.path.join
isdig = str.isdigit
cond = lambda x: isdir(jn(pth, x)) and isdig(x[0])
return [dtd(x) for x in ls(pth) if cond(x)]
def group_by_language(dojos):
"""Return a dictionary of dojos grouped by languages."""
def append_to_language_group(dikt, dojo):
dikt[dojo.get('language', '?').strip()].append(dojo)
return dikt
return dict(reduce(append_to_language_group, dojos, defaultdict(list)))
def print_dojos_by_language(dojos):
groups = group_by_language(dojos)
for language in sorted(groups.keys(), key=lambda lang: (-len(groups[lang]), lang)):
print '-- %s (%d) %s' % (language, len(groups[language]), '-'*(70 - len(language)))
for dojo in sorted(groups[language]):
print '%s: %s' % (dojo['date'], dojo.get('problem', '?'))
print
def print_dojos_by_language_summary(dojos):
groups = group_by_language(dojos)
print "Linguagem\t# dojos"
for language in sorted(groups.keys(), key=lambda lang: (-len(groups[lang]), lang)):
print '%s\t%d' % (language, len(groups[language]))
if __name__ == '__main__':
dirs = set(sys.argv[1:])
help_ops = ["-h", "--help"]
short_ops = ["-s", "--short"]
if not dirs or dirs.intersection(help_ops):
usage = ("Prints information about dojos in directories.\n"
"USAGE: %s [options] DIRECTORY [DIR2] [DIR3] ...\n"
"Options:\n"
" %-16s show this help\n"
" %-16s show only a summary"
% (sys.argv[0],
' , '.join(help_ops),
' , '.join(short_ops)))
print >>sys.stderr, usage
sys.exit(1)
else:
dojos = []
if dirs.intersection(short_ops):
print_ = print_dojos_by_language_summary
dirs.difference_update(short_ops)
else:
print_ = print_dojos_by_language
while dirs:
dojos.extend(dojos_from_dir(dirs.pop()))
print_(dojos)