我期待在Codeacademy上撰写基础生物信息学课程。他们有一个很好的编写课程的界面,但测试有点慢,因为必须保存,然后预览,然后运行。如何获取由exec加载的任意变量,并在函数中使用它们
所以我想写一个模拟他们的小测试环境。它的工作原理是将用户输入的代码作为字符串读入到函数中,代码中的所有str
实例都被转换为unicode
(我刚刚使用过正则表达式),然后使用exec
。
棘手的部分似乎是当我想要合并Submission Test。
提交测试需要返回True
,False
或str
,并写成函数的主体。因此,例如:
的什么,我希望做一个简化版本:
# The submission test must be a function.
def test_code(code, CC, error):
# Use information from errors in student code
if error:
return "Yada yada %s" %error
# Use information in the raw student code
if len(code.split("\n")) is not 2:
return "This should be accomplished in 2 lines"
# Have direct access to variables from the student code
# I'd like to avoid params['y'] if possible.
try:
y
except NameError:
return "Please use the variable y"
if y is not 8:
return "Wrong! Check stuff"
# Use information from print output
if str(y) not in CC:
return "Remember to print your variable!"
return True
# Read in student code
student_code = """y = 8
print y
potato"""
# Catch print output
CC = StringIO.StringIO()
sys.stdout = CC
# Execute student code and catch errors
try:
exec student_code
except Exception as e:
error = e
# Start outputting to the terminal again
sys.stdout = sys.__stdout__
# Run the submission test
submission_test = test_code(student_code, CC.split("\n"), error)
# Output the result of the submission test
if submission_test is True:
print("Well done!")
elif submission_test is False:
print("Oops! You failed... Try again!")
else:
print(submission_test)
不过,我似乎无法从exec code
获得通过变量传递给提交测试功能(test_code
在这种情况下)。
我可以在提交测试中执行代码,但是如果可能的话我想避免这种情况,否则它将不得不被添加到每个测试中,这看起来好像没有问题!
任何帮助,将不胜感激:)
您的代码已损坏。例如,提交可能包含诸如'code_check = None'之类的东西,并且在'submission_test = code_check(code,CC,error)'这一行''末尾处理'TypeError:'NoneType'对象不可调用。顺便说一下:使用'== True'和'== False'是检查对象真值的错误方法。只要'是submission_test:print(“传递”)...',或者使用'is'运算符。 – Bakuriu 2013-02-24 13:43:44
感谢单挑Bakuriu。这仅用于内部测试。实际的实施在Codeacademy。我不确定他们的转轮功能是如何工作的。我只是想模仿他们的功能,但我不需要太担心我的跑步者功能的安全性。 虽然我会在'is'用法编辑:) – 2013-02-24 14:00:25
你可以将提交的代码和提交的测试(作为字符串)连接成一个字符串,然后在其上运行'exec'? – unutbu 2013-02-24 14:12:48