Skip to content

Commit 8ffba2a

Browse files
committed
new features
- run() accepts number of times to run the code. - new method setFlags() to set compiler options(flags) - All languages ported from Judge0
1 parent 7a8c40d commit 8ffba2a

File tree

4 files changed

+54
-42
lines changed

4 files changed

+54
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,5 @@ dmypy.json
131131

132132
# End of https://www.gitignore.io/api/python
133133

134+
# Custom
135+
sample.py

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.6] - Jan x, 2020
6+
7+
### Added
8+
9+
- New optional argument, `number_of_runs` in `run()` method, use this to specify no.of times you want to run the code.
10+
- Ported NEW Languages. CodeRunner now supports all languages, judge0 provides.
11+
- New method `setFlags(options)` for setting ptions for the compiler (i.e. compiler flags).
12+
13+
### Changed
14+
- Minor internal improvemets.
15+
516

617
## [0.5] - Dec 20, 2019
718

coderunner/coderunner.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
import urllib.request
1010

1111
# language IDs on judge0, see Documentation
12-
languages = {"C++": 10, "Java": 27, "Python": 34, "C": 4, "Bash": 1}
12+
languages = {'Assembly': 45, 'Bash': 46, 'Basic': 47, 'C': 50, 'C++': 54, 'C#': 51, 'Common Lisp': 55, 'D': 56, 'Elixir': 57, 'Erlang': 58, 'Executable': 44, 'Fortran': 59, 'Go': 60, 'Haskell': 61, 'Java': 62, 'JavaScript': 63, 'Lua': 64, 'OCaml': 65, 'Octave': 66, 'Pascal': 67, 'PHP': 68, 'Plain Text': 43, 'Prolog': 69, 'Python2': 70, 'Python3': 71, 'Ruby': 72, 'Rust': 73, 'TypeScript': 74}
1313

1414
api_params = {
15-
"number_of_runs": "1",
1615
"cpu_time_limit": "2",
1716
"cpu_extra_time": "0.5",
1817
"wall_time_limit": "5",
@@ -51,6 +50,7 @@ def __init__(
5150
self.__memory = None
5251
self.__time = None
5352
self.__stdout = None
53+
self.languages = list(languages.keys())
5454

5555
if self.path:
5656
if not os.path.exists(source):
@@ -69,19 +69,28 @@ def __init__(
6969
self.inp = inp
7070

7171
def __readCode(self):
72-
with open(self.source, "r") as myfile:
73-
data = myfile.read()
74-
return data
72+
try:
73+
with open(self.source, "r") as program_file:
74+
data = program_file.read()
75+
return data
76+
except FileNotFoundError as e:
77+
raise e
7578

7679
def __readExpectedOutput(self):
77-
with open(self.output, "r") as out:
78-
data = out.read()
79-
return data
80+
try:
81+
with open(self.output, "r") as exp_out:
82+
data = exp_out.read()
83+
return data
84+
except FileNotFoundError as e:
85+
raise e
8086

8187
def __readStandardInput(self):
82-
with open(self.inp, "r") as out:
83-
data = out.read()
84-
return data
88+
try:
89+
with open(self.inp, "r") as standard_input:
90+
data = standard_input.read()
91+
return data
92+
except FileNotFoundError as e:
93+
raise e
8594

8695
def __readStatus(self, token: str):
8796
"""
@@ -121,47 +130,39 @@ def __submit(self):
121130
return token
122131

123132
def getSubmissionDate(self):
124-
"""
125-
return submission date/time of program
126-
"""
133+
"""Submission date/time of program"""
127134
return self.__response["created_at"]
128135

129136
def getExitCode(self):
130-
"""
131-
return exitcode of program (0 or 1)
132-
"""
137+
"""Exitcode of program (0 or 1)"""
133138
return self.__response["exit_code"]
134139

135140
def getOutput(self):
136-
"""
137-
return standard output of program
138-
"""
141+
"""Standard output of the program"""
139142
return self.__stdout
140143

141144
def getMemory(self):
142-
"""
143-
return memory used by the program
144-
"""
145+
"""Memory used by the program"""
145146
return self.__memory
146147

147148
def getError(self):
148-
"""
149-
return any error message occured during execution of program
150-
"""
149+
"""Error occured during execution of program"""
151150
if self.__response["stderr"] != "":
152151
return self.__response["stderr"]
153152
return None
154153

155154
def getTime(self):
156-
"""
157-
return execution time of program
158-
"""
155+
"""Execution time of program"""
159156
return self.__time
160157

161-
def run(self):
162-
"""
163-
submit the source code on judge0's server & return status
164-
"""
158+
def setFlags(self, options: str):
159+
"""Options for the compiler (i.e. compiler flags)"""
160+
api_params["compiler_options"] = options
161+
162+
def run(self, number_of_runs: int = 1):
163+
"""Submit the source code on judge0's server & return status"""
164+
api_params["number_of_runs"] = number_of_runs
165+
165166
if os.path.exists(self.source):
166167
if self.path:
167168
if self.inp is not None:
@@ -173,9 +174,7 @@ def run(self):
173174
self.__token = token
174175

175176
def getStatus(self):
176-
"""
177-
Return submission status
178-
"""
177+
"""Submission status"""
179178
status = self.__readStatus(self.__token)
180179

181180
return status

tests.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ def test_run(self):
3030
self.assertEqual(r.getStatus(),
3131
"Accepted", "Something Wrong")
3232

33-
# test for Python program
33+
# test for Python3 program
3434

3535

3636
class TestRunC(unittest.TestCase):
3737

3838
def test_run(self):
3939
source_code = "testfiles/" + "test_python.py"
40-
language = "Python"
40+
language = "Python3"
4141
output = "testfiles/output/" + "output6.txt"
4242
r = coderunner.code(source_code, language, output)
4343
r.run()
4444
self.assertEqual(r.getStatus(),
4545
"Accepted", "Something Wrong")
4646

47-
# test for Python program with input
47+
# test for Python3 program with input
4848

4949

5050
class TestRunD(unittest.TestCase):
5151

5252
def test_run(self):
5353
source_code = "testfiles/" + "test_python_input.py"
54-
language = "Python"
54+
language = "Python3"
5555
output = "testfiles/output/" + "output2.txt"
5656
Input = "testfiles/input/" + "input.txt"
5757
r = coderunner.code(source_code, language, output, Input)
@@ -130,7 +130,7 @@ def test_run(self):
130130
with self.assertRaises(ValueError):
131131
coderunner.code(
132132
"Hello World",
133-
"PHP",
133+
"fgbh",
134134
"Hello World",
135135
path=False
136136
)
@@ -186,7 +186,7 @@ class TestRunM(unittest.TestCase):
186186

187187
def test_run(self):
188188
source_code = "print(\"This will return Wrong Answer\")"
189-
language = "Python"
189+
language = "Python3"
190190
output = "Wrong Answer"
191191
r = coderunner.code(source_code, language, output, path=False)
192192
r.run()

0 commit comments

Comments
 (0)