26
26
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' # NOQA
27
27
}
28
28
29
- # TODO: define a class to put file ext and annotation
30
-
31
- FILE_EXT = {
32
- 'c++' : 'cpp' ,
33
- 'java' : 'java' ,
34
- 'python' : 'py' ,
35
- 'c' : 'c' ,
36
- 'c#' : 'cs' ,
37
- 'javascript' : 'js' ,
38
- 'ruby' : 'rb' ,
39
- 'swift' : 'swift' ,
40
- 'go' : 'go'
41
- }
42
-
43
- FILE_ANNO = {
44
- 'c++' : '//' ,
45
- 'java' : '//' ,
46
- 'python' : '#' ,
47
- 'c' : '//' ,
48
- 'c#' : '//' ,
49
- 'javascript' : '//' ,
50
- 'ruby' : '#' ,
51
- 'swift' : '//' ,
52
- 'go' : '//'
53
- }
54
-
55
29
56
30
def get_config_from_file ():
57
31
cp = configparser .ConfigParser ()
@@ -98,6 +72,23 @@ def check_and_make_dir(dirname):
98
72
os .mkdir (dirname )
99
73
100
74
75
+ class ProgLang :
76
+ def __init__ (self , language , ext , annotation ):
77
+ self .language = language
78
+ self .ext = ext
79
+ self .annotation = annotation
80
+
81
+ ProgLangList = [ProgLang ('c++' , 'cpp' , '//' ),
82
+ ProgLang ('java' , 'java' , '//' ),
83
+ ProgLang ('python' , 'py' , '#' ),
84
+ ProgLang ('c' , 'c' , '//' ),
85
+ ProgLang ('c#' , 'cs' , '//' ),
86
+ ProgLang ('javascript' , 'js' , '//' ),
87
+ ProgLang ('ruby' , 'rb' , '#' ),
88
+ ProgLang ('swift' , 'swift' , '//' ),
89
+ ProgLang ('go' , 'go' , '//' )]
90
+
91
+ ProgLangDict = dict ((item .language , item ) for item in ProgLangList )
101
92
CONFIG = get_config_from_file ()
102
93
103
94
@@ -128,7 +119,7 @@ def __init__(self):
128
119
self .num_lock = 0
129
120
130
121
self .solutions = []
131
- self .language = CONFIG ['language' ]
122
+ self .proglang = ProgLangDict [ CONFIG ['language' ] ]
132
123
133
124
self .base_url = 'https://leetcode.com'
134
125
self .session = requests .Session ()
@@ -204,7 +195,7 @@ def _load_solutions_by_language(self):
204
195
# runTime = -1 if runText == 'N/A' else int(runText[:-3])
205
196
language = i ('tr>td:nth-child(5)' ).text ().strip ()
206
197
capital_title = i ('tr>td:nth-child(2)' ).text ().strip ()
207
- if pass_status and language == self .language :
198
+ if pass_status and language == self .proglang . language :
208
199
if capital_title not in self .solutions :
209
200
self .solutions .append (capital_title )
210
201
next_page_flag = '$(".next").addClass("disabled");' in content
@@ -248,7 +239,7 @@ def write_readme(self):
248
239
(Notes: :lock: means you need to buy a book from Leetcode to unlock the problem)
249
240
250
241
| # | Title | Source Code | Article | Difficulty |
251
- |:---:|:---:|:---:|:---:|:---:|''' .format (language = self .language ,
242
+ |:---:|:---:|:---:|:---:|:---:|''' .format (language = self .proglang . language ,
252
243
tm = time .strftime ('%Y-%m-%d %H:%M:%S' , time .localtime (time .time ())),
253
244
num_solved = self .num_solved , num_total = self .num_total ,
254
245
num_lock = self .num_lock , repo = CONFIG ['repo' ])
@@ -262,9 +253,9 @@ def write_readme(self):
262
253
else :
263
254
if item .pass_status and item .capital_title in self .solutions :
264
255
dirname = '{id}-{title}' .format (id = str (item .id ).zfill (3 ), title = item .title )
265
- language = '[{language}]({repo}/blob/master/{dirname}/{title}.{ext})' .format (language = self .language , repo = CONFIG ['repo' ],
256
+ language = '[{language}]({repo}/blob/master/{dirname}/{title}.{ext})' .format (language = self .proglang . language , repo = CONFIG ['repo' ],
266
257
dirname = dirname , title = item .title ,
267
- ext = FILE_EXT [ self .language ] )
258
+ ext = self .proglang . ext )
268
259
else :
269
260
language = ''
270
261
@@ -305,10 +296,10 @@ def _generate_submissions_by_quiz(self, quiz):
305
296
yield data
306
297
307
298
def _get_quiz_and_code_by_language (self , quiz ):
308
- submissions_language = [i for i in list (self ._generate_submissions_by_quiz (quiz )) if i ['language' ].lower () == self .language ]
299
+ submissions_language = [i for i in list (self ._generate_submissions_by_quiz (quiz )) if i ['language' ].lower () == self .proglang . language ]
309
300
submissions = [i for i in submissions_language if i ['status' ]]
310
301
if not submissions :
311
- raise Exception ('No pass {language} solution in question:{title}' .format (language = self .language , title = quiz .title ))
302
+ raise Exception ('No pass {language} solution in question:{title}' .format (language = self .proglang . language , title = quiz .title ))
312
303
313
304
if len (submissions ) == 1 :
314
305
sub = submissions [0 ]
@@ -340,23 +331,23 @@ def download_quiz_code_to_dir(self, quiz):
340
331
check_and_make_dir (dirname )
341
332
342
333
path = os .path .join (HOME , dirname )
343
- fname = '{title}.{ext}' .format (title = quiz .title , ext = FILE_EXT [ self .language ] )
334
+ fname = '{title}.{ext}' .format (title = quiz .title , ext = self .proglang . ext )
344
335
filename = os .path .join (path , fname )
345
336
# quote question
346
337
# quote_question = '\n'.join(['# '+i for i in question.split('\n')])
347
338
348
339
l = []
349
340
for item in question .split ('\n ' ):
350
341
if item .strip () == '' :
351
- l .append (FILE_ANNO [ self .language ] )
342
+ l .append (self .proglang . annotation )
352
343
else :
353
- l .append ('{anno} {item}' .format (anno = FILE_ANNO [ self .language ] , item = item ))
344
+ l .append ('{anno} {item}' .format (anno = self .proglang . annotation , item = item ))
354
345
quote_question = '\n ' .join (l )
355
346
356
347
import codecs
357
348
with codecs .open (filename , 'w' , 'utf-8' ) as f :
358
- print ("begin to write file" )
359
- content = '# -*- coding:utf-8 -*-' + '\n ' * 3 if self .language == 'python' else ''
349
+ print ('write to file ->' , fname )
350
+ content = '# -*- coding:utf-8 -*-' + '\n ' * 3 if self .proglang . language == 'python' else ''
360
351
content += quote_question
361
352
content += '\n ' * 3
362
353
content += code
@@ -384,7 +375,7 @@ def _download_quiz(self, quiz):
384
375
print ('{id}-{title} not pass' .format (id = quiz .id , title = quiz .title ))
385
376
else :
386
377
if quiz .capital_title not in self .solutions :
387
- print ('{id}-{title} pass in other language not using {language}' .format (id = quiz .id , title = quiz .title , language = self .language ))
378
+ print ('{id}-{title} pass in other language not use {language}' .format (id = quiz .id , title = quiz .title , language = self . proglang .language ))
388
379
else :
389
380
print ('{id}-{title} pass' .format (id = quiz .id , title = quiz .title ))
390
381
self .download_quiz_code_to_dir (quiz )
0 commit comments