@@ -31,6 +31,7 @@ def from_dict(cls, data):
3131
3232
3333class SubmissionList (QueryTemplate ):
34+ """ A class representing a list of LeetCode submissions for given problem. """
3435 def __init__ (self ):
3536 super ().__init__ ()
3637 # Instance specific variables
@@ -39,27 +40,22 @@ def __init__(self):
3940 self .submission_download = False
4041
4142 self .graphql_query = None
42- self .result = None
43+ self .data = None
4344 self .params = {'offset' : 0 , 'limit' : 20 , 'lastKey' : None , 'questionSlug' : None }
4445
45- def parse_args (self , args ):
46- self .question_id = args .id
47- self .params ['questionSlug' ] = ProblemInfo .get_title_slug (self .question_id )
48-
49- if getattr (args , 'show' ):
50- self .show_terminal = True
51-
52- if getattr (args , 'download' ):
53- self .submission_download = True
46+ def _execute (self , args ):
47+ """ Executes the query with the given arguments and displays the result.
5448
55- def execute (self , args ):
49+ Args:
50+ args (argparse.Namespace): The arguments passed to the query.
51+ """
5652 try :
5753 with Loader ('Fetching submission list...' , '' ):
58- self .parse_args (args )
54+ self .__parse_args (args )
5955 self .graphql_query = GraphQLQuery (self .query , self .params )
60- self .result = self .leet_API .post_query (self .graphql_query )
61- self .result = QuestionSubmisstionList .from_dict (self .result ['data' ])
62- if not self .result .submissions :
56+ self .data = self .leet_API .post_query (self .graphql_query )
57+ self .data = QuestionSubmisstionList .from_dict (self .data ['data' ])
58+ if not self .data .submissions :
6359 raise ValueError ("Apparently you don't have any submissions for this problem." )
6460 self .show ()
6561
@@ -72,29 +68,39 @@ def execute(self, args):
7268 console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
7369
7470 def show (self ):
71+ """ Displays the query result in a table. """
72+
7573 table = LeetTable ()
76- table .add_column ('ID' )
77- # table.add_column('Title')
74+ table .add_column ('Submission ID' )
7875 table .add_column ('Status' )
7976 table .add_column ('Runtime' )
8077 table .add_column ('Memory' )
8178 table .add_column ('Language' )
8279
83- submissions = self .result .submissions
80+ submissions = self .data .submissions
8481 table .title = f"Submissions for problem [blue]{ submissions [0 ].title } "
8582 for x in submissions :
8683 table .add_row (x .id , x .statusDisplay , x .runtime , x .memory , x .langName )
8784 console .print (table )
8885
89-
9086 @staticmethod
9187 def fetch_accepted (submissions ):
88+ """ Fetches the latest accepted submission from the list of submissions.
89+
90+ Args:
91+ submissions (List[Submission]): The list of submissions.
92+
93+ Returns:
94+ Submission: The latest accepted submission. If no accepted submissions are found, None is returned."""
9295 return next ((x for x in submissions if x .statusDisplay == 'Accepted' ), None )
9396
9497 def show_code (self ):
98+ """ Displays the code of the latest accepted submission.
99+
100+ If no accepted submissions are found, an exception is raised. """
95101 try :
96102 with Loader ('Fetching latest accepted code...' , '' ):
97- acc_submission = self .fetch_accepted (self .result .submissions )
103+ acc_submission = self .fetch_accepted (self .data .submissions )
98104
99105 if not acc_submission :
100106 raise ValueError ("No accepted submissions found." )
@@ -114,9 +120,13 @@ def show_code(self):
114120 console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
115121
116122 def download_submission (self ):
123+ """ Downloads the code of the latest accepted submission.
124+
125+ If no accepted submissions are found, an exception is raised.
126+ The code is saved in a file named as follows: [titleSlug].[submissionId].py """
117127 try :
118128 with Loader ('Downloading latest accepted code...' , '' ):
119- acc_submission = self .fetch_accepted (self .result .submissions )
129+ acc_submission = self .fetch_accepted (self .data .submissions )
120130
121131 if not acc_submission :
122132 raise ValueError ("No accepted submissions found." )
@@ -134,6 +144,21 @@ def download_submission(self):
134144 console .print (f"✅ File saved as '{ file_name } '" )
135145 except Exception as e :
136146 console .print (f"{ e .__class__ .__name__ } : { e } " , style = ALERT )
147+
148+ def __parse_args (self , args ):
149+ """ Parses the arguments passed to the query.
150+
151+ Args:
152+ args (argparse.Namespace): The arguments passed to the query.
153+ """
154+ self .question_id = args .id
155+ self .params ['questionSlug' ] = ProblemInfo .get_title_slug (self .question_id )
156+
157+ if getattr (args , 'show' ):
158+ self .show_terminal = True
159+
160+ if getattr (args , 'download' ):
161+ self .submission_download = True
137162
138163
139164
0 commit comments