forked from tddschn/easygraph-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_graph_info_markdown_table.py
executable file
·100 lines (79 loc) · 2.89 KB
/
gen_graph_info_markdown_table.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
#!/usr/bin/env python3
"""
Author : Xinyuan Chen <[email protected]>
Date : 2022-09-20
Purpose: Why not?
"""
import argparse
from itertools import takewhile
import json
from pathlib import Path
from tempfile import mkstemp
from tomark import Tomark
from config import dataset_homepage_mapping
BEGIN_MARKER = '<!-- BEGIN DATASET TABLE -->'
END_MARKER = '<!-- END DATASET TABLE -->'
def get_markdown_content() -> str:
json_path = Path('./graph_info.json')
graph_info_d = json.loads(json_path.read_text())
data: list[dict[str, str | int | bool]] = []
for graph_name, graph_info in graph_info_d.items():
if graph_name.startswith('stub'):
continue
if graph_name in dataset_homepage_mapping:
homepage = dataset_homepage_mapping[graph_name]
data.append({'Dataset Name': f'[{graph_name}]({homepage})', **graph_info})
else:
data.append({'Dataset Name': graph_name, **graph_info})
sorted_data = sorted(data, key=lambda x: ((n := x['Dataset Name']).startswith('er_') or n.startswith('[er_'), x['nodes'], 'sampled' in n, n.endswith('_directed'))) # type: ignore
# from icecream import ic
# ic(sorted_data)
markdown = Tomark.table(sorted_data) # type: ignore
return markdown
def update_content_between_markers_for_file(
file_path: Path, begin_marker: str, end_marker: str, new_content: str, new_file
) -> None:
with open(file_path) as f:
lines_before = list(takewhile(lambda line: begin_marker not in line, f))
list(takewhile(lambda line: end_marker not in line, f))
next(f)
lines_after = f
with open(new_file, 'w') as f_new:
f_new.writelines(lines_before)
f_new.write(f'\n{begin_marker}\n')
f_new.write(f'\n{new_content}\n')
f_new.write(f'\n{end_marker}\n')
f_new.writelines(lines_after)
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Why not?', formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-u',
'--update-readme',
help='Update the dataset table in the README.md',
action='store_true',
)
parser.add_argument(
'-P',
'--no-print',
help='Do not print the markdown content',
action='store_true',
)
return parser.parse_args()
def main():
"""Make a jazz noise here"""
args = get_args()
markdown_content = get_markdown_content()
if not args.no_print:
print(markdown_content)
if args.update_readme:
temp_file = Path(mkstemp()[1])
readme = Path(__file__).parent / 'README.md'
update_content_between_markers_for_file(
readme, BEGIN_MARKER, END_MARKER, markdown_content, temp_file
)
temp_file.rename(readme)
if __name__ == '__main__':
main()