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
을 보일 수 없다 이 경우).
제출 테스트에서 코드를 실행할 수는 있지만 가능하면 피하고 싶습니다. 그렇지 않으면 각 테스트에 코드를 추가해야합니다.이 코드는별로 만족스럽지 않습니다!
는 어떤 도움을 크게 감상 할 수있다 :)
코드가 깨졌습니다. 예를 들어 submission은'code_check = None'과 같은 것을 포함 할 수 있습니다.'submission_test = code_check (code, CC, error)'줄에서'TypeError : 'NoneType'객체가 호출 가능하지 않습니다. 그건 그렇고 :'== True'와'== False'를 사용하는 것은 객체의 진리 값을 검사하는 잘못된 방법입니다. 그냥'submission_test : print ("passed") ...'를 사용하거나'is' 연산자를 사용하십시오. – Bakuriu
헤드 업 바큐 리 감사합니다. 이것은 단지 내부 테스트를위한 것입니다. 실제 구현은 Codeacademy입니다.나는 그들의 주자 기능이 어떻게 작동하는지 모르겠습니다. 나는 그들의 기능을 모방하려고 노력하고 있지만, 러너 기능의 보안에 대해 너무 걱정할 필요가 없습니다. 나는'is' 사용법으로 편집 할 것입니다. –
제출 된 코드와 제출 된 테스트를 하나의 문자열로 연결하고 그것에'exec'을 실행할 수 있습니까? – unutbu