-
Notifications
You must be signed in to change notification settings - Fork 45
/
stats.py
executable file
·772 lines (622 loc) · 20 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env python3
# coding=utf-8
#
# Python Script
#
# Copyleft © Manoel Vilela
#
#
import pandas as pd # sudo pip install pandas
import numpy as np # sudo pip install numpy
from distutils.spawn import find_executable
from optparse import OptionParser
from os import path
import os
import time
import itertools
import threading
import subprocess
import re
import sys
import hashlib
import fileinput
import signal
# #
# Bulding classes
# #
SOLUTION_TIMEOUT_VALUE = 60
class TimeOutController:
class TimeOut(Exception):
pass
def __init__(self, sec=SOLUTION_TIMEOUT_VALUE):
signal.signal(signal.SIGALRM, self.raise_timeout)
signal.alarm(sec)
def cancel(self):
signal.alarm(0) # disable alarm
def raise_timeout(self, a, n):
raise TimeOutController.TimeOut()
class Checker(object):
checked = []
def __init__(self, compiler, path):
self.compiler = compiler.split()
self.path = os.path.abspath(path)
self.check()
def check(self):
binary = self.compiler[0]
if binary not in self.checked and not find_executable(binary):
raise EnvironmentError("{!r} not found. Do you have the compilers?".format(binary)) # noqa
elif binary not in self.checked:
self.checked += binary
class Execute(Checker):
"""Interactive languages building"""
def enter_dir(self):
self.old_dir = os.getcwd()
os.chdir(path.dirname(self.path))
def exit_dir(self):
os.chdir(self.old_dir)
def execute(self):
self.enter_dir()
before = time.time()
args = self.compiler
args += [self.path]
try:
toc = TimeOutController()
program = subprocess.Popen(args, stdout=subprocess.PIPE)
out, _ = program.communicate()
except TimeOutController.TimeOut:
out = b"TIMEOUT"
program.kill()
finally:
toc.cancel()
time_passed = time.time() - before
self.exit_dir()
return out, program.returncode, time_passed
class Build(Checker):
"""For compiled languages: C++, C for example"""
fout = "compiled.out"
def compile(self):
args = [self.compiler[0], self.path, "-o", self.output] + self.compiler[1:]
program = subprocess.Popen(args, stdout=subprocess.PIPE)
return program.wait() == 0
def execute(self):
self.output = path.join(path.dirname(self.path), self.fout)
if self.compile():
compiled = path.abspath(self.output)
program = Execute("bash -c", "{}".format(compiled))
output = program.execute()
os.remove(compiled)
return output
return b"compiles fails", EnvironmentError, 0
ERASE_LINE = "\x1b[2K"
BUILD_SUPPORT = [
"Python", # you need python | pacman -Su python
"Go", # you need golang | pacman -Su golang
"Clojure", # you need clojure | pacman -Su clojure
"CommonLisp", # you need clisp | pacman -Su clisp
"Haskell", # you need ghc | pacman -Su ghc
"Lua", # you need lua | pacman -Su lua5.3
"Ruby", # you need ruby | pacman -Su ruby
"C", # you need gcc | pacman -Su gcc
"C++", # you need | pacman -Su g++
"Elixir", # you need elixir | pacman -Su elixir
"PHP", # you need php | pacman -Su php
# "Swift", # you need swift | yaourt -Su swift
# "Objective-C", # you need gcc-objc | pacman -Su gcc-objc
"Scheme",
"Racket",
"Bash", # hmm, i think you already have this
"OCaml",
]
BUILD_FILES = ["stats.py", "stats.exs", "test", "add"]
BUILD_MACHINE = {
"Python": {
"cmdline": "python3",
"builder": Execute
},
"Go": {
"cmdline": "go run",
"builder": Execute
},
"Clojure": {
"cmdline": "clojure",
"builder": Execute
},
"CommonLisp": {
"cmdline": "sbcl --script",
"builder": Execute
},
"Racket": {
"cmdline": "racket --script",
"builder": Execute
},
"Scheme": {
"cmdline": "racket --script",
"builder": Execute
},
"Haskell": {
"cmdline": "runhaskell",
"builder": Execute
},
"C": {
"cmdline": "gcc -std=c99 -lm",
"builder": Build
},
"C++": {
"cmdline": "g++ -std=c++0x",
"builder": Build
},
"Lua": {
"cmdline": "lua",
"builder": Execute
},
"Ruby": {
"cmdline": "ruby",
"builder": Execute
},
"Bash": {
"cmdline": "bash",
"builder": Execute
},
"Elixir": {
"cmdline": "elixir",
"builder": Execute
},
"Objective-C": {
"cmdline": "gcc -Wall -lm -lobjc",
"builder": Build
},
"PHP": {
"cmdline": "php",
"builder": Execute
},
"Swift": {
"cmdline": "swift",
"builder": Execute
},
"OCaml" : {
"cmdline" : "ocaml",
"builder": Execute,
}
}
# CLI INTERFACE
# -l (list languages with solutions)
# -c (do count solutions)
# -p (print the path)
# -a all languages selected
# -s language (search)
# -b (build)
# Examples of usage:
# python stats.py --list
# python stats.py --list --count
# python stats.py --all --path
# python stats.py --all --count
# python stats.py -s Python -s Haskell -c
# #
# Cmdline parsing definitions
# #
def _callback(option, opt_str, value, parser):
"""
Used to parse several arguments for one option, knowing that arguments
never start with a `-` and `--`
"""
assert value is None
value = []
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
if arg[:1] == "-" and len(arg) > 1:
break
value.append(arg)
del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)
parser = OptionParser()
parser.add_option(
"-l", "--list",
help="Print a list of the languages whose have solutions",
dest="list",
action="store_true",
default=False,
)
parser.add_option(
"-s", "--search",
help="Choose the languages for print information",
dest="search",
action="append",
default=[],
nargs=1,
)
parser.add_option(
"-f", "--files",
help="Receive a list of file paths to build them",
dest="files",
action="callback",
callback=_callback,
)
parser.add_option(
"-c", "--count",
help="Print the count of each solution",
dest="count",
action="store_true",
default=False,
)
parser.add_option(
"-b", "--build",
help="Execute the solutions and print each solution",
dest="build",
action="store_true",
default=False,
)
parser.add_option(
"-p", "--path",
help="Print the path of each solution",
dest="path",
action="store_true",
default=False,
)
parser.add_option(
"-a", "--all",
help="Select all the languages for search",
dest="all",
action="store_true",
default=False,
)
parser.add_option(
"-m", "--blame",
help="Show the slowest solutions that needs help",
dest="blame",
action="store_true",
default=False,
)
parser.add_option(
"-g", "--graph",
help="Make a cool graph with the final DataFrame data",
dest="graph",
action="store_true",
default=False,
)
parser.usage = "%prog [-s language] [-al] [-cpb] [--blame] [--files] [-g]"
def walk_problems(root="."):
"""
Function: walk_problems
Summary: Walking for repository to get each content of ProblemXXX
Examples: Uniq behavior
Returns: list of 3-uples of strings <list ("1", "2", "3"), ...>
"""
problem = re.compile("./Problem[0-9]{3}/")
problems = []
for x in os.walk(root):
if problem.match(x[0]) and "pycache" not in x[0]:
problems.append(x)
return problems
def read_hashfile(fpath):
"""Read .hash based on fpath and clean the weird chars"""
return open(fpath).read().strip(' -\n')
def get_problem_hashes():
"""
Function: get_problem_hashes
Summary: Walking from each problem and return a tuple
(problem_name, hash_content)
Returns: list of tuples <problem_name: string, hash_content: string>
"""
hash_pattern = re.compile("./Problem[0-9]{3}")
hashes = {}
for file_tuple in os.walk("."):
if hash_pattern.match(file_tuple[0]) and ".hash" in file_tuple[-1]:
problem = file_tuple[0]
hash_path = path.join(problem, '.hash')
hash_content = read_hashfile(hash_path)
hashes[problem.strip('./')] = hash_content
return hashes
def digest_answer(answer):
clean_answer = answer.strip(' \n')
return hashlib.md5(clean_answer.encode('utf-8')).hexdigest()
def search_language(query, languages):
"""
Function: search_language
Summary: Search for languages based on regex
Examples:
>>> search_language(["C"], ["C", "C++", "Python"])
["C", "C++"]
Attributes:
@param (query): list of languages for search
@param (languages): collections of languages normalized
Returns: list of results as strings <list (string)>
"""
return set(query) & set(languages)
def split_problem_language(path):
"""
Function: split_problem_language
Summary: Get a path and split into problem and language
Examples:
>>> split_problem_language("./Problem001/Python")
["Problem001", "Python]
Attributes:
@param (path): path like ./Folder/Language
Returns: [Problem, Language] <(string, string)>
"""
return path.strip("./").split("/")
def is_solution(string):
solution = re.compile("solution_+(?!out)")
return solution.match(string)
def parse_solutions(problems):
"""
Function: parse_solutions
Summary: Organize the solutions of problems
Examples: <NONE>
Attributes:
@param (problems): os.walk functions output
Returns: problem:lang -> [solutions] <dict>
"""
map_solutions = {}
for problem_path, dirs, files in problems:
problem, lang = split_problem_language(problem_path)
map_solutions.setdefault(problem, {}).setdefault(lang, [])
for file in files:
if is_solution(file):
map_solutions[problem][lang].append(file)
return map_solutions
def load_dataframe():
"""
Function: load_dataframe
Summary: Load all solutions of repository at dataframe
Examples:
>>> df = load_dataframe()[]
>>> py = df["Python"]
Python
Problem001 [solution_1.py, solution_2.py]
Problem002 [solution_1.py]
Problem003 [solution_1.py]
Problem004 [solution_1.py]
If you observe: (index + column_name) <- list_solutions -> filepaths!
Returns: pd.DataFrame
"""
return pd.DataFrame.from_dict(parse_solutions(walk_problems()), "index")
def solutions_paths(df, from_files=None):
"""
Function: load_filepaths
Summary: Get each filepath of solutions based on pd.DataFrame
Examples:
>>> df = load_dataframe()
>>> py = df[["CommonLisp"]]
>>> load_filepaths(py)
["..."]
Attributes:
@param (df): pd.DataFrame
Returns: list of file paths
"""
paths = []
if from_files:
for problem, lang, s in from_files:
paths.append((lang, path.join(problem, lang, s)))
return paths
for column in df.columns:
solutions = df[df[column].notnull()][column]
lang = solutions.name
problems = solutions.index
for problem in problems:
p = ((lang, path.join(problem, lang, s))
for s in solutions[problem])
paths.extend(p)
return paths
def count_solutions(df, solutions=True):
"""
Function: count_solutions
Summary: Count the number of solutions of each problem and language
Examples: Iam tired...
Attributes:
@param (df): pd.DataFrame
Returns: pd.DataFrame
"""
df = df.dropna(axis=1, how='all') # columns all nan
df = df.dropna(how='all') # rows all nan
df_ = pd.DataFrame()
df_ = df.applymap(lambda x: len(x) if x is not np.NAN else 0)
if len(df.columns) > 1 and solutions:
df_["Solutions"] = df_[df_.columns].apply(tuple, axis=1).map(sum)
df_ = df_[df_.Solutions > 0]
return df_
def handle_files(files):
"""
Analyse files to return two lists :
- solutions : list of files as 3-uple of strings that are more likely solutions
on the format: (ProblemXXX, 'Lang', 'solution_x.y')
- build_files : list of files that are more build files (stats.py,
stats.exs, ...)
"""
solutions = []
build_files = []
for f in files:
if f.count("/") == 2:
solutions.append(tuple(f.split("/")))
elif f.count("/") == 1 and f.startswith("./"):
dic = parse_solutions(walk_problems(f))
problem = list(dic.keys())[0]
for lang in dic[problem]:
for solution in dic[problem][lang]:
solutions.append((problem, lang, solution))
elif f.count("/") == 0 and f in BUILD_FILES:
build_files.append(f)
return list(filter(lambda x: is_solution(x[2]), solutions)), build_files
# docs?
def spinner(control):
animation = r"⣾⣽⣻⢿⡿⣟⣯"
sys.stdout.write(3 * " ")
for c in itertools.cycle(animation):
current_time = time.time() - control.time
message = "(" + c + ")" + " t: {:.2f}".format(current_time)
sys.stdout.write(message)
time.sleep(0.1)
sys.stdout.write(len(message) * "\010")
sys.stdout.flush()
if control.done:
break
# need docs
def choose_builder(lang, fpath):
try:
if lang in BUILD_MACHINE:
builder = BUILD_MACHINE[lang]['builder']
cmdline = BUILD_MACHINE[lang]['cmdline']
b = builder(cmdline, fpath)
else:
raise Exception("Builder not configured for {!r}! Call the developer".format(lang)) # noqa
except Exception as e:
print("\n", e)
os._exit(1)
finally:
return b
# need docs
def execute_builder(b):
out, err, t = b.execute()
answer = out.decode("utf-8").strip("\n")
if err:
print(err)
os._exit(1)
sys.stdout.write(ERASE_LINE)
building = "\rBuilt {}: Answer: {}: {:.2f}s\n".format(b.path, answer, t)
sys.stdout.write(building)
sys.stdout.flush()
return answer, t
# need docs
def build_result(df, ignore_errors=False, blame=False, only=()):
class Control: # to handle the spinner time at each solution
time = time.time()
done = False
control = Control()
columns = ["Problem", "Language", "Time", "Answer", "Correct"]
data = []
hashes = get_problem_hashes()
spin_thread = threading.Thread(target=spinner, args=(control,))
spin_thread.start()
_problems = only if only else solutions_paths(df)
for lang, spath in _problems:
if "slow" in spath and not blame:
sys.stdout.write("\rIgnored {}: bad solution (slow).\n".format(spath)) # noqa
continue
if lang in BUILD_SUPPORT:
sys.stdout.write("@Building next {}: {}".format(spath, 12 * ' '))
b = choose_builder(lang, spath)
problem = split_problem_language(spath)[0]
outtimed = False
correct = False
control.time = time.time()
answer, t = execute_builder(b)
outtimed = answer == "TIMEOUT"
if (not outtimed) and problem in hashes:
answer_hash = digest_answer(answer)
correct = answer_hash == hashes[problem]
data.append([problem, lang, t, answer, correct])
elif not ignore_errors:
sys.stdout.write("\r{}: Don't have support yet for {!r}!\n".format(spath, lang)) # noqa
sys.stdout.write("\r\n")
sys.stdout.flush()
control.done = True
spin_thread.join()
final_df = pd.DataFrame(data, columns=columns)
return final_df.sort_values("Problem")
def list_by_count(df):
df_ = count_solutions(df, solutions=False)
count = [sum(df_[lang]) for lang in df_.columns]
table = pd.DataFrame(count, index=df_.columns,
columns=["Solutions"])
return table.sort_values("Solutions", ascending=False)
def blame_solutions(df):
df_ = df.applymap(
lambda solutions:
[x for x in solutions if 'slow' in x] or np.NAN
if solutions is not np.NAN else np.NAN
)
return df_
# Problem015 -> 15
def remove_problem(df):
df_ = df
df_.Problem = df.Problem.map(lambda x: x.replace("Problem", "").strip('0'))
return df_
def build_per_language(df):
index = df.Problem.map(int).max()
languages = set(df.Language)
data = {lang: np.full(index, np.nan) for lang in languages}
for _, row in df.iterrows():
data[row['Language']][int(row['Problem']) - 1] = row['Time']
df_ = pd.DataFrame(data, index=range(1, index + 1)).dropna(how='all')
df_.index.name = 'Problems'
return df_
def header(opts):
return "Command: " + ' '.join([x.capitalize() for x in opts if opts[x]])
def handle_graph(df, options):
import matplotlib.pyplot as plt
import matplotlib
cicle_colors = itertools.cycle(['b', 'r', 'g', 'y', 'k'])
my_colors = itertools.islice(cicle_colors, None, len(df))
matplotlib.style.use('ggplot')
if options.build:
df = build_per_language(remove_problem(df))
df.plot()
elif options.list and options.count:
# Make a list by cycling through the colors you care about
# to match the length of your data.
df.plot(kind='barh', stacked=True, color=list(my_colors))
plt.show()
def handle_options(options):
df = load_dataframe()
langs = {x.lower(): x for x in df.columns}
query = [x.lower() for x in options.search]
uncommited_solutions = []
uncommited_core_files = []
tbsolutions = []
count_ws = 0 # wrong solutions
langs_selected = [langs[x] for x in search_language(query, langs)]
if options.files:
uncommited_solutions, uncommited_core_files = handle_files(options.files)
if not uncommited_solutions and uncommited_core_files:
sys.stdout.write(
"\rForced to exit: No solutions to build\nChanged_core_files : \n {}".format(
uncommited_core_files)
)
sys.exit(0)
tbsolutions = solutions_paths(df, from_files=uncommited_solutions)
if options.all:
langs_selected = [x for x in langs.values()]
if options.blame:
df = blame_solutions(df)
if options.list:
if options.count:
df = list_by_count(df)
elif options.path:
langs_selected = [x for x in langs.values()]
else:
df = '\n'.join(sorted(df.dropna(axis=1, how='all').columns))
else:
df = df[langs_selected]
if options.count and not options.list:
df = count_solutions(df)
elif options.build:
try:
df = build_result(df[langs_selected],
options.all,
options.blame,
only=tbsolutions)
count_ws = list(df["Correct"]).count(False)
correct_ratio = 1 - count_ws/len(df) if count_ws else 1
sys.stdout.write(
"Correct solutions ratio : {0}% \n".format(correct_ratio * 100)
)
except(SystemExit, KeyboardInterrupt):
os._exit(1)
elif options.path:
df = '\n'.join(path for _, path in solutions_paths(df[langs_selected]))
pd.set_option("display.max_rows", len(df))
print(df)
if count_ws:
sys.exit(1)
if options.graph:
handle_graph(df, options)
def main():
options, _ = parser.parse_args()
if not any(options.__dict__.values()):
parser.print_help()
os._exit(0)
print(header(options.__dict__))
handle_options(options)
if __name__ == "__main__":
main()