Hi all,
I'm designing an educational application that will run Python code and
check the output against a pre-define answer. I want to use the "exec"
statement to run the code, but I don't know how to get output from it.
For instance, exec works like this:
[color=blue][color=green][color=darkred]
>>> code = """[/color][/color][/color]
for i in xrange(1, 5):
print i
"""[color=blue][color=green][color=darkred]
>>> exec code[/color][/color][/color]
1
2
3
4
I want to store the values output by the print statement in a list. Is
there anyway to re-direct the output of the exec statement?
Also, it would be nice if exec had a timeout that automatically
haulted code that ran for too long. Is there a standard trick for
this? I expect I would have to run it in its own thread and kill the
thread when it takes too long (which reminds me I don't know anything
about Python threads!).
Toby
I'm designing an educational application that will run Python code and
check the output against a pre-define answer. I want to use the "exec"
statement to run the code, but I don't know how to get output from it.
For instance, exec works like this:
[color=blue][color=green][color=darkred]
>>> code = """[/color][/color][/color]
for i in xrange(1, 5):
print i
"""[color=blue][color=green][color=darkred]
>>> exec code[/color][/color][/color]
1
2
3
4
I want to store the values output by the print statement in a list. Is
there anyway to re-direct the output of the exec statement?
Also, it would be nice if exec had a timeout that automatically
haulted code that ran for too long. Is there a standard trick for
this? I expect I would have to run it in its own thread and kill the
thread when it takes too long (which reminds me I don't know anything
about Python threads!).
Toby
Comment