whipcode-py
A convenient way to access Whipcode API from Python applications.
Compatible with self-hosted instances as well.
Installation
Get it from PyPI:
pip install whipcode
Usage
Here's an asynchronous snippet:
import asyncio
from whipcode import Whipcode, Langs
from whipcode.exceptions import RequestError, PayloadBuildError
async def main():
whip = Whipcode()
whip.rapid_key("YOUR_RAPIDAPI_KEY") # not needed if the RAPID_KEY environment variable is set
code = "echo 'Hello World!'"
execution = whip.run_async(code, Langs.BASH)
# Other tasks while the execution is in progress
try:
result = await execution
except (RequestError, PayloadBuildError) as e:
# Handle the error
asyncio.run(main())
And a synchronous one:
from whipcode import Whipcode, Langs
from whipcode.exceptions import RequestError, PayloadBuildError
def main():
whip = Whipcode()
code = '(println "Hello World!")'
try:
result = whip.run(code, Langs.CLOJURE)
except (RequestError, PayloadBuildError) as e:
# Handle the error
main()
The result object:
ExecutionResult(status=200, stdout='Hello World!\n', stderr='', container_age=0.338638005, timeout=False, detail='', rapid={'messages': '', 'message': '', 'info': ''})
Providers
Changing the provider is easy. Here's how the default provider is defined:
{
"endpoint": "https://whipcode.p.rapidapi.com/run",
"headers": {
"X-RapidAPI-Key": "",
"X-RapidAPI-Host": "whipcode.p.rapidapi.com"
},
"query_injects": [
{}
]
}
Just pass your custom provider to the constructor:
whip = Whipcode(custom_provider)
Or swap it in on the already initialized object:
whip.provider = custom_provider
An example custom provider:
{
"endpoint": "https://<host>/run",
"headers": {
"Authorization": "Bearer xxx"
},
"query_injects": []
}
Reference
Constructor
Whipcode(provider: dict = Whipcode.default_provider)
Parameters:
- provider - dict, optional
The provider configuration. See the Providers section.
rapid_key
rapid_key(key: str)
Sets the RapidAPI key to use when making requests.
Parameters:
- key - str
Your RapidAPI key.
run
run(code: str, language: str | int, args: list = [], timeout: int = 0) -> ExecutionResult
Makes a request to the endpoint synchronously.
Parameters:
- code - str
The code to execute. - language - str, int
Language ID of the submitted code. - args - list, optional
A list of compiler/interpreter args. - timeout - int, optional
Timeout in seconds for the code to run. - stdin - str, optional
Standard input passed to the execution. - env - dict, optional
Environment variables to set.
Returns:
run_async
run_async(code: str, language: str | int, args: list = [], timeout: int = 0) -> asyncio.Task
Makes a request to the endpoint asynchronously.
Parameters:
- code - str
The code to execute. - language - str, int
Language ID of the submitted code. - args - list, optional
A list of compiler/interpreter args. - timeout - int, optional
Timeout in seconds for the code to run. - stdin - str, optional
Standard input passed to the execution. - env - dict, optional
Environment variables to set.
Returns:
- A Task that returns ExecutionResult.
ExecutionResult
ExecutionResult(stdout: str, stderr: str, container_age: float, timeout: bool, status: int, detail: str, rapid: dict = {})
Returned as the result after a request.
Attributes
- stdout - str
All data captured from stdout. - stderr - str
All data captured from stderr. - container_age - float
Duration the container allocated for your code ran, in seconds. - timeout - bool
Boolean value depending on whether your container lived past the timeout period. - status - int
The status code of the request response. - detail - str
Details about why the request failed to complete. - rapid - dict
Various keys that RapidAPI uses when returning their own error messages.
Langs
Langs:
PYTHON = 1
JAVASCRIPT = 2
BASH = 3
PERL = 4
LUA = 5
RUBY = 6
C = 7
CPP = 8
RUST = 9
FORTRAN = 10
HASKELL = 11
JAVA = 12
GO = 13
TYPESCRIPT = 14
CLISP = 15
RACKET = 16
CRYSTAL = 17
CLOJURE = 18
X86 = 19
ZIG = 20
NIM = 21
DLANG = 22
CSHARP = 23
RSCRIPT = 24
DART = 25
VB = 26
FSHARP = 27
PHP = 28
A mapping of language IDs to their respective names.
Exceptions
- RequestError - Raised when an error occurs during the request
- PayloadBuildError - Raised when an error occurs while building the payload
Contributing
Please read the contributing guidelines before opening a pull request.
License
This library is licensed under the MIT License.
Metadata
Release files for whipcode 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| whipcode-0.2.1.tar.gz | 5.4 kB | Without provenance | Details for whipcode-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| whipcode-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for whipcode-0.2.1-py3-none-any.whl |
Total release size: 12.3 kB
Release files / whipcode-0.2.1.tar.gz
| Download URL | whipcode-0.2.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a5436289bb58d2c4414798fe3aa8fae941ba96c054a07b267b15adfff7b840c
|
|
BLAKE2b-256 checksum How to use checksums |
79e21a6004e0ccfc8dee15237661b688f7033792cea8865e0e371722d0a74f7c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Release files / whipcode-0.2.1-py3-none-any.whl
| Download URL | whipcode-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3835b6cb0b7162aebaa4a8715d9cb1dbb1cd9f7d578eedba59ebc98275f90b39
|
|
BLAKE2b-256 checksum How to use checksums |
d4f2c8aafe917fc654455f2f49d081fe1450a2b930daa0fde6d19b9de2cf2fb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Security for whipcode 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.