Skip to content

Commit e0f4f5e

Browse files
committed
Expand testing and simpify/correct formatting
1 parent 82c5ba4 commit e0f4f5e

6 files changed

Lines changed: 351 additions & 277 deletions

File tree

leetcode_study_tool/formatters.py

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,27 @@
1212
loader=FileSystemLoader(template_dir), autoescape=select_autoescape(["html", "xml"])
1313
)
1414

15+
EXCEL_COLUMNS = [
16+
"ID",
17+
"Title",
18+
"Difficulty",
19+
"URL",
20+
"Date Attempted",
21+
"Tags",
22+
"Neetcode Video",
23+
"Neetcode Short",
24+
"Solution Code",
25+
"Solutions",
26+
"Companies",
27+
]
28+
29+
DATE_COLUMN_INDEX = EXCEL_COLUMNS.index("Date Attempted")
30+
DIFFICULTY_COLUMN_INDEX = EXCEL_COLUMNS.index("Difficulty")
31+
1532

1633
def format_solution_link(slug: str, solution_id: str) -> str:
1734
"""
18-
format a link to the LeetCode solution with the given ID.
35+
Format a link to the LeetCode solution with the given ID.
1936
2037
Arguments
2138
---------
@@ -36,13 +53,13 @@ def render_template(
3653
template_path: Optional[str], template_name: Optional[str], **kwargs
3754
) -> str:
3855
"""
39-
Render a template with the given context
56+
Render a template with the given context.
4057
4158
Arguments
4259
---------
4360
template_path : Optional[str]
4461
Path to a custom template file. If None, use built-in templates.
45-
template_name : str
62+
template_name : Optional[str]
4663
Name of the built-in template to use if template_path is None.
4764
kwargs : dict
4865
Template context variables.
@@ -54,6 +71,7 @@ def render_template(
5471
"""
5572
if not template_path and not template_name:
5673
raise ValueError("Either template_path or template_name must be provided")
74+
5775
if template_path:
5876
custom_dir = Path(template_path).parent
5977
custom_file = Path(template_path).name
@@ -63,7 +81,6 @@ def render_template(
6381
)
6482
template = custom_env.get_template(custom_file)
6583
else:
66-
assert template_name is not None
6784
template = env.get_template(template_name)
6885

6986
kwargs["solution_url"] = format_solution_link
@@ -74,7 +91,7 @@ def format_anki(
7491
url: str, slug: str, data: dict, template_path: Optional[str] = None
7592
) -> str:
7693
"""
77-
formats an Anki problem using Jinja template
94+
Format an Anki problem using a Jinja template.
7895
7996
Arguments
8097
---------
@@ -108,30 +125,9 @@ def format_anki(
108125
return rendered
109126

110127

111-
def format_quizlet(url: str, slug: str, data: dict):
112-
"""
113-
formats a Quizlet problem for the given URL and data
114-
115-
Arguments
116-
---------
117-
url : str
118-
The URL of the question to format a problem for.
119-
slug : str
120-
The slug of the question to format a problem for.
121-
data : dict
122-
The data of the question to format a problem for.
123-
124-
Returns
125-
-------
126-
str
127-
The Quizlet problem for the given URL and data.
128-
"""
129-
pass
130-
131-
132128
def format_excel(url: str, slug: str, data: dict) -> List[Union[str, date]]:
133129
"""
134-
formats an Excel problem for the given URL and data
130+
Format an Excel row for the given URL and data.
135131
136132
Arguments
137133
---------
@@ -144,57 +140,42 @@ def format_excel(url: str, slug: str, data: dict) -> List[Union[str, date]]:
144140
145141
Returns
146142
-------
147-
str
148-
The Excel problem for the given URL and data. The problem
149-
is formatted as a list of strings, where each string is a
150-
column in the Excel file. This row will have the ordering:
151-
[id, title, difficulty, url, date attempted, tags, video_url, short_url, solutions, companies]
143+
List[Union[str, date]]
144+
A list of cell values matching EXCEL_COLUMNS:
145+
[id, title, difficulty, url, date_attempted, tags,
146+
video_url, short_url, solution_code, solutions, companies]
152147
"""
153-
row = []
154-
row.append(data["id"])
155-
row.append(data["title"])
156-
row.append(data["difficulty"])
157-
row.append(get_url(url))
158-
row.append(date.today())
159-
row.append(", ".join([tag["name"] for tag in data["tags"]]))
160-
if str(data["id"]) in LEETCODE_TO_NEETCODE:
161-
neetcode_data = LEETCODE_TO_NEETCODE[str(data["id"])]
162-
# Add regular video URL
163-
video_url = neetcode_data.get("video", {}).get("url", "")
164-
row.append(video_url)
165-
# Add shorts URL in new column
166-
short_url = neetcode_data.get("short", {}).get("url", "")
167-
row.append(short_url)
168-
else:
169-
row.append("") # No regular video
170-
row.append("") # No short
171-
row.append(data.get("neetcode_solution", ""))
172-
row.append(
148+
neetcode = LEETCODE_TO_NEETCODE.get(str(data["id"]), {})
149+
150+
return [
151+
data["id"],
152+
data["title"],
153+
data["difficulty"],
154+
get_url(url),
155+
date.today(),
156+
", ".join(tag["name"] for tag in data["tags"]),
157+
neetcode.get("video", {}).get("url", ""),
158+
neetcode.get("short", {}).get("url", ""),
159+
data.get("neetcode_solution", ""),
173160
"\n".join(
174-
[
175-
format_solution_link(slug, solution["id"])
176-
for solution in data["solutions"]
177-
]
178-
)
179-
)
180-
if data.get("companies"):
181-
row.append(", ".join([company["name"] for company in data["companies"]]))
182-
else:
183-
row.append("")
184-
return row
161+
format_solution_link(slug, solution["id"]) for solution in data["solutions"]
162+
),
163+
", ".join(company["name"] for company in (data.get("companies") or [])),
164+
]
185165

186166

187167
FORMAT_MAP = {
188168
"anki": format_anki,
189-
"quizlet": format_quizlet,
190169
"excel": format_excel,
191170
}
192171

193172
__all__ = [
194173
"format_solution_link",
195174
"format_anki",
196-
"format_quizlet",
197175
"format_excel",
198176
"FORMAT_MAP",
199177
"render_template",
178+
"EXCEL_COLUMNS",
179+
"DATE_COLUMN_INDEX",
180+
"DIFFICULTY_COLUMN_INDEX",
200181
]

leetcode_study_tool/outputs.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
import xlsxwriter
66

7+
from leetcode_study_tool.formatters import (
8+
DATE_COLUMN_INDEX,
9+
DIFFICULTY_COLUMN_INDEX,
10+
EXCEL_COLUMNS,
11+
)
12+
713

814
def save_string(problems: List[Union[str, None]], file: str) -> None:
915
"""
@@ -28,7 +34,7 @@ def save_excel(problems: List[Union[List[Union[str, date]], None]], file: str) -
2834
2935
Arguments
3036
---------
31-
problems : List[Union[List[str], None]]
37+
problems : List[Union[List[Union[str, date]], None]]
3238
The problems to save.
3339
file : str
3440
The file to save the problems to.
@@ -38,68 +44,56 @@ def save_excel(problems: List[Union[List[Union[str, date]], None]], file: str) -
3844
workbook = xlsxwriter.Workbook(file + ".xlsx")
3945
worksheet = workbook.add_worksheet()
4046

41-
worksheet.write(0, 0, "ID")
42-
worksheet.write(0, 1, "Title")
43-
worksheet.write(0, 2, "Difficulty")
44-
worksheet.write(
45-
0,
46-
3,
47-
"Url",
48-
)
49-
worksheet.write(0, 4, "Date Attempted")
50-
worksheet.write(0, 5, "Tags")
51-
worksheet.write(0, 6, "Neetcode")
52-
worksheet.write(0, 7, "Solutions")
53-
worksheet.write(0, 8, "Companies")
54-
55-
# Add bold formatting to the first row
5647
bold = workbook.add_format({"bold": True})
57-
worksheet.set_row(0, None, bold)
48+
for col, header in enumerate(EXCEL_COLUMNS):
49+
worksheet.write(0, col, header, bold)
50+
51+
last_row = len(problems)
52+
diff_col = DIFFICULTY_COLUMN_INDEX
53+
date_col = DATE_COLUMN_INDEX
5854

59-
# Add formatting based on the difficulty
6055
worksheet.conditional_format(
6156
1,
62-
2,
63-
len(problems),
64-
2,
57+
diff_col,
58+
last_row,
59+
diff_col,
6560
{
6661
"type": "cell",
6762
"criteria": "equal to",
68-
"value": "Easy",
63+
"value": '"Easy"',
6964
"format": workbook.add_format({"bg_color": "#00FF00"}),
7065
},
7166
)
7267
worksheet.conditional_format(
7368
1,
74-
2,
75-
len(problems),
76-
2,
69+
diff_col,
70+
last_row,
71+
diff_col,
7772
{
7873
"type": "cell",
7974
"criteria": "equal to",
80-
"value": "Medium",
75+
"value": '"Medium"',
8176
"format": workbook.add_format({"bg_color": "#FFFF00"}),
8277
},
8378
)
8479
worksheet.conditional_format(
8580
1,
86-
2,
87-
len(problems),
88-
2,
81+
diff_col,
82+
last_row,
83+
diff_col,
8984
{
9085
"type": "cell",
9186
"criteria": "equal to",
92-
"value": "Hard",
87+
"value": '"Hard"',
9388
"format": workbook.add_format({"bg_color": "#FF0000"}),
9489
},
9590
)
9691

97-
# Add gradient formatting to the date attempted row
9892
worksheet.conditional_format(
9993
1,
100-
4,
101-
len(problems),
102-
4,
94+
date_col,
95+
last_row,
96+
date_col,
10397
{
10498
"type": "3_color_scale",
10599
"min_color": "red",
@@ -112,17 +106,16 @@ def save_excel(problems: List[Union[List[Union[str, date]], None]], file: str) -
112106

113107
for i, problem in enumerate(problems, start=1):
114108
if problem:
115-
for j, line in enumerate(problem):
116-
if type(line) is date and j == 3:
117-
worksheet.write_datetime(i, j, line, date_format)
109+
for j, cell in enumerate(problem):
110+
if isinstance(cell, date):
111+
worksheet.write_datetime(i, j, cell, date_format)
118112
else:
119-
worksheet.write(i, j, line)
113+
worksheet.write(i, j, cell)
120114

121115
workbook.close()
122116

123117

124118
SAVE_MAP = {
125119
"anki": save_string,
126-
"quizlet": save_string,
127120
"excel": save_excel,
128121
}

0 commit comments

Comments
 (0)