-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.py
More file actions
323 lines (299 loc) · 14.3 KB
/
tables.py
File metadata and controls
323 lines (299 loc) · 14.3 KB
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
import json
import sys
from docx import Document
class Table1:
def transform_data(self, results: dict) -> dict:
data = {}
for category, games in results.items():
data[category] = {}
for _, game in games.items():
for value in game:
try:
if value[2] not in data:
data[value[2]] = {}
if category not in data[value[2]]:
data[value[2]][category] = {}
if value[0] not in data[value[2]][category]:
data[value[2]][category][value[0]] = 0
data[value[2]][category][value[0]] += 1
if value[0] not in data[category]:
data[category][value[0]] = 0
data[category][value[0]] += 1
data[category]["games_count"] = len(games)
except Exception as e:
print("Error by {} value".format(value))
# print("transformed data: ", json.dumps(data, sort_keys=True, indent=4))
return data
def table_inner(self, data: dict) -> list:
"""generates"""
result = {}
for category_or_tech, cat_data in data.items():
if len(category_or_tech) != 1:
continue
for loc_cat in ["13-15", "16-17", "18-19"]:
loc_cat_data = cat_data[loc_cat]
try:
if category_or_tech not in result:
result[category_or_tech] = []
result[category_or_tech].append(
str(
round(
loc_cat_data["w"] / data[loc_cat]["games_count"] / 2, 2
)
).replace(".", ",")
)
result[category_or_tech].append(
f'{round(loc_cat_data["w"]*100/data[loc_cat]["w"], 2)}%'.replace(
".", ","
)
)
result[category_or_tech].append(
str(
round(
loc_cat_data["l"] / data[loc_cat]["games_count"] / 2, 2
)
).replace(".", ",")
)
result[category_or_tech].append(
f'{round(loc_cat_data["l"]*100/data[loc_cat]["l"], 2)}%'.replace(
".", ","
)
)
except Exception as e:
print("Error by {} value".format(loc_cat_data))
return [result[x] for x in ["a", "b", "c", "d", "e", "h"] if x in result]
def table(self, codes: dict, results: dict) -> list:
_data = self.transform_data(results)
res = self.table_inner(_data)
res.insert(0, ["Технічні прийоми", *["K", "%"] * 6])
res.insert(0, ["Технічні прийоми", *[*["WIN"] * 2, *["LOS"] * 2] * 3])
res.insert(
0,
[
"Технічні прийоми",
*["Вік 13-15 років, разів за гру"] * 4,
*["Вік 16-17 років, разів за гру"] * 4,
*["Вік 18-19 років, разів за одну гру"] * 4,
],
)
# print("table out: ", json.dumps(res, sort_keys=True, indent=4))
for i, code in enumerate(["a", "b", "c", "d", "e", "h"]):
if i > len(res) - 4:
res.append([])
res[i + 3].insert(0, codes[code]["_"])
return res
class Table2:
def transform_data(self, results: dict) -> dict:
data = {}
for category, games in results.items():
data[category] = {}
data[category]["games_count"] = len(games)
for _, game_part in games.items():
for value in game_part:
try:
if value[2] not in data:
data[value[2]] = {}
if value[3] not in data[value[2]]:
data[value[2]][value[3]] = {}
if category not in data[value[2]][value[3]]:
data[value[2]][value[3]][category] = {}
if value[0] not in data[value[2]][value[3]][category]:
data[value[2]][value[3]][category][value[0]] = {}
if value[1] not in data[value[2]][value[3]][category][value[0]]:
data[value[2]][value[3]][category][value[0]][value[1]] = 0
data[value[2]][value[3]][category][value[0]][value[1]] += 1
if value[0] not in data[category]:
data[category][value[0]] = {"_": 0}
if value[1] not in data[category][value[0]]:
data[category][value[0]][value[1]] = {"_": 0}
if value[2] not in data[category][value[0]][value[1]]:
data[category][value[0]][value[1]][value[2]] = 0
data[category][value[0]][value[1]][value[2]] += 1
data[category][value[0]][value[1]]["_"] += 1
data[category][value[0]]["_"] += 1
except Exception as e:
print(value)
# print("tr4ansformed data: ", json.dumps(data, sort_keys=True, indent=4))
return data
def table_inner(self, data: dict, tech: str) -> list:
# count all technical move
result = {}
for cat, cat_data in data[tech].items():
if cat not in result:
result[cat] = []
for cat_age in ["13-15", "16-17", "18-19"]:
if cat_age not in cat_data:
result[cat].extend([*[*["0"] * 2, *["0%"] * 2] * 2])
continue
for res in ["w", "l"]:
try:
if res not in cat_data[cat_age]:
result[cat].extend([*["0"] * 2, *["0%"] * 2])
continue
for player_type in ["y", "z"]:
if player_type not in cat_data[cat_age][res]:
result[cat].append("0")
continue
# print(
# f"{tech}|{cat}|{cat_age}|{res}|{player_type}|K: {cat_data[cat_age][res][player_type]} / {data[cat_age]['games_count']} = {str(cat_data[cat_age][res][player_type]/data[cat_age]['games_count']).replace('.', ',')}"
# )
result[cat].append(
f"{str(cat_data[cat_age][res][player_type]/data[cat_age]['games_count']).replace('.', ',')}"
)
for player_type in ["y", "z"]:
if player_type not in cat_data[cat_age][res]:
result[cat].append("0%")
continue
# print(
# f"{tech}|{cat}|{cat_age}|{res}|{player_type}|%: {cat_data[cat_age][res][player_type]} * 100 / {data[cat_age][res][player_type][tech]} = {str(round(cat_data[cat_age][res][player_type]*100/data[cat_age][res][player_type][tech], 2)).replace('.', '',)}%"
# )
result[cat].append(
f"{round(cat_data[cat_age][res][player_type]*100/data[cat_age][res][player_type][tech], 2)}%".replace(
".", ","
)
)
except Exception as e:
print(cat_data)
return result
def table(
self, codes: dict, results: dict, code_tech: str, codes_to_show: list
) -> list:
data = self.transform_data(results)
res_inner = self.table_inner(data, code_tech)
res = [
[
"Технічні прийоми",
*["Вік 13-15 років, разів за гру"] * 8,
*["Вік 16-17 років, разів за гру"] * 8,
*["Вік 18-19 років, разів за одну гру"] * 8,
],
["Технічні прийоми", *[*["WIN"] * 4, *["LOS"] * 4] * 3],
["Технічні прийоми", *[*["K"] * 2, *["%"] * 2] * 6],
["Технічні прийоми", *["Б", "З"] * 12],
]
# print(codes_to_show)
# print(code_tech)
for i, code in enumerate(codes_to_show):
# print(codes[code_tech][code])
if i > len(res) - 5:
res.append([])
res[i + 4].insert(0, codes[code_tech][code])
res[i + 4].extend(res_inner[code] if code in res_inner else ["0"] * 24)
# sys.exit()
return res
class Table3:
def transform_data(self, results: dict, codes: dict) -> dict:
data = {}
for category, games in results.items():
data[category] = {}
data[category]["games_count"] = len(games)
for _, game_part in games.items():
for value in game_part:
if len(value) != 6:
continue
game_res, amplua, tech1, tech2, act, zone = list(value)
try:
zone = str(int(zone))
except ValueError:
zone = str(ord(zone) - 96)
if tech1 not in data[category]:
data[category][tech1] = {}
if tech2 not in data[category][tech1]:
data[category][tech1][tech2] = {
f"{x}_zone": {"w": {"y": 0, "z": 0}, "l": {"y": 0, "z": 0}}
for x in codes[tech1]["zones"].keys()
}
if act not in data[category][tech1][tech2]:
data[category][tech1][tech2][act] = {
x: {"w": {"y": 0, "z": 0}, "l": {"y": 0, "z": 0}}
for x in codes[tech1]["zones"].keys()
}
data[category][tech1][tech2][act][zone][game_res][amplua] += 1
data[category][tech1][tech2][f"{zone}_zone"][game_res][amplua] += 1
# print("transformed data: ", json.dumps(data, sort_keys=True, indent=4))
return data
def table_inner(self, data: dict, cat: str, tech1: str) -> list:
# count all technical move
result = {}
for tech2, tech2_data in data[cat][tech1].items():
for zone_act, zone_act_data in tech2_data.items():
if "zone" in zone_act:
for game_res in ["w", "l"]:
for amplua in ["y", "z"]:
if tech2 not in result:
result[tech2] = {}
if "_" not in result[tech2]:
result[tech2]["_"] = []
if game_res in zone_act_data:
result[tech2]["_"].append(
str(zone_act_data[game_res][amplua])
if amplua in zone_act_data[game_res]
else "0"
)
else:
for zone, zone_data in zone_act_data.items():
for game_res in ["w", "l"]:
for amplua in ["y", "z"]:
if tech2 not in result:
result[tech2] = {}
if zone_act not in result[tech2]:
result[tech2][zone_act] = []
if game_res in zone_data:
result[tech2][zone_act].append(
str(zone_data[game_res][amplua])
if amplua in zone_data[game_res]
else "0"
)
return result
def table(
self,
codes: dict,
results: dict,
cat: str,
code_tech: str,
codes_to_show: list = None,
) -> list:
if not codes_to_show:
codes_to_show = list(codes[code_tech].keys())
codes_to_show.remove("zones")
codes_to_show.remove("_")
codes_to_show.remove("func")
data = self.transform_data(results, codes)
len_zones = len(codes[code_tech]["zones"])
res_inner = self.table_inner(data, cat, code_tech)
header = [
[
"Напрям руху м'яча",
*[
x2
for x1 in [[x] * 4 for x in codes[code_tech]["zones"].values()]
for x2 in x1
],
],
[
"Результат гри",
*[*["WIN"] * 2, *["LOS"] * 2] * len_zones,
],
["Амплуа гравчині", *[*["Б", "З"] * 2] * len_zones],
]
res = []
for code in codes_to_show:
if code not in res_inner:
res.append([*["0"] * 4] * len_zones)
res[-1].insert(0, codes[code_tech][code])
else:
res.append(res_inner[code]["_"])
res[-1].insert(0, codes[code_tech][code])
for func_n, func in codes[code_tech]["func"].items():
if func_n == "counter":
continue
if code not in res_inner:
res.append([*["0"] * 4] * len_zones)
res[-1].insert(0, func)
elif func_n not in res_inner[code]:
res.append([*["0"] * 4] * len_zones)
res[-1].insert(0, func)
else:
res.append(res_inner[code][func_n])
res[-1].insert(0, func)
return [*header, *res]