Run C code directly from Python
Project description
ZeroC - Simple C Runner
Run C code directly from Python with seamless integration.
Installation
pip install zeroc
Basic Usage
from zeroc import run
# Simple C program execution
output = run("""
#include <stdio.h>
int main() {
printf("Hello from C!\\n");
return 0;
}
""")
print(output) # "Hello from C!"
Advanced Usage
With Input/Output
# Pass input to C program
result = run("""
#include <stdio.h>
int main() {
char name[100];
scanf("%s", name);
printf("Hello, %s!\\n", name);
return 0;
}
""", input_data="World")
print(result) # "Hello, World!"
Web Framework Integrations
Flask Example
from flask import Flask, request, jsonify
from zeroc import run
app = Flask(__name__)
@app.route('/run-c', methods=['POST'])
def execute_c():
try:
code = request.json.get('code')
input_data = request.json.get('input', '')
output = run(code, input_data=input_data)
return jsonify({"output": output})
except RuntimeError as e:
return jsonify({"error": str(e)}), 400
if __name__ == '__main__':
app.run()
Django Example
# views.py
from django.http import JsonResponse
from zeroc import run
def run_c_code(request):
if request.method == 'POST':
try:
code = request.POST.get('code')
input_data = request.POST.get('input', '')
output = run(code, input_data=input_data)
return JsonResponse({'output': output})
except RuntimeError as e:
return JsonResponse({'error': str(e)}, status=400)
FastAPI Example
from fastapi import FastAPI, HTTPException
from zeroc import run
app = FastAPI()
@app.post("/execute")
async def execute(code: str, input_data: str = ""):
try:
output = run(code, input_data=input_data)
return {"output": output}
except RuntimeError as e:
raise HTTPException(status_code=400, detail=str(e))
Performance Optimization
# Enable compiler optimizations (-O3 flag)
from zeroc.compiler import compile
from subprocess import run as subprocess_run
exe_path = compile("""
#include <stdio.h>
int main() {
// Performance-critical code
for(int i=0; i<1000000; i++) {
printf("%d\\n", i);
}
return 0;
}
""", optimize=True)
# Run the optimized executable
result = subprocess_run([exe_path], capture_output=True, text=True)
print(result.stdout)
Jupyter Notebook Usage
from IPython.display import display, Markdown
from zeroc import run
def run_c_in_notebook(code):
try:
output = run(code)
display(Markdown(f"```\\n{output}\\n```"))
except RuntimeError as e:
display(Markdown(f"**Error:** {e}"))
run_c_in_notebook("""
#include <stdio.h>
int main() {
printf("Notebook integration works!\\n");
return 0;
}
""")
Requirements
GCC compiler must be installed:
-
Linux: sudo apt-get install gcc
-
Mac: xcode-select --install
-
Windows: Install MinGW or use WSL
License
MIT
Author: Fidal PalamParambil
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zeroc-0.2.1.tar.gz
(3.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
zeroc-0.2.1-py3-none-any.whl
(3.5 kB
view details)
File details
Details for the file zeroc-0.2.1.tar.gz.
File metadata
- Download URL: zeroc-0.2.1.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7badb62ccde29080594c8479f939b24023b9375744011b0ec16147286f22bdd1
|
|
| MD5 |
c1849a05f7e141accb320bb77986648e
|
|
| BLAKE2b-256 |
39275c4af082a1252af3f1cb1a57ee251d3e9315668890f84e5fb7b0d26ad689
|
File details
Details for the file zeroc-0.2.1-py3-none-any.whl.
File metadata
- Download URL: zeroc-0.2.1-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee5cd4ba1a01e601842ba71a9f1c9a5385d799fd3a61c04df50b6ac5a9fbad57
|
|
| MD5 |
0bd0fb0e9e0d74ce6ab3adc00605c7a8
|
|
| BLAKE2b-256 |
2c4fbdb2c66bc1dbaca8e585118ee3d2c40da0979a3b59d18865db799e238d8a
|