Commit 0a5e6ce6 by David Ormsbee Committed by Calen Pennington

A little refactoring to make clearer what running test on problem means

parent d9e72b91
......@@ -12,6 +12,7 @@ import logging
import math
import numpy
import os
import os.path
import random
import re
import scipy
......@@ -125,6 +126,9 @@ class LoncapaProblem(object):
responder = response_types[response.tag](response, self.context, self.system)
responder.preprocess_response()
def __unicode__(self):
return u"LoncapaProblem ({0})".format(os.path.basename(self.filename))
def get_state(self):
''' Stored per-user session data neeeded to:
1) Recreate the problem
......
......@@ -2,6 +2,8 @@
"""
Commandline tool for doing operations on Problems
"""
from __future__ import unicode_literals
import argparse
import logging
import os.path
......@@ -26,12 +28,8 @@ def main():
args = parser.parse_args()
log.setLevel(args.log_level.upper())
old_stdout = sys.stdout
old_stderr = sys.stderr
for problem_file in args.files:
log.info("Opening {0}".format(problem_file.name))
sys.stdout = problem_stdout = StringIO()
sys.stderr = problem_stderr = StringIO()
try:
problem = LoncapaProblem(problem_file.name, "fakeid", seed=args.seed)
......@@ -41,28 +39,31 @@ def main():
continue
if args.command == 'test':
test_problem(problem)
log_captured_output(problem_stdout,
"captured stdout from {0}".format(problem_file.name))
log_captured_output(problem_stderr,
"captured stderr from {0}".format(problem_file.name))
command_test(problem)
# Print captured problem prints
problem_file.close()
sys.stdout = old_stdout
sys.stderr = old_stderr
# In case we want to do anything else here.
def log_captured_output(output_stream, stream_name):
output_stream.seek(0)
output_text = output_stream.read()
if output_text:
log.info("##### Begin {0} #####\n".format(stream_name) + output_text)
log.info("##### End {0} #####".format(stream_name))
def test_problem(problem):
def command_test(problem):
# We're going to trap stdout/stderr from the problems (yes, some print)
old_stdout, old_stderr = sys.stdout, sys.stderr
try:
sys.stdout = StringIO()
sys.stderr = StringIO()
check_that_suggested_answers_work(problem)
log_captured_output(sys.stdout,
"captured stdout from {0}".format(problem))
log_captured_output(sys.stderr,
"captured stderr from {0}".format(problem))
except Exception as e:
log.exception(e)
finally:
sys.stdout, sys.stderr = old_stdout, old_stderr
def check_that_suggested_answers_work(problem):
"""Split this up so that we're only used for formula/numeric answers.
Examples of where this fails:
......@@ -99,6 +100,12 @@ def test_problem(problem):
except Exception as ex:
log.exception(ex)
def log_captured_output(output_stream, stream_name):
output_stream.seek(0)
output_text = output_stream.read()
if output_text:
log.info("##### Begin {0} #####\n".format(stream_name) + output_text)
log.info("##### End {0} #####".format(stream_name))
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment