3
3
====================================
4
4
The core module of CodeRunner
5
5
"""
6
+ import os
7
+
6
8
import requests
7
9
8
- # language IDs on judge0, see README.md
10
+ # language IDs on judge0, see Documentation
9
11
languages = {"C++" : 10 , "Java" : 27 , "Python" : 34 , "C" : 4 }
10
12
11
13
api_params = {
22
24
}
23
25
24
26
API_URL = "https://api.judge0.com/submissions/"
27
+ FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"
25
28
26
29
27
30
class Run :
28
31
"""
29
32
Args:
30
- - Source Code path
33
+ - Source Code
31
34
- Language
32
- - Expected Output File Path
33
- - Standard Input File Path
35
+ - Expected Output
36
+ - Standard Input (Optional)
37
+ - path (optional)
34
38
"""
35
39
36
- def __init__ (self , program_name : str , lang : str , output : str , inp : str = None ):
37
- """Constructor method
38
- """
39
- self . program_name = program_name
40
- self .lang = lang
40
+ def __init__ (
41
+ self , source : str , lang : str , output : str , inp : str = None , path : bool = True
42
+ ):
43
+
44
+ self .path = path
41
45
if lang not in languages :
42
46
raise ValueError (f"{ lang } is not a supported language { languages .keys ()} " )
43
-
44
- self .output = output
45
- self .inp = inp
47
+ self .lang = lang
46
48
self .language_id = languages [lang ]
47
49
self .__response = None
48
50
self .__memory = None
49
51
self .__time = None
50
52
self .__stdout = None
51
53
54
+ if self .path :
55
+ if not os .path .exists (source ):
56
+ raise OSError (f"{ source } is not a valid file path" )
57
+ self .source = source
58
+
59
+ if not os .path .exists (output ):
60
+ raise OSError (f"{ output } is not a valid file path" )
61
+ self .output = output
62
+
63
+ if inp is not None and not os .path .exists (inp ):
64
+ raise OSError (f"{ inp } is not a valid file path" )
65
+ self .inp = inp
66
+ self .source = source
67
+ self .output = output
68
+ self .inp = inp
69
+
52
70
def __readCode (self ):
53
- with open (self .program_name , "r" ) as myfile :
71
+ with open (self .source , "r" ) as myfile :
54
72
data = myfile .read ()
55
73
return data
56
74
@@ -69,7 +87,7 @@ def __readStatus(self, token: str):
69
87
Check Submission status
70
88
"""
71
89
while True :
72
- req = requests .get (API_URL + token ["token" ])
90
+ req = requests .get (API_URL + token ["token" ] + FIELDS )
73
91
self .__response = req .json ()
74
92
self .__memory = self .__response ["memory" ]
75
93
self .__time = self .__response ["time" ]
@@ -88,68 +106,61 @@ def __submit(self):
88
106
89
107
api_params ["expected_output" ] = self .output
90
108
api_params ["language_id" ] = self .language_id
91
- api_params ["source_code" ] = self .program_name
109
+ api_params ["source_code" ] = self .source
92
110
93
111
res = requests .post (API_URL , data = api_params )
94
112
token = res .json ()
95
113
return token
96
114
97
- def getStandardOutput (self ):
115
+ def getSubmissionDate (self ):
116
+ """
117
+ return submission date/time of program
118
+ """
119
+ return self .__response ["created_at" ]
120
+
121
+ def getExitCode (self ):
122
+ """
123
+ return exitcode of program (0 or 1)
98
124
"""
99
- Return the standard output of the program
125
+ return self . __response [ "exit_code" ]
100
126
101
- :param: None
102
- :rtype: String
127
+ def getOutput (self ):
128
+ """
129
+ return standard output of program
103
130
"""
104
131
return self .__stdout
105
132
106
133
def getMemory (self ):
107
134
"""
108
- Return the memory used by the program (in kilobytes)
109
-
110
- :param: None
111
- :return: Return the memory for eg 3564 KiloBytes
112
- :rtype: String
135
+ return memory used by the program
113
136
"""
114
137
return self .__memory
115
138
116
139
def getError (self ):
117
140
"""
118
- Return any error occured during program execution
119
-
120
- :param: None
121
- :rtype: String
141
+ return any error message occured during execution of program
122
142
"""
123
143
if self .__response ["stderr" ] != "" :
124
144
return self .__response ["stderr" ]
125
145
return None
126
146
127
147
def getTime (self ):
128
148
"""
129
- Return execution time used by the program
130
-
131
- :param: None
132
- :return: Returns the execution time used by Source Code for e.g 0.037 secs
133
- :rtype: String
149
+ return execution time of program
134
150
"""
135
151
return self .__time
136
152
137
153
def getStatus (self ):
138
154
"""
139
- Submits the program on Judge0's server and returns its status
140
-
141
- :param: None
142
- :return: Returns either `Accepted` or `Run Time Error`
143
- :rtype: String
155
+ submit the source code on judge0's server & return status
144
156
"""
145
- self .program_name = self .__readCode ()
146
- self .output = self .__readExpectedOutput ()
157
+ if self .path :
158
+ if self .inp is not None :
159
+ self .inp = self .__readStandardInput ()
160
+ self .source = self .__readCode ()
161
+ self .output = self .__readExpectedOutput ()
162
+
163
+ token = self .__submit ()
164
+ status = self .__readStatus (token )
147
165
148
- if self .inp is not None :
149
- self .inp = self .__readStandardInput ()
150
- token = self .__submit ()
151
- status = self .__readStatus (token )
152
- else :
153
- token = self .__submit ()
154
- status = self .__readStatus (token )
155
166
return status
0 commit comments