Skip to content

Commit cfed228

Browse files
committed
ported to package
1 parent be3e22e commit cfed228

File tree

3 files changed

+85
-69
lines changed

3 files changed

+85
-69
lines changed

CodeRunner/run.py

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import requests
21
import time
32
import sys
3+
import requests
44

55
# language IDs on judge0, see README.md
66
languages = {
@@ -25,67 +25,79 @@
2525

2626
API_URL = "https://api.judge0.com/submissions/"
2727

28-
def readCode(program):
29-
with open(program, 'r') as myfile:
30-
data = myfile.read()
31-
return data
28+
class coderunner:
29+
30+
def __init__(self, program_name, lang, output, inp = None):
31+
self.program_name = program_name
32+
self.lang = lang
33+
self.output = output
34+
self.inp = inp
35+
self.language_id = languages[lang]
36+
37+
def readCode(self):
38+
"""
39+
Read Source Code & return as string
40+
"""
41+
with open(self.program_name, 'r') as myfile:
42+
data = myfile.read()
43+
return data
44+
3245

46+
def readExpectedOutput(self):
47+
with open(self.output, 'r') as out:
48+
data = out.read()
49+
return data
3350

34-
def readExpectedOutput(output_file):
35-
with open(output_file, 'r') as out:
36-
data = out.read()
37-
return data
3851

52+
def readStandardInput(self):
53+
with open(self.inp, 'r') as out:
54+
data = out.read()
55+
return data
3956

40-
def readStandardInput(output_file):
41-
with open(output_file, 'r') as out:
42-
data = out.read()
43-
return data
4457

58+
def readStatus(self, token):
59+
"""
60+
Check Submission status
61+
"""
62+
while True:
63+
req = requests.get(API_URL + token['token'])
64+
response = req.json()
65+
status = response['status']['description']
66+
if status != "Processing" and status != "In Queue":
67+
break
68+
print(status)
4569

46-
def readStatus(token):
47-
while True:
48-
req = requests.get(API_URL + token['token'])
49-
response = req.json()
50-
status = response['status']['description']
51-
if status != "Processing" and status != "In Queue":
52-
break
53-
print(status)
70+
if status == "Accepted":
71+
#print(f'Output : \n{response["stdout"]}')
72+
print(f'Time : {response["time"]}')
73+
print("Compile Success ✅")
74+
return status
75+
else:
76+
return response['status']['description']
5477

55-
if status == "Accepted":
56-
#print(f'Output : \n{response["stdout"]}')
57-
print(f'Time : {response["time"]}')
58-
print("Compile Success ✅")
78+
def submit(self):
79+
if self.inp != None:
80+
api_params['stdin'] = self.inp
81+
82+
api_params['expected_output'] = self.output
83+
api_params['language_id'] = self.language_id
84+
api_params['source_code'] = self.program_name
85+
86+
res = requests.post(API_URL, data=api_params)
87+
token = res.json()
88+
return token
89+
90+
91+
def run(self):
92+
self.program_name = self.readCode()
93+
self.output = self.readExpectedOutput()
94+
95+
if self.inp != None:
96+
self.inp = self.readStandardInput()
97+
token = self.submit()
98+
status = self.readStatus(token)
99+
else:
100+
token = self.submit()
101+
status = self.readStatus(token)
59102
return status
60-
else:
61-
return response['status']['description']
62-
63-
def submit(program, language_id, *argv):
64-
if len(argv) == 2:
65-
stdout = argv[0]
66-
stdin = argv[1]
67-
api_params['stdin'] = stdin
68-
elif len(argv) == 1:
69-
stdout = argv[0]
70-
71-
api_params['expected_output'] = stdout
72-
api_params['language_id'] = language_id
73-
api_params['source_code'] = program
74-
75-
res = requests.post(API_URL, data=api_params)
76-
token = res.json()
77-
return token
78-
79-
80-
def run(program, language, *argv):
81-
program = readCode(program)
82-
stdout = readExpectedOutput(argv[0])
83-
84-
if len(argv) == 2:
85-
stdin = readStandardInput(argv[1])
86-
token = submit(program, languages[language], stdout, stdin)
87-
status = readStatus(token)
88-
elif len(argv) == 1:
89-
token = submit(program, languages[language], stdout)
90-
status = readStatus(token)
91-
return status
103+

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ python3 tests.py
2222

2323
### Usage
2424

25-
Import the `run` method from *CodeRunner* module.
26-
2725
```python
28-
from CodeRunner.run import run
26+
import CodeRunner.run as cr
2927

30-
program_name = "test_python.py"
28+
program_name = "testfiles/" + "test_python.py"
3129
language = "Python"
32-
output = "output.txt"
30+
output = "testfiles/" + "output2.txt"
31+
Input = "testfiles/" + "input.txt"
32+
r = cr.coderunner(program_name, language, output, Input)
3333

34-
status = run(program_name, language, output)
35-
print(status)
34+
print(r.run())
3635
```
3736

3837

tests.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import unittest
2-
from CodeRunner.run import run
2+
import CodeRunner.run as cr
33

44
#test for Java program
55
class TestRunA(unittest.TestCase):
66
def test_run(self):
77
program_name = "testfiles/" + "test_java.java"
88
language = "Java"
99
output = "testfiles/" + "output.txt"
10-
self.assertEqual(run(program_name, language, output),
10+
r = cr.coderunner(program_name, language, output)
11+
self.assertEqual(r.run(),
1112
"Accepted", "Something Wrong")
1213

1314
#test for Python program
@@ -17,16 +18,19 @@ def test_run(self):
1718
language = "Python"
1819
output = "testfiles/" + "output2.txt"
1920
Input = "testfiles/" + "input.txt"
20-
self.assertEqual(run(program_name, language, output, Input),
21+
r = cr.coderunner(program_name, language, output, Input)
22+
self.assertEqual(r.run(),
2123
"Accepted", "Something Wrong")
24+
2225
#test for C program
2326
class TestRunC(unittest.TestCase):
2427
def test_run(self):
2528
program_name = "testfiles/" + "test_c.c"
2629
language = "C"
2730
output = "testfiles/" + "output3.txt"
2831
Input = "testfiles/" + "input2.txt"
29-
self.assertEqual(run(program_name, language, output, Input),
32+
r = cr.coderunner(program_name, language, output, Input)
33+
self.assertEqual(r.run(),
3034
"Accepted", "Something Wrong")
3135

3236
#test for C++ program
@@ -36,7 +40,8 @@ def test_run(self):
3640
language = "C++"
3741
output = "testfiles/" + "output4.txt"
3842
Input = "testfiles/" + "input3.txt"
39-
self.assertEqual(run(program_name, language, output, Input),
43+
r = cr.coderunner(program_name, language, output, Input)
44+
self.assertEqual(r.run(),
4045
"Accepted", "Something Wrong")
4146

4247

0 commit comments

Comments
 (0)