2
2
from datetime import datetime
3
3
from typing import Any , Optional , Union
4
4
5
- from judge0 . filesystem import Filesystem
5
+ from pydantic import BaseModel
6
6
7
7
from .base_types import Iterable , LanguageAlias , Status
8
8
from .common import decode , encode
9
+ from .filesystem import Filesystem
9
10
10
11
ENCODED_REQUEST_FIELDS = {
11
12
"source_code" ,
65
66
Submissions = Iterable ["Submission" ]
66
67
67
68
68
- class Submission :
69
+ class Submission ( BaseModel ) :
69
70
"""
70
71
Stores a representation of a Submission to/from Judge0.
71
72
@@ -125,72 +126,42 @@ class Submission:
125
126
URL for a callback to report execution results or status.
126
127
"""
127
128
128
- def __init__ (
129
- self ,
130
- * ,
131
- source_code : Optional [str ] = None ,
132
- language : Union [LanguageAlias , int ] = LanguageAlias .PYTHON ,
133
- additional_files : Optional [str ] = None ,
134
- compiler_options : Optional [str ] = None ,
135
- command_line_arguments : Optional [str ] = None ,
136
- stdin : Optional [str ] = None ,
137
- expected_output : Optional [str ] = None ,
138
- cpu_time_limit : Optional [float ] = None ,
139
- cpu_extra_time : Optional [float ] = None ,
140
- wall_time_limit : Optional [float ] = None ,
141
- memory_limit : Optional [float ] = None ,
142
- stack_limit : Optional [int ] = None ,
143
- max_processes_and_or_threads : Optional [int ] = None ,
144
- enable_per_process_and_thread_time_limit : Optional [bool ] = None ,
145
- enable_per_process_and_thread_memory_limit : Optional [bool ] = None ,
146
- max_file_size : Optional [int ] = None ,
147
- redirect_stderr_to_stdout : Optional [bool ] = None ,
148
- enable_network : Optional [bool ] = None ,
149
- number_of_runs : Optional [int ] = None ,
150
- callback_url : Optional [str ] = None ,
151
- ):
152
- self .source_code = source_code
153
- self .language = language
154
- self .additional_files = additional_files
155
-
156
- # Extra pre-execution submission attributes.
157
- self .compiler_options = compiler_options
158
- self .command_line_arguments = command_line_arguments
159
- self .stdin = stdin
160
- self .expected_output = expected_output
161
- self .cpu_time_limit = cpu_time_limit
162
- self .cpu_extra_time = cpu_extra_time
163
- self .wall_time_limit = wall_time_limit
164
- self .memory_limit = memory_limit
165
- self .stack_limit = stack_limit
166
- self .max_processes_and_or_threads = max_processes_and_or_threads
167
- self .enable_per_process_and_thread_time_limit = (
168
- enable_per_process_and_thread_time_limit
169
- )
170
- self .enable_per_process_and_thread_memory_limit = (
171
- enable_per_process_and_thread_memory_limit
172
- )
173
- self .max_file_size = max_file_size
174
- self .redirect_stderr_to_stdout = redirect_stderr_to_stdout
175
- self .enable_network = enable_network
176
- self .number_of_runs = number_of_runs
177
- self .callback_url = callback_url
178
-
179
- # Post-execution submission attributes.
180
- self .stdout : Optional [str ] = None
181
- self .stderr : Optional [str ] = None
182
- self .compile_output : Optional [str ] = None
183
- self .message : Optional [str ] = None
184
- self .exit_code : Optional [int ] = None
185
- self .exit_signal : Optional [int ] = None
186
- self .status : Optional [Status ] = None
187
- self .created_at : Optional [datetime ] = None
188
- self .finished_at : Optional [datetime ] = None
189
- self .token : str = ""
190
- self .time : Optional [float ] = None
191
- self .wall_time : Optional [float ] = None
192
- self .memory : Optional [float ] = None
193
- self .post_execution_filesystem : Optional [Filesystem ] = None
129
+ source_code : Optional [str ] = None
130
+ language : Union [LanguageAlias , int ] = LanguageAlias .PYTHON
131
+ additional_files : Optional [str ] = None
132
+ compiler_options : Optional [str ] = None
133
+ command_line_arguments : Optional [str ] = None
134
+ stdin : Optional [str ] = None
135
+ expected_output : Optional [str ] = None
136
+ cpu_time_limit : Optional [float ] = None
137
+ cpu_extra_time : Optional [float ] = None
138
+ wall_time_limit : Optional [float ] = None
139
+ memory_limit : Optional [float ] = None
140
+ stack_limit : Optional [int ] = None
141
+ max_processes_and_or_threads : Optional [int ] = None
142
+ enable_per_process_and_thread_time_limit : Optional [bool ] = None
143
+ enable_per_process_and_thread_memory_limit : Optional [bool ] = None
144
+ max_file_size : Optional [int ] = None
145
+ redirect_stderr_to_stdout : Optional [bool ] = None
146
+ enable_network : Optional [bool ] = None
147
+ number_of_runs : Optional [int ] = None
148
+ callback_url : Optional [str ] = None
149
+
150
+ # Post-execution submission attributes.
151
+ stdout : Optional [str ] = None
152
+ stderr : Optional [str ] = None
153
+ compile_output : Optional [str ] = None
154
+ message : Optional [str ] = None
155
+ exit_code : Optional [int ] = None
156
+ exit_signal : Optional [int ] = None
157
+ status : Optional [Status ] = None
158
+ created_at : Optional [datetime ] = None
159
+ finished_at : Optional [datetime ] = None
160
+ token : str = ""
161
+ time : Optional [float ] = None
162
+ wall_time : Optional [float ] = None
163
+ memory : Optional [float ] = None
164
+ post_execution_filesystem : Optional [Filesystem ] = None
194
165
195
166
def set_attributes (self , attributes : dict [str , Any ]) -> None :
196
167
"""Set Submissions attributes while taking into account different
@@ -215,7 +186,7 @@ def set_attributes(self, attributes: dict[str, Any]) -> None:
215
186
elif attr in FLOATING_POINT_FIELDS and value is not None :
216
187
value = float (value )
217
188
elif attr == "post_execution_filesystem" :
218
- value = Filesystem (value )
189
+ value = Filesystem (content = value )
219
190
220
191
setattr (self , attr , value )
221
192
@@ -240,43 +211,6 @@ def as_body(self, client: "Client") -> dict:
240
211
241
212
return body
242
213
243
- def to_dict (self ) -> dict :
244
- encoded_request_fields = {
245
- field_name : encode (getattr (self , field_name ))
246
- for field_name in ENCODED_REQUEST_FIELDS
247
- if getattr (self , field_name ) is not None
248
- }
249
- extra_request_fields = {
250
- field_name : getattr (self , field_name )
251
- for field_name in EXTRA_REQUEST_FIELDS
252
- if getattr (self , field_name ) is not None
253
- }
254
- encoded_response_fields = {
255
- field_name : encode (getattr (self , field_name ))
256
- for field_name in ENCODED_RESPONSE_FIELDS
257
- if getattr (self , field_name ) is not None
258
- }
259
- extra_response_fields = {
260
- field_name : getattr (self , field_name )
261
- for field_name in EXTRA_RESPONSE_FIELDS
262
- if getattr (self , field_name ) is not None
263
- }
264
-
265
- submission_dict = (
266
- encoded_request_fields
267
- | extra_request_fields
268
- | encoded_response_fields
269
- | extra_response_fields
270
- )
271
-
272
- return submission_dict
273
-
274
- @staticmethod
275
- def from_dict (submission_dict ) -> "Submission" :
276
- submission = Submission ()
277
- submission .set_attributes (submission_dict )
278
- return submission
279
-
280
214
def is_done (self ) -> bool :
281
215
"""Check if submission is finished processing.
282
216
0 commit comments