1
- import os
2
1
import time
3
2
import click
4
3
from bs4 import BeautifulSoup
10
9
import leetcode
11
10
import leetcode .auth
12
11
import requests
12
+ import os
13
+ import shutil
14
+ import glob
13
15
14
16
15
17
def non_lib_configuration (): # had to change name becasue of python-leetcode lib
@@ -61,21 +63,42 @@ def print_question_data(question):
61
63
62
64
def print_test_result (test_data , data_input ):
63
65
status_msg = test_data .get ("status_msg" )
64
- status_runtime = test_data .get ("status_runtime" )
65
- code_answer = test_data .get ("code_answer" )
66
- expected_code_answer = test_data .get ("expected_code_answer" )
67
- status_color = Colors .GREEN if status_msg == "Accepted" else Colors .RED
68
-
69
- print ("" .center (40 , "=" ))
70
- print (f"{ status_color } { status_msg } { Colors .RESET } ({ status_runtime } )" )
71
- print ("" .center (40 , "=" ))
72
- print ("input" .center (40 , "-" ))
73
- print (data_input )
74
- print ("your code output" .center (40 , "-" ))
75
- print (code_answer )
76
- print ("expected output" .center (40 , "-" ))
77
- print (expected_code_answer )
78
- print ("" .center (40 , "=" ))
66
+
67
+ if status_msg == "Accepted" :
68
+ status_runtime = test_data .get ("status_runtime" )
69
+ code_answer = test_data .get ("code_answer" )
70
+ expected_code_answer = test_data .get ("expected_code_answer" )
71
+ status_color = Colors .GREEN
72
+
73
+ print ("" .center (40 , "=" ))
74
+ print (f"{ status_color } { status_msg } { Colors .RESET } ({ status_runtime } )" )
75
+ print ("" .center (40 , "=" ))
76
+ print ("input" .center (40 , "-" ))
77
+ print (data_input )
78
+ print ("your code output" .center (40 , "-" ))
79
+ print (code_answer )
80
+ print ("expected output" .center (40 , "-" ))
81
+ print (expected_code_answer )
82
+ print ("" .center (40 , "=" ))
83
+ else :
84
+ runtime_error = test_data .get ("runtime_error" )
85
+ full_runtime_error = test_data .get ("full_runtime_error" )
86
+ status_color = Colors .RED
87
+
88
+ # Use BeautifulSoup to convert the runtime error message from HTML to plain text
89
+ soup = BeautifulSoup (full_runtime_error , "html.parser" )
90
+ plain_runtime_error = soup .get_text ()
91
+
92
+ print ("" .center (40 , "=" ))
93
+ print (f"{ status_color } { status_msg } { Colors .RESET } " )
94
+ print ("" .center (40 , "=" ))
95
+ print ("input" .center (40 , "-" ))
96
+ print (data_input )
97
+ print ("runtime error" .center (40 , "-" ))
98
+ print (runtime_error )
99
+ print ("full runtime error" .center (40 , "-" ))
100
+ print (plain_runtime_error )
101
+ print ("" .center (40 , "=" ))
79
102
80
103
81
104
def print_submission_result (submission ): # used python-leetocde library
@@ -144,7 +167,8 @@ def print_submission_result(submission): # used python-leetocde library
144
167
def initialize_leetcode_api_instance (
145
168
leetcode_session , leetcode_csrf_token
146
169
): # used python-leetocde library
147
- configuration = leetcode .Configuration ()
170
+ configuration = leetcode \
171
+ .Configuration ()
148
172
csrf_token = leetcode_csrf_token
149
173
150
174
configuration .api_key ["x-csrftoken" ] = csrf_token
@@ -153,12 +177,15 @@ def initialize_leetcode_api_instance(
153
177
configuration .api_key ["Referer" ] = "https://leetcode.com"
154
178
configuration .debug = False
155
179
156
- api_instance = leetcode .DefaultApi (leetcode .ApiClient (configuration ))
180
+ api_instance = leetcode \
181
+ .DefaultApi (leetcode
182
+ .ApiClient (configuration ))
157
183
return api_instance
158
184
159
185
160
186
def interpret_solution (title_slug , payload , api_instance ):
161
- test_submission = leetcode .TestSubmission (
187
+ test_submission = leetcode \
188
+ .TestSubmission (
162
189
data_input = payload ["data_input" ],
163
190
typed_code = payload ["typed_code" ],
164
191
question_id = payload ["question_id" ],
@@ -199,7 +226,6 @@ def submit_solution(
199
226
200
227
def process_test_file (leetcode_api_instance , api_instance , test ):
201
228
title_slug , lang_name = title_and_file_extension (test )
202
- print (lang_name )
203
229
question_detail_data = get_question_detail (api_instance , title_slug )
204
230
if question_detail_data :
205
231
question_id = question_detail_data .get ("questionId" )
@@ -477,6 +503,8 @@ def write_code_snippet_to_file(question_detail_data, lang, title_slug):
477
503
if code :
478
504
lang_extension = LANG_EXTENSIONS .get (lang )
479
505
if lang_extension :
506
+ if not os .path .exists ("code_editor" ):
507
+ os .makedirs ("code_editor" )
480
508
file_path = os .path .join (
481
509
"code_editor" ,
482
510
f"{ question_detail_data ['questionFrontendId' ]} _{ title_slug } .{ lang_extension } " ,
@@ -540,30 +568,56 @@ def title_and_file_extension(file):
540
568
541
569
def print_help_usage ():
542
570
help_message = """
571
+ IMPORTANT: python lc.py --lib
572
+
543
573
Usage:
544
- python leetcode .py --config
545
- python leetcode .py --config --user-lang <language>
546
- python leetcode .py --question/-q <question_id_or_title>
547
- python leetcode .py --solve <question_id_or_title>
548
- python leetcode .py --test/-t <filename>
549
- python leetcode .py --submit/-sb <filename>
574
+ python lc .py --config
575
+ python lc .py --config --user-lang <language>
576
+ python lc .py --question/-q <question_id_or_title>
577
+ python lc .py --solve <question_id_or_title>
578
+ python lc .py --test/-t <filename>
579
+ python lc .py --submit/-sb <filename>
550
580
551
581
Examples:
552
- python leetcode .py --config --user-lang=python3
553
- python leetcode .py --question 1
554
- python leetcode .py --question add-two-numbers
555
- python leetcode .py --question 10:20
556
- python leetcode .py --solve/-s add-two-numbers
557
- python leetcode .py --solve 1
558
- python leetcode .py --test test_file.py
559
- python leetcode .py --submit submit_file.py
582
+ python lc .py --config --user-lang=python3
583
+ python lc .py --question 1
584
+ python lc .py --question add-two-numbers
585
+ python lc .py --question 10:20
586
+ python lc .py --solve/-s add-two-numbers
587
+ python lc .py --solve 1
588
+ python lc .py --test test_file.py
589
+ python lc .py --submit submit_file.py
560
590
561
591
For any issues or feature requests, please visit:
562
592
https://github.com/hrdkmishra/leetcode.py
563
593
"""
564
594
print (help_message )
565
595
566
596
597
+ def replace_files ():
598
+ source_dir = "custom_lib_file/"
599
+ destination_dir = "venv/Lib/site-packages/leetcode/models/"
600
+ file_paths = glob .glob (os .path .join (source_dir , "*" ))
601
+
602
+ if not file_paths :
603
+ print (f"No files found in the source directory '{ source_dir } '." )
604
+ return
605
+
606
+ for src_path in file_paths :
607
+ filename = os .path .basename (src_path )
608
+ dest_path = os .path .join (destination_dir , filename )
609
+
610
+ if os .path .exists (dest_path ):
611
+ try :
612
+ os .remove (dest_path )
613
+ shutil .copy (src_path , dest_path )
614
+ print (f"File '{ src_path } ' replaced successfully." )
615
+ except Exception as e :
616
+ print (f"An error occurred while replacing the file: { e } " )
617
+ else :
618
+ print (f"Destination path '{ dest_path } ' does not exist." )
619
+
620
+
567
621
@click .command ()
568
622
@click .option ("--config" , is_flag = True , help = "Enter credentials and save to config" )
569
623
@click .option (
@@ -572,6 +626,9 @@ def print_help_usage():
572
626
default = "" ,
573
627
help = "Set user preferred language (e.g., python3)" ,
574
628
)
629
+ @click .option (
630
+ "--lib" , is_flag = True , default = False , help = "Show usage information"
631
+ )
575
632
@click .option (
576
633
"--question" ,
577
634
"-q" ,
@@ -603,13 +660,16 @@ def print_help_usage():
603
660
@click .option (
604
661
"--help" , "-h" , is_flag = True , default = False , help = "Show usage information"
605
662
)
606
- def main (config , user_lang , question , solve , test , submit , help ):
663
+ def main (config , user_lang , question , solve , test , submit , help , lib ):
664
+ if lib :
665
+ replace_files ()
666
+ exit ()
607
667
if config :
608
668
leetcode_session , csrf_token = non_lib_configuration ()
609
669
# If the --user-lang option is provided, save it to config
610
670
if user_lang :
611
671
save_user_data_to_config (user_lang )
612
- exit ()
672
+ exit ()
613
673
else :
614
674
leetcode_session , csrf_token = load_credentials_from_config ()
615
675
@@ -647,4 +707,4 @@ def main(config, user_lang, question, solve, test, submit, help):
647
707
648
708
649
709
if __name__ == "__main__" :
650
- main () # main
710
+ main ()
0 commit comments