33====================================
44The core module of CodeRunner
55"""
6+ import os
7+
68import requests
79
8- # language IDs on judge0, see README.md
10+ # language IDs on judge0, see Documentation
911languages = {"C++" : 10 , "Java" : 27 , "Python" : 34 , "C" : 4 }
1012
1113api_params = {
2224}
2325
2426API_URL = "https://api.judge0.com/submissions/"
27+ FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"
2528
2629
2730class Run :
2831 """
2932 Args:
30- - Source Code path
33+ - Source Code
3134 - Language
32- - Expected Output File Path
33- - Standard Input File Path
35+ - Expected Output
36+ - Standard Input (Optional)
37+ - path (optional)
3438 """
3539
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
4145 if lang not in languages :
4246 raise ValueError (f"{ lang } is not a supported language { languages .keys ()} " )
43-
44- self .output = output
45- self .inp = inp
47+ self .lang = lang
4648 self .language_id = languages [lang ]
4749 self .__response = None
4850 self .__memory = None
4951 self .__time = None
5052 self .__stdout = None
5153
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+
5270 def __readCode (self ):
53- with open (self .program_name , "r" ) as myfile :
71+ with open (self .source , "r" ) as myfile :
5472 data = myfile .read ()
5573 return data
5674
@@ -69,7 +87,7 @@ def __readStatus(self, token: str):
6987 Check Submission status
7088 """
7189 while True :
72- req = requests .get (API_URL + token ["token" ])
90+ req = requests .get (API_URL + token ["token" ] + FIELDS )
7391 self .__response = req .json ()
7492 self .__memory = self .__response ["memory" ]
7593 self .__time = self .__response ["time" ]
@@ -88,68 +106,61 @@ def __submit(self):
88106
89107 api_params ["expected_output" ] = self .output
90108 api_params ["language_id" ] = self .language_id
91- api_params ["source_code" ] = self .program_name
109+ api_params ["source_code" ] = self .source
92110
93111 res = requests .post (API_URL , data = api_params )
94112 token = res .json ()
95113 return token
96114
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)
98124 """
99- Return the standard output of the program
125+ return self . __response [ "exit_code" ]
100126
101- :param: None
102- :rtype: String
127+ def getOutput (self ):
128+ """
129+ return standard output of program
103130 """
104131 return self .__stdout
105132
106133 def getMemory (self ):
107134 """
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
113136 """
114137 return self .__memory
115138
116139 def getError (self ):
117140 """
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
122142 """
123143 if self .__response ["stderr" ] != "" :
124144 return self .__response ["stderr" ]
125145 return None
126146
127147 def getTime (self ):
128148 """
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
134150 """
135151 return self .__time
136152
137153 def getStatus (self ):
138154 """
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
144156 """
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 )
147165
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 )
155166 return status
0 commit comments