-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·151 lines (124 loc) · 3.78 KB
/
main.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
#!/usr/bin/env python
import random
from io import BytesIO
from flask import Flask, redirect, render_template, request, send_file, url_for
import algorithms.search
import algorithms.sort
import stats
app = Flask(__name__)
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 600
MIN_TEAM_CHARS = 3
CHOICES_OF_N = [
10,
100,
200,
300,
400,
500,
600,
700,
1_000,
1_500,
2_000,
3_000,
5_000,
10_000,
50_000,
100_000,
500_000,
1_000_000,
]
@app.template_filter("num")
def num_filter(n):
return f"{n:_}"
@app.route("/", methods=["POST", "GET"])
def home():
error = None
if request.method == "POST":
team = request.form["team"]
if len(team) >= MIN_TEAM_CHARS:
return redirect(url_for("team_home", team=team))
else:
error = f"Le nom d'équipe doit avoir au minimum {MIN_TEAM_CHARS} caractères"
return render_template("home.html", error=error)
@app.route("/<team>")
def team_home(team):
return render_template("team_home.html", team=team)
@app.route("/<team>/play")
def team_play(team):
return render_template("team_play.html", team=team)
@app.route("/<team>/search", methods=["POST", "GET"])
def team_search(team):
error = None
stat = None
algorithm_names = algorithms.search.registry.keys()
if request.method == "POST":
algorithm_name = request.form["algorithm_name"]
max = int(request.form["max"])
algorithm = algorithms.search.registry[algorithm_name]
game = algorithms.search.Game(max)
algorithm(max=max, oracle=game.guess)
stat = stats.Stat(
team="autobot", algorithm=algorithm_name, n=max, result=game.guesses
)
stats.insert_stat(stat)
return render_template(
"team_search.html",
team=team,
algorithm_names=algorithm_names,
choices_of_max=CHOICES_OF_N,
stat=stat,
error=error,
)
@app.route("/<team>/sort", methods=["POST", "GET"])
def team_sort(team):
error = None
stat = None
algorithm_names = algorithms.sort.registry.keys()
if request.method == "POST":
algorithm_name = request.form["algorithm_name"]
list_size = int(request.form["list_size"])
algorithm = algorithms.sort.registry[algorithm_name]
lst = list(range(list_size))
random.shuffle(lst)
swaps = algorithm(lst)
print(
f"\nTri d'une liste de taille: {list_size:_} avec {algorithm_name} a demandé {swaps:_} échanges.\n"
)
stat = stats.Stat(
team=team, algorithm=algorithm_name, n=list_size, result=swaps
)
stats.insert_stat(stat)
return render_template(
"team_sort.html",
team=team,
algorithm_names=algorithm_names,
choices_of_size=CHOICES_OF_N,
stat=stat,
error=error,
)
import xlsxwriter
@app.route("/<team>/stats")
def team_stats(team):
team_stats = stats.all_stats(team=team)
excel_io = BytesIO()
workbook = xlsxwriter.Workbook(excel_io)
worksheet = workbook.add_worksheet("mes stats")
for col, header in enumerate(["equipe", "algorithme", "n", "résultat"]):
worksheet.write(0, col, header)
for i, stat in enumerate(team_stats):
worksheet.write(i + 1, 0, stat.team)
worksheet.write(i + 1, 1, stat.algorithm)
worksheet.write(i + 1, 2, stat.n)
worksheet.write(i + 1, 3, stat.result)
workbook.close()
excel_io.seek(0)
return send_file(
excel_io,
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
download_name="my_stats.xlsx",
as_attachment=True,
)
if __name__ == "__main__":
# Only when developing
app.run(host="0.0.0.0", port=8080, debug=True)