Skip to content

Commit a355fce

Browse files
committed
code snippet file creation
1 parent 94cddf6 commit a355fce

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

leetcode/models/problem_by_id_slug.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self):
1111
super().__init__()
1212
# Instance specific variables
1313
self.browserFlag = False
14+
self.fileFlag = False
1415

1516
self.title_slug: str = None
1617
self.resuklt = None
@@ -44,6 +45,8 @@ def lookup_slug(cls, question_slug: str):
4445
def parse_args(self, args):
4546
if getattr(args, 'browser'):
4647
self.browserFlag = True
48+
if getattr(args, 'file'):
49+
self.fileFlag = True
4750

4851
def execute(self, args):
4952
self.parse_args(args)
@@ -57,27 +60,28 @@ def execute(self, args):
5760
break
5861
if not self.title_slug:
5962
raise ValueError("Invalid ID has been provided. Please try again.")
60-
elif getattr(args, 'slug'):
61-
for item in self.result.get('stat_status_pairs', []):
62-
if item['stat'].get('question__title_slug') == args.slug:
63-
self.title_slug = item['stat'].get('question__title_slug', '')
64-
break
65-
if not self.title_slug:
66-
raise ValueError("Invalid slug has been provided. Please try again.")
6763
self.show()
6864
except Exception as e:
6965
console.print(f"{e.__class__.__name__}: {e}", style=ALERT)
70-
66+
if self.fileFlag:
67+
self.create_submission_file()
7168

69+
def create_submission_file(self):
70+
question = GetQuestionDetail(self.title_slug)
71+
file_name = f"{question.question_id}.{question.title_slug}.py"
72+
with open(file_name, 'w') as file:
73+
file.write(question.code_snippet)
74+
console.print(f"File '{file_name}' has been created.")
75+
7276
def show(self):
7377
if self.browserFlag:
7478
question_info_table = QuestionInfoTable(self.title_slug)
75-
print(question_info_table)
79+
console.print(question_info_table)
7680
link = self.config.host + f'/problems/{self.title_slug}/'
77-
print(f'Link to the problem: {link}')
81+
console.print(f'Link to the problem: {link}')
7882
self.open_in_browser(link)
7983
else:
8084
question_info_table = QuestionInfoTable(self.title_slug)
81-
print(question_info_table)
85+
console.print(question_info_table)
8286
question_content = QuestionContent(self.title_slug)
83-
print(question_content)
87+
console.print(question_content)

0 commit comments

Comments
 (0)