Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ dmypy.json

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

# Custom
sample.py
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ script:
- isort --check-only --recursive coderunner
- black --check --diff coderunner
- flake8 coderunner --max-line-length=88 --ignore=F401
- pylint coderunner --disable=bad-continuation,invalid-name,too-many-instance-attributes,too-many-arguments,attribute-defined-outside-init
- pylint coderunner --disable=bad-continuation,invalid-name,too-many-instance-attributes,too-many-arguments,attribute-defined-outside-init,no-self-use
after_success:
- codecov
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

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

## [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)` for setting options for the compiler (i.e. compiler flags).
- `setArguments(arguments)` for setting Command line arguments for the program.

### Changed
- Minor internal improvemets.


## [0.5] - Dec 20, 2019

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ python tests.py
5. Lint the project with
```bash
flake8 coderunner --max-line-length=88 --ignore=F401
pylint coderunner --disable=bad-continuation,invalid-name,too-many-instance-attributes
black --check --diff coderunner
```

## 📝 Changelog
Expand Down
118 changes: 82 additions & 36 deletions coderunner/coderunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,38 @@
import urllib.request

# language IDs on judge0, see Documentation
languages = {"C++": 10, "Java": 27, "Python": 34, "C": 4, "Bash": 1}
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,
}

api_params = {
"number_of_runs": "1",
"cpu_time_limit": "2",
"cpu_extra_time": "0.5",
"wall_time_limit": "5",
Expand All @@ -28,6 +56,10 @@
FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"


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


class code:
"""
Args:
Expand All @@ -51,6 +83,7 @@ def __init__(
self.__memory = None
self.__time = None
self.__stdout = None
self.languages = list(languages.keys())

if self.path:
if not os.path.exists(source):
Expand All @@ -69,19 +102,28 @@ def __init__(
self.inp = inp

def __readCode(self):
with open(self.source, "r") as myfile:
data = myfile.read()
return data
try:
with open(self.source, "r") as program_file:
data = program_file.read()
return data
except FileNotFoundError as e:
raise e

def __readExpectedOutput(self):
with open(self.output, "r") as out:
data = out.read()
return data
try:
with open(self.output, "r") as exp_out:
data = exp_out.read()
return data
except FileNotFoundError as e:
raise e

def __readStandardInput(self):
with open(self.inp, "r") as out:
data = out.read()
return data
try:
with open(self.inp, "r") as standard_input:
data = standard_input.read()
return data
except FileNotFoundError as e:
raise e

def __readStatus(self, token: str):
"""
Expand Down Expand Up @@ -121,47 +163,53 @@ def __submit(self):
return token

def getSubmissionDate(self):
"""
return submission date/time of program
"""
"""Submission date/time of program"""
return self.__response["created_at"]

def getExitCode(self):
"""
return exitcode of program (0 or 1)
"""
"""Exitcode of program (0 or 1)"""
return self.__response["exit_code"]

def getOutput(self):
"""
return standard output of program
"""
"""Standard output of the program"""
return self.__stdout

def getMemory(self):
"""
return memory used by the program
"""
"""Memory used by the program"""
return self.__memory

def getError(self):
"""
return any error message occured during execution of program
"""
"""Error occured during execution of program"""
if self.__response["stderr"] != "":
return self.__response["stderr"]
return None

def getTime(self):
"""
return execution time of program
"""
"""Execution time of program"""
return self.__time

def run(self):
"""
submit the source code on judge0's server & return status
"""
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")

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")

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

if os.path.exists(self.source):
if self.path:
if self.inp is not None:
Expand All @@ -173,9 +221,7 @@ def run(self):
self.__token = token

def getStatus(self):
"""
Return submission status
"""
"""Submission status"""
status = self.__readStatus(self.__token)

return status
4 changes: 2 additions & 2 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import pprint

source_code = "testfiles/" + "test_python_input.py"
language = "Python"
language = "Python3"
output = "testfiles/output/" + "output2.txt"
Input = "testfiles/input/" + "input.txt"
r = coderunner.code(source_code, language, output, Input)

r2 = coderunner.code("print(\"yo\")", "Python", "YO", path=False)
r2 = coderunner.code("print(\"yo\")", "Python3", "YO", path=False)

# run the code
r.run()
Expand Down
13 changes: 13 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@



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

### Changed
- Minor internal improvemets.


## [0.5] - Dec 20, 2019

### Added
Expand Down
101 changes: 97 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,102 @@

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

[![Build Status](https://travis-ci.org/codeclassroom/CodeRunner.svg?branch=master)](https://travis-ci.org/codeclassroom/CodeRunner)

![PyPI](https://img.shields.io/pypi/v/coderunner?color=blue)
[![Build Status](https://travis-ci.org/codeclassroom/CodeRunner.svg?branch=master)](https://travis-ci.org/codeclassroom/CodeRunner)
[![codecov](https://codecov.io/gh/codeclassroom/CodeRunner/branch/master/graph/badge.svg)](https://codecov.io/gh/codeclassroom/CodeRunner)
![PyPI - Format](https://img.shields.io/pypi/format/coderunner?color=orange)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/coderunner)
[![Documentation Status](https://readthedocs.org/projects/coderunner/badge/?version=latest)](https://coderunner.readthedocs.io/en/latest/?badge=latest)
[![GitHub license](https://img.shields.io/github/license/codeclassroom/CodeRunner)](https://github.com/codeclassroom/CodeRunner/blob/master/LICENSE)
[![GitHub issues](https://img.shields.io/github/issues/codeclassroom/CodeRunner?color=blueviolet)](https://github.com/codeclassroom/CodeRunner/issues)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-orange.svg)](http://makeapullrequest.com)
![PyPI - Downloads](https://img.shields.io/pypi/dm/coderunner?color=blue)


## Installation

Install using `pip` from PyPI

```bash
pip install coderunner
```

or directly from GitHub if you cannot wait to test new features

```bash
pip install git+https://github.com/codeclassroom/CodeRunner.git
```

## Usage

```python

import coderunner

source_code = "path-to/test_python.py"
language = "Python"
expected_output = "path-to/output.txt"
standard_input = "path-to/input.txt"

# use this if you have a standard input to Program
r = coderunner.code(source_code, language, expected_output, standard_input)

# 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)
```

## Documentation

> [CodeRunner Documentation](https://coderunner.readthedocs.io/en/latest/)


## Development

##### Prerequisites
- Python 3.6+
- virtualenv

1. Create virtual environment.
```bash
virtualenv -p python3 venv && cd venv && source bin/activate
```
2. Clone the repository.
```bash
git https://github.com/codeclassroom/CodeRunner.git
```
3. Install Dependencies.
```bash
pip install -r requirements.txt
```
4. Run tests.
```bash
python tests.py
```
5. Lint the project with
```bash
flake8 coderunner --max-line-length=88 --ignore=F401
black --check --diff coderunner
```

## 📝 Changelog

See the [CHANGELOG.md](CHANGELOG.md) file for details.


## Author

👥 **Bhupesh Varshney**

- Twitter: [@bhupeshimself](https://twitter.com/bhupeshimself)
- DEV: [bhupesh](https://dev.to/bhupesh)

[![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)

## 📜 License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## 👋 Contributing

Please read the [CONTRIBUTING](CONTRIBUTING.md) guidelines for the process of submitting pull requests to us.
3 changes: 1 addition & 2 deletions docs/judge0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ CodeRunner is powered by Judge0 api.<br>
The following api parameters are set default by coderunner:
```python

api_params = {
"number_of_runs": "1",
{
"cpu_time_limit": "2",
"cpu_extra_time": "0.5",
"wall_time_limit": "5",
Expand Down
Loading