MA0301/exam_template/python/Python.py

25 lines
563 B
Python

import tempfile
import subprocess
import os
import sys
from common import replaceContent
def makeTmpFile(content):
fd, path = tempfile.mkstemp()
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])
)