Skip to content

Commit

Permalink
v1.0.2 (returns Output obj instead dict)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince2347X committed Apr 6, 2021
1 parent d70ed13 commit 3065461
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ An API wrapper of online compiler jdoodle.com written in python.


## How to install?
Use the following command on you device to install it.
Install pydoodle by running
```
pip install pydoodle
```

### Documentation
- Documentaion is hosted on [pydoodle.readthedocs.io](https://pydoodle.readthedocs.io) (Under development, contributions are appreciated)
- Documentation is hosted on [pydoodle.readthedocs.io](https://pydoodle.readthedocs.io) (Under development, contributions are appreciated)

### Example
- A basic [example](examples/example.py) on how to use this package.
- Look how easy it is to use:

```python
import pydoodle
c = pydoodle.Compiler(clientId="client-id", clientSecret="client-secret")
result = c.execute(script="print('Hello World')", language="python3")
usage = c.usage()
print(usage, result.output, sep='\n')
```
- [example.py](examples/example.py) -> Basic example on how to use!
- [example_stdIn.py](examples/example_stdIn.py) -> Example on how to use stdIn.


### Misc
- Star this repo if you're using this wrapper 😄
- Star [this repo](https://github.com/Prince2347X/pydoodle/) if you're using this wrapper 😄
- Head over to [jdoodle](https://jdoodle.com/compiler-api) to get clientId and clientSecret.
- Read the FAQs of the API [here.](https://docs.jdoodle.com/compiler-api/compiler-api)

Expand Down
4 changes: 2 additions & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Look how easy it is to use:
import pydoodle
c = pydoodle.Compiler(clientId="client-id", clientSecret="client-secret")
output = c.execute(script="print('Hello World')", language="python3")
result = c.execute(script="print('Hello World')", language="python3")
usage = c.usage()
print(usage, output, sep='\n')
print(usage, result.output, sep='\n')
Features
--------
Expand Down
4 changes: 2 additions & 2 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
with open(file="test1.py") as f:
script = f.read()
f.close()
output = c.execute(script=script, language="python3")
result = c.execute(script=script, language="python3")
usage = c.usage()
print(usage, output, sep='\n')
print(usage, result.output, sep='\n')
8 changes: 4 additions & 4 deletions examples/example_stdIn.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pydoodle.jdoodle import Compiler
import pydoodle

c = Compiler(clientId="client-id",
c = pydoodle.Compiler(clientId="client-id",
clientSecret="client-secret")
with open(file="test2.py") as f:
script = f.read()
f.close()
output = c.execute(script=script, language="python3", stdIn="Hello World!")
print(output)
result = c.execute(script=script, language="python3", stdIn="Hello World!")
print(result.output)
2 changes: 1 addition & 1 deletion pydoodle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# METADATA #
############

__version__ = "v1.0.1"
__version__ = "v1.0.2"
__title__ = "pydoodle"
__license__ = "MIT"
__author__ = "Prince2347X"
Expand Down
20 changes: 16 additions & 4 deletions pydoodle/jdoodle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
from .errors import *


class Output:
def __init__(self, output, statusCode, memory, cpuTime):
self.output = output
self.statusCode = statusCode
self.memory = memory
self.cpuTime = cpuTime


class Compiler:
def __init__(self, clientId: str, clientSecret: str):
"""
Expand Down Expand Up @@ -29,7 +37,7 @@ def __init__(self, clientId: str, clientSecret: str):
'whitespace', 'yabasic']
self.json = {}

def execute(self, script: str, language: str, stdIn: str = None, versionIndex: int = None) -> dict:
def execute(self, script: str, language: str, stdIn: str = None, versionIndex: int = None) -> Output:
"""
Executes the script and return output in dict datatype.
:param script: The script to be executed.
Expand All @@ -40,8 +48,8 @@ def execute(self, script: str, language: str, stdIn: str = None, versionIndex: i
:type stdIn: str
:param versionIndex: Version Index of language, defaults to 0.
:type versionIndex: int
:returns: Returns a dict, having following keys(if execution is successful): "output", "statusCode", "memory", "cpuTime"
:rtype: dict
:returns: Returns an Output object.
:rtype: Output
"""
if not isinstance(script, str):
raise TypeError
Expand All @@ -67,8 +75,12 @@ def execute(self, script: str, language: str, stdIn: str = None, versionIndex: i
else:
self.json["stdin"] = stdIn
response = requests.post(url=self.base_url, headers=self.headers, json=self.json)
response_json = response.json()
if response.status_code == 200:
return response.json()
return Output(output=response_json['output'],
statusCode=response_json['statusCode'],
memory=response_json['memory'],
cpuTime=response_json['cpuTime'])
else:
error = response.json()
if response.status_code == 401:
Expand Down

0 comments on commit 3065461

Please sign in to comment.