99import urllib .request
1010
1111# language IDs on judge0, see Documentation
12- languages = {"C++" : 10 , "Java" : 27 , "Python" : 34 , "C" : 4 , "Bash" : 1 }
12+ languages = {
13+ "Assembly" : 45 ,
14+ "Bash" : 46 ,
15+ "Basic" : 47 ,
16+ "C" : 50 ,
17+ "C++" : 54 ,
18+ "C#" : 51 ,
19+ "Common Lisp" : 55 ,
20+ "D" : 56 ,
21+ "Elixir" : 57 ,
22+ "Erlang" : 58 ,
23+ "Executable" : 44 ,
24+ "Fortran" : 59 ,
25+ "Go" : 60 ,
26+ "Haskell" : 61 ,
27+ "Java" : 62 ,
28+ "JavaScript" : 63 ,
29+ "Lua" : 64 ,
30+ "OCaml" : 65 ,
31+ "Octave" : 66 ,
32+ "Pascal" : 67 ,
33+ "PHP" : 68 ,
34+ "Plain Text" : 43 ,
35+ "Prolog" : 69 ,
36+ "Python2" : 70 ,
37+ "Python3" : 71 ,
38+ "Ruby" : 72 ,
39+ "Rust" : 73 ,
40+ "TypeScript" : 74 ,
41+ }
1342
1443api_params = {
15- "number_of_runs" : "1" ,
1644 "cpu_time_limit" : "2" ,
1745 "cpu_extra_time" : "0.5" ,
1846 "wall_time_limit" : "5" ,
2856FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"
2957
3058
59+ class ValueTooLargeError (Exception ):
60+ """Raised when the input value is too large"""
61+
62+
3163class code :
3264 """
3365 Args:
@@ -51,6 +83,7 @@ def __init__(
5183 self .__memory = None
5284 self .__time = None
5385 self .__stdout = None
86+ self .languages = list (languages .keys ())
5487
5588 if self .path :
5689 if not os .path .exists (source ):
@@ -69,19 +102,28 @@ def __init__(
69102 self .inp = inp
70103
71104 def __readCode (self ):
72- with open (self .source , "r" ) as myfile :
73- data = myfile .read ()
74- return data
105+ try :
106+ with open (self .source , "r" ) as program_file :
107+ data = program_file .read ()
108+ return data
109+ except FileNotFoundError as e :
110+ raise e
75111
76112 def __readExpectedOutput (self ):
77- with open (self .output , "r" ) as out :
78- data = out .read ()
79- return data
113+ try :
114+ with open (self .output , "r" ) as exp_out :
115+ data = exp_out .read ()
116+ return data
117+ except FileNotFoundError as e :
118+ raise e
80119
81120 def __readStandardInput (self ):
82- with open (self .inp , "r" ) as out :
83- data = out .read ()
84- return data
121+ try :
122+ with open (self .inp , "r" ) as standard_input :
123+ data = standard_input .read ()
124+ return data
125+ except FileNotFoundError as e :
126+ raise e
85127
86128 def __readStatus (self , token : str ):
87129 """
@@ -121,47 +163,53 @@ def __submit(self):
121163 return token
122164
123165 def getSubmissionDate (self ):
124- """
125- return submission date/time of program
126- """
166+ """Submission date/time of program"""
127167 return self .__response ["created_at" ]
128168
129169 def getExitCode (self ):
130- """
131- return exitcode of program (0 or 1)
132- """
170+ """Exitcode of program (0 or 1)"""
133171 return self .__response ["exit_code" ]
134172
135173 def getOutput (self ):
136- """
137- return standard output of program
138- """
174+ """Standard output of the program"""
139175 return self .__stdout
140176
141177 def getMemory (self ):
142- """
143- return memory used by the program
144- """
178+ """Memory used by the program"""
145179 return self .__memory
146180
147181 def getError (self ):
148- """
149- return any error message occured during execution of program
150- """
182+ """Error occured during execution of program"""
151183 if self .__response ["stderr" ] != "" :
152184 return self .__response ["stderr" ]
153185 return None
154186
155187 def getTime (self ):
156- """
157- return execution time of program
158- """
188+ """Execution time of program"""
159189 return self .__time
160190
161- def run (self ):
162- """
163- submit the source code on judge0's server & return status
164- """
191+ def setFlags (self , options : str ):
192+ """Options for the compiler (i.e. compiler flags)"""
193+ try :
194+ if len (options ) > 128 :
195+ raise ValueTooLargeError
196+ api_params ["compiler_options" ] = options
197+ except ValueTooLargeError :
198+ print ("Maximum 128 characters allowed" )
199+
200+ def setArguments (self , arguments : str ):
201+ """Command line arguments for the program"""
202+ try :
203+ if len (arguments ) > 128 :
204+ raise ValueTooLargeError
205+ api_params ["command_line_arguments" ] = arguments
206+ except ValueTooLargeError :
207+ print ("Maximum 128 characters allowed" )
208+
209+ def run (self , number_of_runs : int = 1 ):
210+ """Submit the source code on judge0's server & return status"""
211+ api_params ["number_of_runs" ] = number_of_runs
212+
165213 if os .path .exists (self .source ):
166214 if self .path :
167215 if self .inp is not None :
@@ -173,9 +221,7 @@ def run(self):
173221 self .__token = token
174222
175223 def getStatus (self ):
176- """
177- Return submission status
178- """
224+ """Submission status"""
179225 status = self .__readStatus (self .__token )
180226
181227 return status
0 commit comments