-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatemdtables.py
136 lines (106 loc) · 3.83 KB
/
generatemdtables.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
import json
from argparse import ArgumentParser
from pathlib import Path
LINK_ROOT = 'https://tablebase.lichess.ovh/tables/standard/7'
GIGABYTE = 1_000_000_000
def folder_from_egtb_name(name: str) -> str:
"""
Determine EGTB folder (Xvy_pawn(less|ful)) from EGTB name
:param name: EGTB name
"""
l, r = name.split('v')
prefix = f'{len(l)}v{len(r)}'
suffix = '_pawnful' if ('P' in l or 'P' in r) else '_pawnless'
return prefix + suffix
def generate_table(
key: str, statsfile: Path, linksfolder: Path, threshold: float
) -> list:
"""
Generate Markdown tables with download links
from a JSON file with stats.
:param key: key in cumulative stats
:param statsfile: input file with stats
:param linksfolder: folder to store DL lists with links
:param threshold: threshold for percentage of games
"""
with open(statsfile) as f:
data = json.load(f)
total = data['total_games']
egtb_orig = data[key]
tablerows = [
'|#|Name|No. of games|Percentage|WDL|WDL (cumulative)|Download|WDL+DTZ|WDL+DTZ (cumulative)|Download|',
'|:----:|:----:|:----:|:----:|:----:|:----:|:----:|:----:|:----:|:----:|',
]
links_wdl = []
links_wdl_dtz = []
cumulative_wdl = 0
cumulative_wdl_dtz = 0
with open('filesizes.json') as f:
filesizes = json.load(f)
for idx, val in enumerate(egtb_orig.items(), start=1):
name, amount = val
percentage = amount / total * 100
if percentage <= threshold:
break
wdl_size = filesizes[name + '.rtbw']
dtz_size = filesizes[name + '.rtbz']
cumulative_wdl += wdl_size
cumulative_wdl_dtz += wdl_size + dtz_size
wdl_link = f'{LINK_ROOT}/{folder_from_egtb_name(name)}/{name}.rtbw'
dtz_link = f'{LINK_ROOT}/{folder_from_egtb_name(name)}/{name}.rtbz'
links_wdl.append(wdl_link)
wdl_path = f'{key[5:]}/top-{idx}-wdl.txt'
wdl_store_as = linksfolder.joinpath(wdl_path)
with open(wdl_store_as, 'w') as f:
for lnk in links_wdl:
f.write(lnk + '\n')
links_wdl_dtz.append(wdl_link)
links_wdl_dtz.append(dtz_link)
wdl_dtz_path = f'{key[5:]}/top-{idx}-wdl-dtz.txt'
wdl_dtz_store_as = linksfolder.joinpath(wdl_dtz_path)
with open(wdl_dtz_store_as, 'w') as f:
for lnk in links_wdl_dtz:
f.write(lnk + '\n')
tablerows.append(
f'|{idx}'
f'|{name}'
f'|{amount}'
f'|{percentage:.2f}%'
f'|{wdl_size / GIGABYTE:.2f} GB'
f'|{cumulative_wdl / GIGABYTE:.2f} GB'
f'|[List](../{wdl_store_as})'
f'|{(wdl_size + dtz_size) / GIGABYTE:.2f} GB'
f'|{cumulative_wdl_dtz / GIGABYTE:.2f} GB'
f'|[List](../{wdl_dtz_store_as})'
)
return tablerows
def generate_md_file(db: str):
"""
Generate final Markdown file with tables.
:param db: name of the database
"""
statsfile = Path(f'./json_stats/{db}.json')
linksfolder = Path(f'./download_lists/{db}')
mdfile = Path(f'./markdown_tables/{db}.md')
material_diff = generate_table(
'EGTB_material_diff', statsfile, linksfolder, 0.0995
)
most_games = generate_table(
'EGTB_most_games', statsfile, linksfolder, 0.995
)
with open(mdfile, 'w') as f:
f.write(f'# {db.capitalize()}\n\n')
f.write('**Least material imbalance**\n')
for line in material_diff:
f.write(line + '\n')
f.write('<br />\n')
f.write('\n**Most games**\n')
for line in most_games:
f.write(line + '\n')
def main():
ap = ArgumentParser()
ap.add_argument('db', choices=('caissa', 'mega', 'lichess'))
args = ap.parse_args()
generate_md_file(args.db)
if __name__ == '__main__':
main()