-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathef.py
More file actions
78 lines (64 loc) · 1.85 KB
/
ef.py
File metadata and controls
78 lines (64 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import json
import argparse
from termcolor import cprint
def ext_to_lang(file_extension):
# getting file type
match file_extension:
case "cpp":
return "cpp"
case "c":
return "c"
case "py":
return "python"
case "java":
return "java"
case "js":
return "javascript"
case "cs":
return "csharp"
def get_data(input: str, filename: str) -> dict:
"""
Retrieves data by sending a POST request to the specified API endpoint.
Parameters:
input (str): The input code to be executed.
filename (str): The name of the file containing the input code.
Returns:
dict: The JSON response from the API endpoint.
"""
url = "https://onecompiler.com/api/code/exec"
file_extension = filename.split(".")[-1]
language = ext_to_lang(file_extension)
request_obj = {
"properties": {
"language": language,
"files": [{"name": filename, "content": input}],
},
}
headers = {
"content-type": "application/json",
}
response = requests.post(
url,
headers=headers,
data=json.dumps(request_obj),
).json()
return response["stdout"], response["stderr"], response["executionTime"]
if __name__ == "__main__":
# Parsing Args
parser = argparse.ArgumentParser()
parser.add_argument("file", help="the file to compile")
args = parser.parse_args()
filename = args.file
# reading file
content = ""
with open(filename) as f:
content = f.read()
# Getting Response
stdout, stderr, executionTime = get_data(content, filename)
# Printing
if stdout:
print(stdout)
if stderr:
cprint(stderr, "red")
cprint(f"Execution Time : {executionTime}ms", "yellow")