Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/leetscrape/generate_code_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class GenerateCodeStub:
"""

def __init__(
self,
titleSlug: str | None = None,
qid: int | None = None,
self,
titleSlug: str | None = None,
qid: int | None = None,
):
self.all_questions_stub_id = (
GetQuestion.fetch_all_questions_id_and_stub().reset_index().set_index("QID")
Expand All @@ -42,8 +42,8 @@ def __init__(
if self.titleSlug in self.all_questions_stub_id.titleSlug.tolist():
if self.qid is not None:
if (
self.titleSlug
!= self.all_questions_stub_id.loc[self.qid].titleSlug
self.titleSlug
!= self.all_questions_stub_id.loc[self.qid].titleSlug
):
raise ValueError(
f"Both titleSlug and qid were passed but they do not match.\n"
Expand All @@ -52,7 +52,7 @@ def __init__(
else:
self.qid = self.all_questions_stub_id[
self.all_questions_stub_id["titleSlug"] == self.titleSlug
].index[0]
].index[0]
else:
raise ValueError("There is no question with the passed titleSlug.")
print(f"Generating code stub for {self.qid}. {self.titleSlug}")
Expand All @@ -79,7 +79,7 @@ def generate(self, testing=False, directory: str = ".") -> None:

if not self.data.isPaidOnly:
with open(
f"{directory}/test_{self.filename}", "w", encoding="utf-8"
f"{directory}/test_{self.filename}", "w", encoding="utf-8"
) as f:
f.write(
format_str(test_to_write, mode=FileMode())
Expand Down Expand Up @@ -142,7 +142,7 @@ def _create_code_file(self) -> str:
return text_to_write

def _extract_codeblocks_in_problem_statement(
self, problem_statement: str
self, problem_statement: str
) -> list[str]:
"""Extract the code blocks from the given problem statement string. These codeblocks contain the basic test cases provided by Leetcode.

Expand Down Expand Up @@ -193,7 +193,13 @@ def _get_parameters(self, code_blocks: list[str]) -> tuple[str, str]:
.replace("\n", "")
)["Output"]
parameters.append(parameter_dict)
output_string = ", ".join(list(parameters[0].keys()))

# Some questions do not have any code stub, just add error handling code to deal with this
try:
output_string = ", ".join(list(parameters[0].keys()))
except IndexError:
output_string = ""

input_string = ", ".join(
f"({test_case})"
for test_case in [
Expand Down
22 changes: 19 additions & 3 deletions src/leetscrape/question.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
from json import JSONDecodeError
import os
import time
import warnings

Expand All @@ -12,6 +14,14 @@
# Leetcode's graphql api endpoint
BASE_URL = "https://leetcode.com/graphql"

# JSON response file from https://leetcode.com/api/problems/all/
# Get the directory of the current script
script_dir = os.path.dirname(__file__)

# Construct the full path to the JSON file
json_file_path = os.path.join(script_dir, './response.json')



class GetQuestion:
"""
Expand All @@ -27,9 +37,15 @@ def __init__(self, titleSlug: str):

@staticmethod
def fetch_all_questions_id_and_stub():
req = requests.get(
"https://leetcode.com/api/problems/all/", headers=HEADERS
).json()

try:
req = requests.get(
"https://leetcode.com/api/problems/all/", headers=HEADERS
).json()
except JSONDecodeError: # Use locally-saved json for now to handle error
with open(json_file_path, "r") as file:
req = json.load(file)

question_data = pd.json_normalize(req["stat_status_pairs"]).rename(
columns={
"stat.frontend_question_id": "QID",
Expand Down
1 change: 1 addition & 0 deletions src/leetscrape/response.json

Large diffs are not rendered by default.