Skip to content

Commit 760a599

Browse files
committed
Make the parser and crawler more robust
- Use a wider window width in the crawler to prevent code lines getting wrapped. - Use a new method to separate the type and identifier in a variable declaration.
1 parent 9d9ecbe commit 760a599

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lchelper/crawler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_problems(contest_url: str, site: str, cookie_path: str) -> List[Problem]
128128
options.add_argument("headless")
129129
browser = webdriver.Chrome(options=options)
130130
browser.set_window_position(0, 0)
131-
browser.set_window_size(1920, 600) # a wide enough window so code does not get wrapped
131+
browser.set_window_size(3840, 600) # a wide enough window so code does not get wrapped
132132
browser.implicitly_wait(10)
133133

134134
log("Loading LeetCode contest page...")

lchelper/parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ def parse_vardef(s: str) -> Tuple[str, str]:
1717
:return: A tuple of (type, name).
1818
"""
1919
s = s.strip()
20-
type_end = next((idx for idx in range(len(s) - 1, -1, -1) if not s[idx].isidentifier()), -1)
21-
# In case there's no type (e.g., constructor), `type_end` will be -1, so `type_name` will be empty string.
22-
identifier = s[(type_end + 1):].strip()
23-
type_name = s[:(type_end + 1)].strip()
20+
ident_start = next((idx for idx in range(len(s)) if s[idx:].isidentifier()), 0)
21+
# Identifier is the longest suffix that is a valid identifier.
22+
# If the entire definition is an identifier, it's a constructor and we count it as the type name.
23+
if ident_start == 0:
24+
type_name = s
25+
identifier = ""
26+
else:
27+
type_name = s[:ident_start].strip()
28+
identifier = s[ident_start:].strip()
2429
return type_name, identifier
2530

2631

0 commit comments

Comments
 (0)