|
1 | | -import requests |
2 | 1 | import time |
3 | 2 | import sys |
| 3 | +import requests |
4 | 4 |
|
5 | 5 | # language IDs on judge0, see README.md |
6 | 6 | languages = { |
|
25 | 25 |
|
26 | 26 | API_URL = "https://api.judge0.com/submissions/" |
27 | 27 |
|
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 | + |
32 | 45 |
|
| 46 | + def readExpectedOutput(self): |
| 47 | + with open(self.output, 'r') as out: |
| 48 | + data = out.read() |
| 49 | + return data |
33 | 50 |
|
34 | | -def readExpectedOutput(output_file): |
35 | | - with open(output_file, 'r') as out: |
36 | | - data = out.read() |
37 | | - return data |
38 | 51 |
|
| 52 | + def readStandardInput(self): |
| 53 | + with open(self.inp, 'r') as out: |
| 54 | + data = out.read() |
| 55 | + return data |
39 | 56 |
|
40 | | -def readStandardInput(output_file): |
41 | | - with open(output_file, 'r') as out: |
42 | | - data = out.read() |
43 | | - return data |
44 | 57 |
|
| 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) |
45 | 69 |
|
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'] |
54 | 77 |
|
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) |
59 | 102 | 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 | + |
0 commit comments