MA0301/exam_template/python/Python.py

27 lines
620 B
Python
Raw Permalink Normal View History

2021-05-17 22:06:10 +02:00
import tempfile
import subprocess
import os
import sys
from common import replaceContent
def makeTmpFile(content):
fd, path = tempfile.mkstemp()
2021-05-17 22:29:58 +02:00
while ('_' in path):
fd, path = tempfile.mkstemp()
2021-05-17 22:06:10 +02:00
with os.fdopen(fd, 'w') as tmp:
tmp.write(content)
return path
def grabOutput(path):
return subprocess.check_output(['python', path]).decode(sys.stdout.encoding)
def processFileContent(content):
path = makeTmpFile(content)
output = grabOutput(path)
return replaceContent(
(path, output),
'Python',
lambda temp, cont: temp.replace('%CODEFILE', cont[0]).replace('%OUTPUT', cont[1])
)