-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (102 loc) · 4.17 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
import os
from dataclasses import dataclass, field
from typing import Dict, List
import pandas as pd
import yaml
@dataclass
class Data:
category: str
dir: str
name: str
topics: Dict[str, List[int]] = field(default_factory=dict)
def load_config_yaml(file_path: str) -> Data:
with open(file_path, "r") as file:
data = yaml.safe_load(file)
return Data(**data)
def code(category, basename):
if category == "algorithms":
return f'```python\n--8<-- "{basename}.py"\n```'
elif category == "sql":
return f"""```txt\n--8<-- "sql/{basename}.txt"\n```\n```sql\n--8<-- "sql/{basename}.sql"\n```\n"""
def check_make_file(file_path: str):
if not os.path.exists(file_path):
with open(file_path, "w") as f:
f.write("")
def create(config_path: str) -> str:
cfg = load_config_yaml(os.path.join("config", config_path + ".yaml"))
src = os.path.join("src")
docs = os.path.join("docs")
folder = os.path.join(docs, cfg.dir)
comments = "---\ncomments: True\n---\n\n"
if not os.path.exists(folder):
os.makedirs(folder)
index_md_path = os.path.join(folder, "index.md")
if not os.path.exists(index_md_path):
with open(index_md_path, "w") as f:
f.write(comments + f"# {cfg.name}\n\n")
df = pd.read_parquet(os.path.join("utils", "questions.parquet"))
mkdocs = f" - {cfg.name}:\n"
mkdocs += f" - Home: {cfg.dir}/index.md\n"
for topic, problems in cfg.topics.items():
md_path_name = topic.lower().replace(" ", "_") + ".md"
md_path = os.path.join(folder, md_path_name)
mkdocs += f" - {topic}: {cfg.dir}/{md_path_name}\n"
content = comments + f"# {topic}\n\n"
problems = cfg.topics[topic]
for idx in problems:
row = df.loc[idx]
# file checks
problem_md_path = os.path.join(docs, "md", row["basename"] + ".md")
check_make_file(problem_md_path)
if cfg.category == "algorithms":
problem_py_path = os.path.join(src, row["basename"] + ".py")
check_make_file(problem_py_path)
elif cfg.category == "sql":
problem_sql_path = os.path.join(
src, "sql", row["basename"] + ".sql"
)
problem_txt_path = os.path.join(
src, "sql", row["basename"] + ".txt"
)
check_make_file(problem_sql_path)
check_make_file(problem_txt_path)
content += row["markdown"]
with open(problem_md_path, "r") as f:
content += f.read() + "\n"
content += code(cfg.category, row["basename"]) + "\n\n"
with open(md_path, "w") as f:
f.write(content)
return mkdocs
def main():
main_configuration = os.path.join("config", "main.yaml")
default_mkdocs = os.path.join("utils", "mkdocs.yaml")
output_mkdocs = os.path.join("mkdocs.yaml")
with open(main_configuration, "r") as file:
configs = yaml.safe_load(file)
mkdocs = ""
for config in configs:
mkdocs += create(config) + "\n"
# inset mkdocs into default_mkdocs line 3
with open(default_mkdocs, "r") as file:
lines = file.readlines()
lines.insert(3, mkdocs)
with open(output_mkdocs, "w") as file:
file.writelines(lines)
def create_problem_files(qid: int):
df = pd.read_parquet(os.path.join("utils", "questions.parquet"))
row = df.loc[qid]
problem_md_path = os.path.join("docs", "md", row["basename"] + ".md")
check_make_file(problem_md_path)
if row["categorySlug"] == "algorithms":
problem_py_path = os.path.join("src", row["basename"] + ".py")
check_make_file(problem_py_path)
print("created", problem_py_path)
elif row["categorySlug"] == "database":
problem_sql_path = os.path.join("src", "sql", row["basename"] + ".sql")
problem_txt_path = os.path.join("src", "sql", row["basename"] + ".txt")
check_make_file(problem_sql_path)
check_make_file(problem_txt_path)
print("created", problem_sql_path)
if __name__ == "__main__":
main()
# create_problem_files(2055)