Skip to content

Commit 4d80f4d

Browse files
committed
leet problem command amendment
1 parent f335877 commit 4d80f4d

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

leetcode/main.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@
1515
# TODO: add a command to show the solution in the terminal
1616
# TODO: add a command to show the solution in the browser
1717
# TODO: problem with import in synced code or code to submit
18-
# TODO: random problem selector (from not accepted problems)
1918
# TODO: check the changes in question_content and apply them to the code in other files
20-
# TODO: use config without having to have a session
2119
# TODO: check all commands for errors
2220
# TODO: README - search - download - check - submit
23-
# TODO: leet problem -f displays erors
24-
# TODO: submit with only id (or filename)
2521

2622
def positive_integer(value):
2723
try:
@@ -62,6 +58,7 @@ def main():
6258
problem_parser.add_argument('id', type=positive_integer, help='Problem ID of the problem', default=0, nargs='?')
6359
problem_parser.add_argument('-b', '--browser', action='store_true', help='Open the page in browser.')
6460
problem_parser.add_argument('-f', '--file', action='store_true', help='Create a file with the problem content.')
61+
problem_parser.add_argument('-c', '--contents', action='store_true', help='Display contents of the question in the terminal.')
6562

6663

6764
today_problem_parser = subparsers.add_parser('today', help="Display today's problem.")
@@ -78,17 +75,18 @@ def main():
7875
submission_parser.set_defaults(func=SubmissionList)
7976

8077
submission_parser = subparsers.add_parser('submit', help='Submit code answer')
81-
submission_parser.add_argument('question_slug', type=str, help="Title slug of the question")
8278
submission_parser.add_argument('path', type=str, help='Path to the file with code answer')
8379
submission_parser.set_defaults(func=SendSubmission)
84-
85-
8680

81+
submission_parser = subparsers.add_parser('check', help='Check code answer on example test')
82+
submission_parser.add_argument('path', type=str, help='Path to the file with code answer')
83+
submission_parser.set_defaults(func=SendSubmission)
84+
8785
args = parser.parse_args()
8886

8987
if hasattr(args, 'func'):
9088
command_instance = args.func()
91-
command_instance._execute(args) # call the private method __execute
89+
command_instance._execute(args)
9290
else:
9391
print("Unknown command. Use 'leet --help' for available commands.")
9492

leetcode/models/problem_by_id_slug.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ def __init__(self):
1717
# Instance specific variables
1818
self.browserFlag = False
1919
self.fileFlag = False
20+
self.randomFlag = False
21+
self.contentFlag = False
2022

2123
self._question_id: int = None
2224
self._title_slug: str = None
@@ -107,8 +109,7 @@ def _execute(self, args):
107109
question_content = QuestionContent(problem.titleSlug)
108110
console.print(question_info_table)
109111
console.print(question_content)
110-
111-
else:
112+
elif getattr(args, 'id') != 0:
112113
try:
113114
with Loader('Fetching problem info...', ''):
114115
self.data = self.leet_api.get_request(self.API_URL)
@@ -124,6 +125,8 @@ def _execute(self, args):
124125
console.print(f"{e.__class__.__name__}: {e}", style=ALERT)
125126
if self.fileFlag:
126127
self.create_submission_file(self.title_slug)
128+
else:
129+
console.print("Invalid ID has been provided. Please try again.", style=ALERT)
127130

128131
@classmethod
129132
def create_submission_file(cls, title_slug: str = None) -> None:
@@ -133,9 +136,11 @@ def create_submission_file(cls, title_slug: str = None) -> None:
133136
Args:
134137
title_slug (str): The title slug of the problem.
135138
"""
139+
watermark_info = '# This file was created by pyleetcode-cli software.\n# Do NOT modify the name of the file.\n\n'
136140
question = GetQuestionDetail(title_slug)
137141
file_name = f"{question.question_id}.{question.title_slug}.py"
138142
with open(file_name, 'w') as file:
143+
file.write(watermark_info)
139144
file.write(question.code_snippet)
140145
console.print(f"File '{file_name}' has been created.")
141146

@@ -146,12 +151,15 @@ def show(self):
146151
link = self.config.host + f'/problems/{self.title_slug}/'
147152
console.print(f'Link to the problem: {link}')
148153
self.open_in_browser(link)
149-
else:
154+
elif self.contentFlag:
150155
question_info_table = QuestionInfoTable(self.title_slug)
151156
console.print(question_info_table)
152157
question_content = QuestionContent(self.title_slug)
153158
console.print(question_content)
154-
159+
else:
160+
question_info_table = QuestionInfoTable(self.title_slug)
161+
console.print(question_info_table)
162+
155163
def __parse_args(self, args) -> None:
156164
""" Parses the arguments passed to the query.
157165
@@ -161,6 +169,10 @@ def __parse_args(self, args) -> None:
161169
self.browserFlag = True
162170
if getattr(args, 'file'):
163171
self.fileFlag = True
172+
if getattr(args, 'random'):
173+
self.randomFlag = True
174+
if getattr(args, 'contents'):
175+
self.contentFlag = True
164176

165177
@property
166178
def data(self):

0 commit comments

Comments
 (0)