Skip to content

Commit c809126

Browse files
committed
Fix bugs in crawler and Python codegen
- Crawler also needs to catch "NoSuchElementException" when checking whether the problem page contains an interactive IDE. - Python codegen needs to import typing and define TreeNode before the Solution class.
1 parent 9097229 commit c809126

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

lchelper/codegen/python.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def line_comment_symbol(self) -> str:
2727
@property
2828
def template_code(self) -> str:
2929
return r"""
30+
from typing import *
31+
32+
class TreeNode:
33+
def __init__(self, x):
34+
self.val = x
35+
self.left = None
36+
self.right = None
37+
3038
# BEGIN SUBMIT
3139
3240
# BEGIN USER TEMPLATE
@@ -43,14 +51,6 @@ def template_code(self) -> str:
4351
4452
# END STATEMENT
4553
46-
from typing import List, Optional
47-
48-
class TreeNode:
49-
def __init__(self, x):
50-
self.val = x
51-
self.left = None
52-
self.right = None
53-
5454
5555
def _construct_tree(parent: List[Optional[int]], idx: int = 0) -> Optional[TreeNode]:
5656
if idx >= len(parent) or parent[idx] is None:
@@ -79,11 +79,11 @@ def test(msg: str, a, b):
7979
"string": "str",
8080
}
8181

82-
def _convert_cpp_type_to_py(self, type_name: str) -> str:
82+
def _convert_cpp_type(self, type_name: str) -> str:
8383
type_name = type_name.strip().rstrip("*&")
8484
if type_name.startswith("vector<") and type_name.endswith(">"):
8585
inner_type_name = type_name[len("vector<"):-len(">")]
86-
return f"List[{self._convert_cpp_type_to_py(inner_type_name)}]"
86+
return f"List[{self._convert_cpp_type(inner_type_name)}]"
8787
return self.TYPE_MAP.get(type_name, type_name)
8888

8989
def generate_solution_code(self, signature: Signature) -> Code:
@@ -95,15 +95,15 @@ def generate_solution_code(self, signature: Signature) -> Code:
9595
functions = [signature.function]
9696
fn_codes = []
9797
for func_sig in functions:
98-
args = ", ".join(f"{arg_name}: {self._convert_cpp_type_to_py(arg_type)}"
98+
args = ", ".join(f"{arg_name}: {self._convert_cpp_type(arg_type)}"
9999
for arg_type, arg_name in func_sig.arguments)
100100
if func_sig.name == class_name:
101101
fn_code = [
102102
f" def __init__(self, {args}):",
103103
f" pass"]
104104
else:
105105
fn_code = [
106-
f" def {func_sig.name}(self, {args}) -> {self._convert_cpp_type_to_py(func_sig.return_type)}:",
106+
f" def {func_sig.name}(self, {args}) -> {self._convert_cpp_type(func_sig.return_type)}:",
107107
f" pass"]
108108
fn_codes.append(fn_code)
109109
code = [f"class {class_name}:"] + self.list_join(fn_codes, [""])
@@ -128,7 +128,7 @@ def to_tree(parent: List[Optional[int]]) -> str:
128128
return f"_construct_tree([{', '.join('None' if x is None else str(x) for x in parent)}])"
129129

130130
def to_val(val: Any, type_name: str) -> str:
131-
if type_name == "TreeNode":
131+
if self._convert_cpp_type(type_name) == "TreeNode":
132132
return to_tree(val)
133133
return to_str(val)
134134

lchelper/crawler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List
44

55
from selenium import webdriver
6-
from selenium.common.exceptions import TimeoutException
6+
from selenium.common.exceptions import NoSuchElementException, TimeoutException
77
from selenium.webdriver.common.by import By
88
from selenium.webdriver.support import expected_conditions as Expected
99
from selenium.webdriver.support.wait import WebDriverWait
@@ -157,7 +157,7 @@ def get_problems(contest_url: str, site: str, cookie_path: str) -> List[Problem]
157157
statement_css_selector = "div.question-content"
158158
code_css_selector = "pre.CodeMirror-line"
159159
statement = browser.find_element_by_css_selector(statement_css_selector).text
160-
except TimeoutException:
160+
except (TimeoutException, NoSuchElementException):
161161
# Page after contest; statement and editor in vertically split panes.
162162
statement_css_selector = "div[data-key='description-content'] div.content__1Y2H"
163163
code_css_selector = "div.monaco-scrollable-element div.view-line"

0 commit comments

Comments
 (0)