2
2
from datetime import datetime
3
3
from typing import Any , Optional , Union
4
4
5
- from pydantic import BaseModel
5
+ from pydantic import BaseModel , ConfigDict , Field , field_validator , UUID4
6
6
7
7
from .base_types import Iterable , LanguageAlias , Status
8
8
from .common import decode , encode
18
18
"stdout" ,
19
19
"stderr" ,
20
20
"compile_output" ,
21
- "post_execution_filesystem" ,
21
+ # "post_execution_filesystem",
22
22
}
23
23
ENCODED_FIELDS = ENCODED_REQUEST_FIELDS | ENCODED_RESPONSE_FIELDS
24
24
EXTRA_REQUEST_FIELDS = {
@@ -126,42 +126,86 @@ class Submission(BaseModel):
126
126
URL for a callback to report execution results or status.
127
127
"""
128
128
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
129
+ source_code : Optional [str ] = Field (default = None , repr = True )
130
+ language : Union [LanguageAlias , int ] = Field (
131
+ default = LanguageAlias .PYTHON ,
132
+ repr = True ,
133
+ )
134
+ additional_files : Optional [str ] = Field (default = None , repr = True )
135
+ compiler_options : Optional [str ] = Field (default = None , repr = True )
136
+ command_line_arguments : Optional [str ] = Field (default = None , repr = True )
137
+ stdin : Optional [str ] = Field (default = None , repr = True )
138
+ expected_output : Optional [str ] = Field (default = None , repr = True )
139
+ cpu_time_limit : Optional [float ] = Field (default = None , repr = True )
140
+ cpu_extra_time : Optional [float ] = Field (default = None , repr = True )
141
+ wall_time_limit : Optional [float ] = Field (default = None , repr = True )
142
+ memory_limit : Optional [float ] = Field (default = None , repr = True )
143
+ stack_limit : Optional [int ] = Field (default = None , repr = True )
144
+ max_processes_and_or_threads : Optional [int ] = Field (default = None , repr = True )
145
+ enable_per_process_and_thread_time_limit : Optional [bool ] = Field (
146
+ default = None , repr = True
147
+ )
148
+ enable_per_process_and_thread_memory_limit : Optional [bool ] = Field (
149
+ default = None , repr = True
150
+ )
151
+ max_file_size : Optional [int ] = Field (default = None , repr = True )
152
+ redirect_stderr_to_stdout : Optional [bool ] = Field (default = None , repr = True )
153
+ enable_network : Optional [bool ] = Field (default = None , repr = True )
154
+ number_of_runs : Optional [int ] = Field (default = None , repr = True )
155
+ callback_url : Optional [str ] = Field (default = None , repr = True )
149
156
150
157
# 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
158
+ stdout : Optional [str ] = Field (default = None , repr = True )
159
+ stderr : Optional [str ] = Field (default = None , repr = True )
160
+ compile_output : Optional [str ] = Field (default = None , repr = True )
161
+ message : Optional [str ] = Field (default = None , repr = True )
162
+ exit_code : Optional [int ] = Field (default = None , repr = True )
163
+ exit_signal : Optional [int ] = Field (default = None , repr = True )
164
+ status : Optional [Status ] = Field (default = None , repr = True )
165
+ created_at : Optional [datetime ] = Field (default = None , repr = True )
166
+ finished_at : Optional [datetime ] = Field (default = None , repr = True )
167
+ token : Optional [UUID4 ] = Field (default = None , repr = True )
168
+ time : Optional [float ] = Field (default = None , repr = True )
169
+ wall_time : Optional [float ] = Field (default = None , repr = True )
170
+ memory : Optional [float ] = Field (default = None , repr = True )
171
+ post_execution_filesystem : Optional [Filesystem ] = Field (default = None , repr = True )
172
+
173
+ model_config = ConfigDict (extra = "ignore" )
174
+
175
+ @field_validator (* ENCODED_FIELDS , mode = "before" )
176
+ @classmethod
177
+ def process_encoded_fields (cls , value : str ) -> Optional [str ]:
178
+ """Validate all encoded attributes."""
179
+ if value is None :
180
+ return None
181
+ else :
182
+ try :
183
+ return decode (value )
184
+ except Exception :
185
+ return value
186
+
187
+ @field_validator ("post_execution_filesystem" , mode = "before" )
188
+ @classmethod
189
+ def process_post_execution_filesystem (cls , content : str ) -> Filesystem :
190
+ """Validate post_execution_filesystem attribute."""
191
+ return Filesystem (content = content )
192
+
193
+ @field_validator ("status" , mode = "before" )
194
+ @classmethod
195
+ def process_status (cls , value : dict ) -> Status :
196
+ """Validate status attribute."""
197
+ return Status (value ["id" ])
198
+
199
+ @field_validator ("language" , mode = "before" )
200
+ @classmethod
201
+ def process_language (
202
+ cls , value : Union [LanguageAlias , dict ]
203
+ ) -> Union [LanguageAlias , int ]:
204
+ """Validate status attribute."""
205
+ if isinstance (value , dict ):
206
+ return value ["id" ]
207
+ else :
208
+ return value
165
209
166
210
def set_attributes (self , attributes : dict [str , Any ]) -> None :
167
211
"""Set Submissions attributes while taking into account different
@@ -177,7 +221,7 @@ def set_attributes(self, attributes: dict[str, Any]) -> None:
177
221
if attr in SKIP_FIELDS :
178
222
continue
179
223
180
- if attr in ENCODED_FIELDS and attr not in ( "post_execution_filesystem" ,) :
224
+ if attr in ENCODED_FIELDS :
181
225
value = decode (value ) if value else None
182
226
elif attr == "status" :
183
227
value = Status (value ["id" ])
@@ -229,10 +273,6 @@ def pre_execution_copy(self) -> "Submission":
229
273
setattr (new_submission , attr , copy .deepcopy (getattr (self , attr )))
230
274
return new_submission
231
275
232
- def __repr__ (self ) -> str :
233
- arguments = ", " .join (f"{ field } ={ getattr (self , field )!r} " for field in FIELDS )
234
- return f"{ self .__class__ .__name__ } ({ arguments } )"
235
-
236
276
def __iter__ (self ):
237
277
if self .post_execution_filesystem is None :
238
278
return iter ([])
0 commit comments