Skip to content

Commit e9a81f3

Browse files
committed
Add methods documentation
1 parent 7bbd859 commit e9a81f3

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ To use it locally you can either download it from pypi.org or you can clone this
2727

2828
## Download from pypi.org
2929

30-
Execute `py install leetcode-export` to download it and install all the needed dependencies. You might need to use `py3`
31-
depending on how python is configured in your system.
30+
Execute `pip install leetcode-export` to download it and install all the needed dependencies. You might need to use `pip3`
31+
depending on the configuration of your system.
3232

3333
### Clone the repository
3434

@@ -75,19 +75,16 @@ docker run -it -v $(pwd):/usr/app/out --rm nevermendel/leetcode-export
7575
The script accepts the following arguments:
7676

7777
```
78-
usage: leetcode-export [-h] [--username USERNAME] [--password PASSWORD]
79-
[--folder FOLDER] [--cookies COOKIES] [-v] [-vv]
78+
usage: leetcode-export [-h] [--cookies COOKIES] [--folder FOLDER] [-v] [-vv]
8079
[--problem-filename PROBLEM_FILENAME]
8180
[--submission-filename SUBMISSION_FILENAME]
8281

8382
Export LeetCode solutions
8483

8584
optional arguments:
8685
-h, --help show this help message and exit
87-
--username USERNAME Set LeetCode username
88-
--password PASSWORD Set LeetCode password
89-
--folder FOLDER Output folder
9086
--cookies COOKIES Set LeetCode cookies
87+
--folder FOLDER Output folder
9188
-v, --verbose Enable verbose logging details
9289
-vv, --extra-verbose Enable more verbose logging details
9390
--problem-filename PROBLEM_FILENAME

leetcode_export/leetcode.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def log_in(self, username: str, password: str) -> bool:
3737
return False
3838

3939
def set_cookies(self, cookies: str) -> bool:
40+
'''
41+
Log in to LeetCode using cookies
42+
:param cookies: string with cookies to set
43+
:return: bool, true if login is successful, false otherwise
44+
'''
4045
cookies_list = cookies.split(';')
4146
cookies_list = map(lambda el: el.split('='), cookies_list)
4247
for cookies in cookies_list:
@@ -49,6 +54,10 @@ def set_cookies(self, cookies: str) -> bool:
4954
return False
5055

5156
def is_user_logged(self) -> bool:
57+
'''
58+
Check if user is logged in LeetCode account
59+
:return: bool, true if user is logged in, false otherwise
60+
'''
5261
if self.user_logged and datetime.datetime.now() < self.user_logged_expiration:
5362
return True
5463
cookie_dict = self.session.cookies.get_dict()
@@ -64,6 +73,11 @@ def is_user_logged(self) -> bool:
6473
return False
6574

6675
def get_problem(self, slug: str) -> Problem:
76+
'''
77+
Get LeetCode problem info
78+
:param slug: problem identifier
79+
:return: Problem
80+
'''
6781
response = self.session.post(
6882
GRAPHQL_URL,
6983
json=question_detail_json(slug))
@@ -72,6 +86,10 @@ def get_problem(self, slug: str) -> Problem:
7286
return Problem.from_dict(problem_dict)
7387

7488
def get_submissions(self) -> Dict[str, List[Submission]]:
89+
'''
90+
Get list of submission for logged user
91+
:return: Dict[str, List[Submission]], dictionary with slug as key and submission for given problem as value
92+
'''
7593
if not self.is_user_logged():
7694
logging.warning("Trying to get user submissions while user is not logged in")
7795
return {}
@@ -86,7 +104,8 @@ def get_submissions(self) -> Dict[str, List[Submission]]:
86104
for submission_dict in response_json['submissions_dump']:
87105
submission_dict['runtime'] = submission_dict['runtime'].replace(' ', '')
88106
submission_dict['memory'] = submission_dict['memory'].replace(' ', '')
89-
submission_dict['date_formatted'] = datetime.datetime.fromtimestamp(submission_dict['timestamp']).strftime(
107+
submission_dict['date_formatted'] = datetime.datetime.fromtimestamp(
108+
submission_dict['timestamp']).strftime(
90109
'%Y-%m-%d %H.%M.%S')
91110
submission_dict['extension'] = language_to_extension(submission_dict['lang'])
92111
for key in submission_dict:

leetcode_export/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,41 @@
3030

3131

3232
def language_to_extension(language: str) -> str:
33+
'''
34+
Return extension format for given programming language
35+
:param language: programming language
36+
:return: str extension format
37+
'''
3338
return FILE_EXTENSIONS[language]
3439

3540

3641
def remove_special_characters(string: str) -> str:
42+
'''
43+
Returns a new string where without any character that is not allowed in windows filenames
44+
:param string: string to process
45+
:return: str string without special characters
46+
'''
3747
for el in SPECIAL_CHARACTERS_FILENAME:
3848
string = string.replace(el, '')
3949
return string
4050

4151

4252
def camelcase_to_snakecase(string: str) -> str:
53+
'''
54+
Returns a new string transforming a CamelCase string into snake_case
55+
:param string: CamelCase string to process
56+
:return: str snake_case string
57+
'''
4358
subbed = _regex_camelcase_to_snakecase1.sub(r'\1_\2', string)
4459
return _regex_camelcase_to_snakecase2.sub(r'\1_\2', subbed).lower()
4560

4661

4762
def dict_camelcase_to_snakecase(dictionary: Dict[str, any]) -> Dict[str, any]:
63+
'''
64+
Returns a new dictionary where keys are transformed from CamelCase into snake_case
65+
:param dictionary: original dictionary
66+
:return: Dict[str, any] new dictionary with snake_case keys
67+
'''
4868
new_dict: Dict[str, any] = {}
4969
for key in dictionary:
5070
new_dict[camelcase_to_snakecase(key)] = dictionary[key]

0 commit comments

Comments
 (0)