Skip to content

Commit e257196

Browse files
authored
Merge pull request #6 from codeclassroom/dev
version 1.0
2 parents 7ea5b2e + f84965b commit e257196

File tree

15 files changed

+291
-199
lines changed

15 files changed

+291
-199
lines changed

.readthedocs.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
version: 2
77

88
# Build documentation with MkDocs
9-
#mkdocs:
10-
# configuration: mkdocs.yml
119
mkdocs:
1210
configuration: mkdocs.yml
1311
fail_on_warning: false

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ python:
66

77
install:
88
- pip install -r requirements.txt
9-
- pip install isort black flake8 pylint
109
- pip install codecov
1110
script:
1211
- python tests.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+
## [1.0] - June , 2020
6+
**Major Update (Breaking Changes)**
7+
8+
### Added
9+
- Add API key authorizations
10+
- Custom API URL support
11+
12+
### Changed
13+
- Due to Judge0 API going [freemium](https://github.com/judge0/api/issues/171), the default API URL, [https://api.judge0.com] is no longer available. To use the API signup for a plan on [RapidAPI](https://rapidapi.com/hermanzdosilovic/api/judge0/pricing) & use the token to access the API through coderunner.
14+
15+
516
## [0.8] - May 27, 2020
617

718
### Fix

README.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CodeRunner 🏃
22

3-
> A judge 👨🏽‍⚖️ for your programs, run and test your programs through Python
3+
> A judge 👨🏽‍⚖️ for your programs, run and test your programs using Python
44
55

66
![PyPI](https://img.shields.io/pypi/v/coderunner?color=blue)
@@ -30,28 +30,32 @@ pip install git+https://github.com/codeclassroom/CodeRunner.git
3030

3131
```python
3232

33-
import coderunner
33+
from coderunner import coderunner
34+
import os
3435

35-
source_code = "path-to/test_python.py"
36-
language = "Python"
37-
expected_output = "path-to/output.txt"
38-
standard_input = "path-to/input.txt"
36+
from dotenv import load_dotenv
37+
load_dotenv()
3938

40-
# use this if you have a standard input to Program
41-
r = coderunner.code(source_code, language, expected_output, standard_input)
39+
source_code = "testfiles/" + "test_python_input.py"
40+
language = "Python3"
41+
output = "testfiles/output/" + "output2.txt"
42+
Input = "testfiles/input/" + "input.txt"
4243

43-
# otherwise
44-
r = coderunner.code(source_code, language, expected_output)
4544

46-
# you can also ignore both fields
47-
r = coderunner.code(source_code, language)
45+
API_KEY = os.environ["API_KEY"]
4846

49-
# Use path=False if not using file paths
50-
r = coderunner.code("Hello, World", language, "Hello, World", path=False)
47+
r = coderunner.code(source_code, language, output, Input)
5148

49+
# Necessary step to initialize API keys & URL
50+
r.api(key=API_KEY)
51+
52+
# run the code
5253
r.run()
53-
print(r.getOutput())
54-
print(r.getError())
54+
55+
print("Running r :")
56+
print("Status : " + r.getStatus())
57+
print("Output : " + r.getOutput())
58+
5559
# See Documentation for more methods.
5660
```
5761

coderunner/coderunner.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@
5252
"max_file_size": "1024",
5353
}
5454

55-
API_URL = "https://api.judge0.com/submissions/"
55+
HEADERS = {"x-rapidapi-host": "judge0.p.rapidapi.com", "useQueryString": True}
56+
5657
FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"
5758

5859

5960
class ValueTooLargeError(Exception):
6061
"""Raised when the input value is too large"""
6162

6263

64+
class InvalidURL(Exception):
65+
"""Raise when api_url is invalid"""
66+
67+
6368
class code:
6469
"""
6570
Args:
@@ -132,10 +137,12 @@ def __readStandardInput(self):
132137

133138
def __readStatus(self, token: str):
134139
"""
135-
Check Submission status
140+
Check Submission Status
136141
"""
137142
while True:
138-
req = urllib.request.Request(API_URL + token["token"] + FIELDS)
143+
req = urllib.request.Request(
144+
f'{self.API_URL}submissions/{token["token"]}{FIELDS}', headers=HEADERS
145+
)
139146
with urllib.request.urlopen(req) as response:
140147
req = response.read()
141148

@@ -161,13 +168,28 @@ def __submit(self):
161168
api_params["source_code"] = self.source
162169

163170
post_data = urllib.parse.urlencode(api_params).encode("ascii")
164-
req = urllib.request.Request(API_URL, post_data)
171+
req = urllib.request.Request(
172+
f"{self.API_URL}submissions/", post_data, headers=HEADERS
173+
)
165174
with urllib.request.urlopen(req) as response:
166175
req = response.read()
167176
token = json.loads(req.decode("utf-8"))
168177

169178
return token
170179

180+
def api(self, key: str, url: str = None):
181+
"""Setup API url and key"""
182+
HEADERS["x-rapidapi-key"] = key
183+
self.API_KEY = key
184+
if url is None:
185+
self.API_URL = "https://judge0.p.rapidapi.com/"
186+
else:
187+
user_api_url = urllib.parse.urlparse(url)
188+
if user_api_url.scheme and user_api_url.netloc:
189+
self.API_URL = url
190+
else:
191+
raise InvalidURL("Invalid API URL")
192+
171193
def getSubmissionDate(self):
172194
"""Submission date/time of program"""
173195
return self.__response["created_at"]
@@ -196,21 +218,15 @@ def getTime(self):
196218

197219
def setFlags(self, options: str):
198220
"""Options for the compiler (i.e. compiler flags)"""
199-
try:
200-
if len(options) > 128:
201-
raise ValueTooLargeError
202-
api_params["compiler_options"] = options
203-
except ValueTooLargeError:
204-
print("Maximum 128 characters allowed")
221+
if len(options) > 128:
222+
raise ValueTooLargeError("Maximum 128 characters allowed")
223+
api_params["compiler_options"] = options
205224

206225
def setArguments(self, arguments: str):
207226
"""Command line arguments for the program"""
208-
try:
209-
if len(arguments) > 128:
210-
raise ValueTooLargeError
211-
api_params["command_line_arguments"] = arguments
212-
except ValueTooLargeError:
213-
print("Maximum 128 characters allowed")
227+
if len(arguments) > 128:
228+
raise ValueTooLargeError("Maximum 128 characters allowed")
229+
api_params["command_line_arguments"] = arguments
214230

215231
def run(self, number_of_runs: int = 1):
216232
"""Submit the source code on judge0's server & return status"""

demo.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
from coderunner import coderunner
2-
import pprint
2+
import os
3+
4+
from dotenv import load_dotenv
5+
load_dotenv()
36

47
source_code = "testfiles/" + "test_python_input.py"
58
language = "Python3"
69
output = "testfiles/output/" + "output2.txt"
710
Input = "testfiles/input/" + "input.txt"
11+
12+
API_KEY = os.environ["API_KEY"]
13+
814
r = coderunner.code(source_code, language, output, Input)
915

10-
r2 = coderunner.code("print(\"yo\")", "Python3", "YO", path=False)
16+
# Necessary step to initialize API keys & URL
17+
r.api(key=API_KEY)
1118

1219
# run the code
1320
r.run()
1421

15-
print("Run r :")
22+
print("Running r :")
1623
print("Status : " + r.getStatus())
17-
18-
r2.run()
19-
20-
print("Run r2 :")
21-
print("Status : " + r2.getStatus())
22-
23-
# check if any error occured
24-
if r.getError() is not None:
25-
pprint.pprint("Error : " + r.getError())
26-
else:
27-
print("Standard Output : ")
28-
pprint.pprint(r.getOutput())
29-
print("Execution Time : " + r.getTime())
30-
print("Memory : " + str(r.getMemory()))
24+
print("Output : " + r.getOutput())

docs/changelog.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
All notable changes to this project will be documented in this file.
4+
5+
## [1.0] - June , 2020
6+
**Major Update (Breaking Changes)**
7+
8+
### Added
9+
- Add API key authorizations
10+
- Custom API URL support
11+
12+
### Changed
13+
- Due to Judge0 API going [freemium](https://github.com/judge0/api/issues/171), the default API URL, [https://api.judge0.com] is no longer available. To use the API signup for a plan on [RapidAPI](https://rapidapi.com/hermanzdosilovic/api/judge0/pricing) & use the token to access the API through coderunner.
314

415

516
## [0.8] - May 27, 2020
@@ -8,14 +19,20 @@
819
- Fixed bug where compiling a Java program resulted in Compilation Error.
920

1021

22+
## [0.7] - Jan 19, 2020
23+
24+
### Changed
25+
- `code()` class now ignores `output`. i.e you can just provide source code & language to run a program.
26+
27+
1128
## [0.6] - Jan 5, 2020
1229

1330
### Added
1431

1532
- New optional argument, `number_of_runs` in `run()` method, use this to specify no.of times you want to run the code. Default is set to 1.
1633
- Ported NEW Languages. CodeRunner now supports all languages provided by Judge0.
17-
- [`setFlags(options)`](/usage/#9-setflagsoptions) for setting options for the compiler (i.e. compiler flags).
18-
- [`setArguments(arguments)`](/usage/#10-setargumentsarguments) for setting Command line arguments for the program.
34+
- `setFlags(options)` for setting options for the compiler (i.e. compiler flags).
35+
- `setArguments(arguments)` for setting Command line arguments for the program.
1936

2037
### Changed
2138
- Minor internal improvemets.
@@ -67,7 +84,4 @@ This is effect fixes [#2](https://github.com/codeclassroom/CodeRunner/issues/2).
6784

6885

6986
## [0.1] - Oct 30, 2019
70-
- Initial Release
71-
72-
# Releases
73-
See releases on [PyPi](https://pypi.org/project/coderunner/#history)
87+
- Initial Release

docs/index.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CodeRunner 🏃
22

3-
> A judge 👨🏽‍⚖️ for your programs, run and test your programs through Python
3+
> A judge 👨🏽‍⚖️ for your programs, run and test your programs using Python
44
55

66
![PyPI](https://img.shields.io/pypi/v/coderunner?color=blue)
@@ -30,21 +30,31 @@ pip install git+https://github.com/codeclassroom/CodeRunner.git
3030

3131
```python
3232

33-
import coderunner
33+
from coderunner import coderunner
34+
import os
3435

35-
source_code = "path-to/test_python.py"
36-
language = "Python"
37-
expected_output = "path-to/output.txt"
38-
standard_input = "path-to/input.txt"
36+
from dotenv import load_dotenv
37+
load_dotenv()
3938

40-
# use this if you have a standard input to Program
41-
r = coderunner.code(source_code, language, expected_output, standard_input)
39+
source_code = "testfiles/" + "test_python_input.py"
40+
language = "Python3"
41+
output = "testfiles/output/" + "output2.txt"
42+
Input = "testfiles/input/" + "input.txt"
4243

43-
# otherwise
44-
r = coderunner.code(source_code, language, expected_output)
4544

46-
# Use path=False if not using file paths
47-
r = coderunner.code("Hello, World", language, "Hello, World", path=False)
45+
API_KEY = os.environ["API_KEY"]
46+
47+
r = coderunner.code(source_code, language, output, Input)
48+
49+
# Necessary step to initialize API keys & URL
50+
r.api(key=API_KEY)
51+
52+
# run the code
53+
r.run()
54+
55+
print("Running r :")
56+
print("Status : " + r.getStatus())
57+
print("Output : " + r.getOutput())
4858
```
4959

5060
## Documentation

docs/usage.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage
22

3-
coderunner provides the following class constructors
3+
coderunner provides the following class.
44

55
### code(source, lang, output, inp, path)
66

@@ -52,6 +52,41 @@ print(r.languages)
5252

5353
Methods available in class `code()`.
5454

55+
### api()
56+
57+
Since v1.0, you need to provide a API Key & URL for using Judge0 through coderunner.
58+
59+
Here is an example on how to do this.
60+
61+
```python
62+
from coderunner import coderunner
63+
import os
64+
from dotenv import load_dotenv
65+
66+
load_dotenv()
67+
68+
source_code = "testfiles/" + "test_python_input.py"
69+
language = "Python3"
70+
output = "testfiles/output/" + "output2.txt"
71+
Input = "testfiles/input/" + "input.txt"
72+
73+
74+
API_KEY = os.environ["API_KEY"]
75+
76+
r = coderunner.code(source_code, language, output, Input)
77+
78+
# Necessary step to initialize API keys & URL
79+
r.api(key=API_KEY)
80+
81+
r.run()
82+
83+
print("Run r :")
84+
print("Status : " + r.getStatus())
85+
print("Output : " + r.getOutput())
86+
````
87+
88+
The default API URL is [https://judge0.p.rapidapi.com/]()
89+
5590
### 1. run()
5691
**Parameters(type)** : Number Of Runs (`int`), optional<br>
5792
**Return Type** : None <br>

mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ site_name: CodeRunner
22
site_url: https://codeclassroom.github.io/CodeRunner/
33
repo_url: https://github.com/codeclassroom/CodeRunner
44
site_author: Bhupesh Varshney
5-
site_description: CodeRunner v0.5 Documentation
6-
copyright: © 2019, Bhupesh Varshney
5+
site_description: CodeRunner v1.0 Documentation
6+
copyright: © 2020, Bhupesh Varshney
77
nav:
88
- Documentation: index.md
99
- Installation: installation.md

0 commit comments

Comments
 (0)