8000 responses visual update · Coderbeep/LeetCode-CLI@e33c745 · GitHub
[go: up one dir, main page]

Skip to content

Commit e33c745

Browse files
committed
responses visual update
1 parent 587ac6f commit e33c745

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

leetcode/models/submit.py

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(self):
1212
super().__init__()
1313
self.title_slug = None
1414
self.path = None
15+
self.command = None
1516
self.runcode = None
1617
self.submission_id = None
1718

@@ -34,19 +35,30 @@ def runcode_check_url(self):
3435
return f"https://leetcode.com/submissions/detail/{self.runcode}/check/"
3536

3637
def parse_args(self, args):
37-
self.title_slug = args.question_slug
38+
import os
39+
self.command = args.command
3840
self.path = args.path
3941

42+
43+
# Get the info from filename : 1234.title-slug.py
44+
filename = os.path.basename(self.path)
45+
self.title_slug = filename.split('.')[1]
46+
47+
4048
# check if such slug exists
4149
ProblemInfo.lookup_slug(self.title_slug)
4250

4351
def _execute(self, args):
4452
try:
45-
with Loader('Uploading submission...', ''):
46-
self.parse_args(args)
47-
self.execute_submission(self.title_slug, self.path)
48-
49-
self.show_submission_info(self.submit_response)
53+
self.parse_args(args)
54+
if self.command == 'submit':
55+
with Loader('Uploading submission...', ''):
56+
submit_response = self.execute_submission(self.title_slug, self.path)
57+
self.show_submission_info(submit_response)
58+
elif self.command == 'check':
59+
with Loader('Checking submission...', ''):
60+
check_response = self.execute_check(self.title_slug, self.path)
61+
self.show_check_info(check_response)
5062
except Exception as e:
5163
console.print(f"{e.__class__.__name__}: {e}", style=ALERT)
5264

@@ -81,16 +93,19 @@ def execute_check(self, title_slug, filename):
8193
response = requests.get(url=self.runcode_check_url,
8294
headers=self.config.headers,
8395
cookies=self.config.cookies)
84-
self.show_check_info(response.json())
96+
return response.json()
97+
8598

8699
def show_check_info(self, response):
87100
if response.get('run_success'):
88-
print(f"Runtime: {response.get('status_runtime')}")
89-
print(f"Answer: {response.get('correct_answer')}")
90-
print(f"Expected: {response.get('expected_code_answer')}")
91-
print(f"Got answer: {response.get('code_answer')}")
101+
console.print("\n[bold green]✓ Check Passed[/bold green]\n")
102+
console.print(f"[bold]Runtime:[/bold] {response.get('status_runtime')}")
103+
console.print(f"[bold]Answer:[/bold] {response.get('correct_answer')}")
104+
console.print(f"[bold]Expected:[/bold] {response.get('expected_code_answer')}")
105+
console.print(f"[bold]Got answer:[/bold] {response.get('code_answer')}")
92106
else:
93-
print(f"Exception: {response.get('status_msg')}")
107+
console.print("\n[bold red]✗ Check Failed[/bold red]\n")
108+
console.print(f"[bold]Exception:[/bold] {response.get('status_msg')}")
94109

95110
def execute_submission(self, title_slug, filename):
96111
# In similar way execute clicking submit button on the leetcode website
@@ -114,24 +129,24 @@ def execute_submission(self, title_slug, filename):
114129
response = requests.get(url=self.submit_check_url,
115130
headers=self.config.headers,
116131
cookies=self.config.cookies)
117-
self.submit_response = response.json()
132+
return response.json()
118133

119134
def show_submission_info(self, response):
120135
if response.get('run_success'):
121136
status_msg = response.get('status_msg')
122137
if status_msg == 'Accepted': # If the solution is accepted
123-
print(f"Status: [bold green]{status_msg}[/bold green] :tada:")
124-
print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} test cases -> {response.get('status_runtime')}")
138+
console.print(f"\n[bold green]✓ Submission Passed[/bold green] :tada:")
139+
console.print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} test cases in {response.get('status_runtime')}")
125140

126141
perc_evalutaion = SubmitEvaluation(f"{response.get('runtime_percentile'):.2f}", f"{response.get('memory_percentile'):.2f}")
127-
print(perc_evalutaion)
142+
console.print(perc_evalutaion)
128143
elif status_msg == 'Wrong Answer': # If the solution is wrong
129-
print(f"Status: [bold red]{status_msg}[/bold red] :tada:")
130-
print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} testcases")
144+
console.print(f"\n[bold red]✗ Submission Failed[/bold red] :tada:")
145+
console.print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} testcases")
131146
else:
132147
if response.get('status_msg') == 'Time Limit Exceeded':
133-
print(f"Status: [bold red]{response.get('status_msg')}[/bold red] :alarm_clock:")
134-
print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} testcases")
148+
console.print(f"\n[bold red]✗ Submission Failed[/bold red] :alarm_clock:")
149+
console.print(f"Passed {response.get('total_correct')}/{response.get('total_testcases')} testcases")
135150
elif response.get('status_msg') == 'Runtime Error':
136-
print(f"Status: [bold red]{response.get('status_msg')}[/bold red]")
137-
print(f"{response.get('runtime_error')}")
151+
console.print(f"\n[bold red]✗ Submission Failed[/bold red]")
152+
console.print(f"{response.get('runtime_error')}")

0 commit comments

Comments
 (0)
0