Skip to content

Commit

Permalink
Merge pull request #6 from codeclassroom/dev
Browse files Browse the repository at this point in the history
version 1.0
  • Loading branch information
Bhupesh-V committed Jul 23, 2020
2 parents 7ea5b2e + f84965b commit e257196
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 199 deletions.
2 changes: 0 additions & 2 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
version: 2

# Build documentation with MkDocs
#mkdocs:
# configuration: mkdocs.yml
mkdocs:
configuration: mkdocs.yml
fail_on_warning: false
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ python:

install:
- pip install -r requirements.txt
- pip install isort black flake8 pylint
- pip install codecov
script:
- python tests.py
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

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

## [1.0] - June , 2020
**Major Update (Breaking Changes)**

### Added
- Add API key authorizations
- Custom API URL support

### Changed
- 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.


## [0.8] - May 27, 2020

### Fix
Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CodeRunner 🏃

> A judge 👨🏽‍⚖️ for your programs, run and test your programs through Python
> A judge 👨🏽‍⚖️ for your programs, run and test your programs using Python

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

```python

import coderunner
from coderunner import coderunner
import os

source_code = "path-to/test_python.py"
language = "Python"
expected_output = "path-to/output.txt"
standard_input = "path-to/input.txt"
from dotenv import load_dotenv
load_dotenv()

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

# otherwise
r = coderunner.code(source_code, language, expected_output)

# you can also ignore both fields
r = coderunner.code(source_code, language)
API_KEY = os.environ["API_KEY"]

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

# Necessary step to initialize API keys & URL
r.api(key=API_KEY)

# run the code
r.run()
print(r.getOutput())
print(r.getError())

print("Running r :")
print("Status : " + r.getStatus())
print("Output : " + r.getOutput())

# See Documentation for more methods.
```

Expand Down
48 changes: 32 additions & 16 deletions coderunner/coderunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@
"max_file_size": "1024",
}

API_URL = "https://api.judge0.com/submissions/"
HEADERS = {"x-rapidapi-host": "judge0.p.rapidapi.com", "useQueryString": True}

FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"


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


class InvalidURL(Exception):
"""Raise when api_url is invalid"""


class code:
"""
Args:
Expand Down Expand Up @@ -132,10 +137,12 @@ def __readStandardInput(self):

def __readStatus(self, token: str):
"""
Check Submission status
Check Submission Status
"""
while True:
req = urllib.request.Request(API_URL + token["token"] + FIELDS)
req = urllib.request.Request(
f'{self.API_URL}submissions/{token["token"]}{FIELDS}', headers=HEADERS
)
with urllib.request.urlopen(req) as response:
req = response.read()

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

post_data = urllib.parse.urlencode(api_params).encode("ascii")
req = urllib.request.Request(API_URL, post_data)
req = urllib.request.Request(
f"{self.API_URL}submissions/", post_data, headers=HEADERS
)
with urllib.request.urlopen(req) as response:
req = response.read()
token = json.loads(req.decode("utf-8"))

return token

def api(self, key: str, url: str = None):
"""Setup API url and key"""
HEADERS["x-rapidapi-key"] = key
self.API_KEY = key
if url is None:
self.API_URL = "https://judge0.p.rapidapi.com/"
else:
user_api_url = urllib.parse.urlparse(url)
if user_api_url.scheme and user_api_url.netloc:
self.API_URL = url
else:
raise InvalidURL("Invalid API URL")

def getSubmissionDate(self):
"""Submission date/time of program"""
return self.__response["created_at"]
Expand Down Expand Up @@ -196,21 +218,15 @@ def getTime(self):

def setFlags(self, options: str):
"""Options for the compiler (i.e. compiler flags)"""
try:
if len(options) > 128:
raise ValueTooLargeError
api_params["compiler_options"] = options
except ValueTooLargeError:
print("Maximum 128 characters allowed")
if len(options) > 128:
raise ValueTooLargeError("Maximum 128 characters allowed")
api_params["compiler_options"] = options

def setArguments(self, arguments: str):
"""Command line arguments for the program"""
try:
if len(arguments) > 128:
raise ValueTooLargeError
api_params["command_line_arguments"] = arguments
except ValueTooLargeError:
print("Maximum 128 characters allowed")
if len(arguments) > 128:
raise ValueTooLargeError("Maximum 128 characters allowed")
api_params["command_line_arguments"] = arguments

def run(self, number_of_runs: int = 1):
"""Submit the source code on judge0's server & return status"""
Expand Down
28 changes: 11 additions & 17 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
from coderunner import coderunner
import pprint
import os

from dotenv import load_dotenv
load_dotenv()

source_code = "testfiles/" + "test_python_input.py"
language = "Python3"
output = "testfiles/output/" + "output2.txt"
Input = "testfiles/input/" + "input.txt"

API_KEY = os.environ["API_KEY"]

r = coderunner.code(source_code, language, output, Input)

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

# run the code
r.run()

print("Run r :")
print("Running r :")
print("Status : " + r.getStatus())

r2.run()

print("Run r2 :")
print("Status : " + r2.getStatus())

# check if any error occured
if r.getError() is not None:
pprint.pprint("Error : " + r.getError())
else:
print("Standard Output : ")
pprint.pprint(r.getOutput())
print("Execution Time : " + r.getTime())
print("Memory : " + str(r.getMemory()))
print("Output : " + r.getOutput())
26 changes: 20 additions & 6 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

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

## [1.0] - June , 2020
**Major Update (Breaking Changes)**

### Added
- Add API key authorizations
- Custom API URL support

### Changed
- 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.


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


## [0.7] - Jan 19, 2020

### Changed
- `code()` class now ignores `output`. i.e you can just provide source code & language to run a program.


## [0.6] - Jan 5, 2020

### Added

- 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.
- Ported NEW Languages. CodeRunner now supports all languages provided by Judge0.
- [`setFlags(options)`](/usage/#9-setflagsoptions) for setting options for the compiler (i.e. compiler flags).
- [`setArguments(arguments)`](/usage/#10-setargumentsarguments) for setting Command line arguments for the program.
- `setFlags(options)` for setting options for the compiler (i.e. compiler flags).
- `setArguments(arguments)` for setting Command line arguments for the program.

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


## [0.1] - Oct 30, 2019
- Initial Release

# Releases
See releases on [PyPi](https://pypi.org/project/coderunner/#history)
- Initial Release
34 changes: 22 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CodeRunner 🏃

> A judge 👨🏽‍⚖️ for your programs, run and test your programs through Python
> A judge 👨🏽‍⚖️ for your programs, run and test your programs using Python

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

```python

import coderunner
from coderunner import coderunner
import os

source_code = "path-to/test_python.py"
language = "Python"
expected_output = "path-to/output.txt"
standard_input = "path-to/input.txt"
from dotenv import load_dotenv
load_dotenv()

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

# otherwise
r = coderunner.code(source_code, language, expected_output)

# Use path=False if not using file paths
r = coderunner.code("Hello, World", language, "Hello, World", path=False)
API_KEY = os.environ["API_KEY"]

r = coderunner.code(source_code, language, output, Input)

# Necessary step to initialize API keys & URL
r.api(key=API_KEY)

# run the code
r.run()

print("Running r :")
print("Status : " + r.getStatus())
print("Output : " + r.getOutput())
```

## Documentation
Expand Down
37 changes: 36 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Usage

coderunner provides the following class constructors
coderunner provides the following class.

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

Expand Down Expand Up @@ -52,6 +52,41 @@ print(r.languages)

Methods available in class `code()`.

### api()

Since v1.0, you need to provide a API Key & URL for using Judge0 through coderunner.

Here is an example on how to do this.

```python
from coderunner import coderunner
import os
from dotenv import load_dotenv

load_dotenv()

source_code = "testfiles/" + "test_python_input.py"
language = "Python3"
output = "testfiles/output/" + "output2.txt"
Input = "testfiles/input/" + "input.txt"


API_KEY = os.environ["API_KEY"]

r = coderunner.code(source_code, language, output, Input)

# Necessary step to initialize API keys & URL
r.api(key=API_KEY)

r.run()

print("Run r :")
print("Status : " + r.getStatus())
print("Output : " + r.getOutput())
````

The default API URL is [https://judge0.p.rapidapi.com/]()

### 1. run()
**Parameters(type)** : Number Of Runs (`int`), optional<br>
**Return Type** : None <br>
Expand Down
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ site_name: CodeRunner
site_url: https://codeclassroom.github.io/CodeRunner/
repo_url: https://github.com/codeclassroom/CodeRunner
site_author: Bhupesh Varshney
site_description: CodeRunner v0.5 Documentation
copyright: © 2019, Bhupesh Varshney
site_description: CodeRunner v1.0 Documentation
copyright: © 2020, Bhupesh Varshney
nav:
- Documentation: index.md
- Installation: installation.md
Expand Down
2 changes: 1 addition & 1 deletion ppp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# Shell Script to Publish a Python Package
find_setup_py(){
Expand Down
Loading

0 comments on commit e257196

Please sign in to comment.