Skip to content

Commit

Permalink
Add new python3_scratchpad question type
Browse files Browse the repository at this point in the history
  • Loading branch information
trampgeek committed Apr 1, 2023
1 parent f99fba8 commit 7967d3b
Show file tree
Hide file tree
Showing 19 changed files with 2,189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/ucquestiontypes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added python3_scratchpad/.cache/pylint/__source_1.stats
Binary file not shown.
47 changes: 47 additions & 0 deletions python3_scratchpad/__author_solution.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="coderunner-test-results good">
<a class="btn btn-link coderunner-solution-link" style="background-color:#CFC">Show author's solution</a>
</div>
<div class="coderunner-authors-solution collapse" expanded="false" style="margin-bottom:8px">
<pre class="code-highlight">%s</pre>
</div>
<script>
window.coderunner_solution_link_clicked = window.coderunner_solution_link_clicked || function(src) {
var question = src.target.closest('div.specificfeedback');
var solution_div = question.getElementsByClassName('coderunner-authors-solution')[0];
var solution_link = question.getElementsByClassName('coderunner-solution-link')[0];
var code = question.getElementsByClassName('code-highlight')[0];

if (!code.classList.contains('has-highlight')) {
var highlight = window.ace.require("ace/ext/static_highlight");
highlight(code, {
mode: "ace/mode/python",
showGutter: false
}, function(highlighted) {
code.getElementsByClassName('ace_static_highlight')[0].style['font-size'] = "14px";
});
code.classList.add('has-highlight');
}

if (!$(solution_div).hasClass('collapsing')) {
if ($(solution_div).attr("expanded") === "true") {
solution_link.innerHTML = "Show author's solution";
$(solution_div).hide(300);
$(solution_div).attr("expanded", "false");
} else {
solution_link.innerHTML = "Hide author's solution";
$(solution_div).show(300);
$(solution_div).attr("expanded", "true");
}
}
}
var coderunner_all_links = document.getElementsByClassName('coderunner-solution-link');

for (var i = 0; i < coderunner_all_links.length; i++) {
var el = coderunner_all_links[i];
if (!el.classList.contains('has-click-handler')) {
el.addEventListener('click', window.coderunner_solution_link_clicked);
el.classList.add('has-click-handler');
}
}
</script>

47 changes: 47 additions & 0 deletions python3_scratchpad/__author_solution_scrambled.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="coderunner-test-results bad">
<a class="btn btn-link coderunner-scrambled-solution-link" style="background-color:#FCC">Show scrambled author's solution</a>
</div>
<div class="coderunner-scrambled-authors-solution collapse" expanded="false" style="margin-bottom:8px">
<pre class="code-highlight">%s</pre>
</div>
<script>
window.coderunner_solution_link_clicked = window.coderunner_solution_link_clicked || function(src) {
var question = src.target.closest('div.specificfeedback');
var solution_div = question.getElementsByClassName('coderunner-scrambled-authors-solution')[0];
var solution_link = question.getElementsByClassName('coderunner-scrambled-solution-link')[0];
var code = question.getElementsByClassName('code-highlight')[0];

if (!code.classList.contains('has-highlight')) {
var highlight = window.ace.require("ace/ext/static_highlight");
highlight(code, {
mode: "ace/mode/python",
showGutter: false
}, function(highlighted) {
code.getElementsByClassName('ace_static_highlight')[0].style['font-size'] = "14px";
});
code.classList.add('has-highlight');
}

if (!$(solution_div).hasClass('collapsing')) {
if ($(solution_div).attr("expanded") === "true") {
solution_link.innerHTML = "Show scrambled author's solution";
$(solution_div).collapse('hide');
$(solution_div).attr("expanded", "false");
} else {
solution_link.innerHTML = "Hide scrambled author's solution";
$(solution_div).collapse('show');
$(solution_div).attr("expanded", "true");
}
}
}
var coderunner_all_links = document.getElementsByClassName('coderunner-scrambled-solution-link');

for (var i = 0; i < coderunner_all_links.length; i++) {
var el = coderunner_all_links[i];
if (!el.classList.contains('has-click-handler')) {
el.addEventListener('click', window.coderunner_solution_link_clicked);
el.classList.add('has-click-handler');
}
}
</script>

77 changes: 77 additions & 0 deletions python3_scratchpad/__languagetask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""The generic LanguageTask, subclasses of which manage compiling and executing
code in a particular language.
"""
from datetime import datetime

WATCHDOG_FREEBOARD = 1

class CompileError(Exception):
def __init__(self, error_message):
Exception.__init__(self, error_message)


class RunError(Exception):
def __init__(self, error_message=''):
Exception.__init__(self, error_message)

class LanguageTask:
def __init__(self, params, code=None):
"""Initialise the object, recording the parameters that will control compilation and
running plus the code if supplied. Code may be alternatively be supplied later by
calls to set_code.
self.params is the dictionary of template & global parameters - language specific.
"""
self.params = params
self.code = code
self.executable_built = False
self.compile_error_message = None
self.error_message_offset = 0
self.stderr = ''
self.stdout = ''
self.start_time = datetime.now()
self.timed_out = False
if 'totaltimeout' not in params:
self.params['totaltimeout'] = 30 # Secs

def seconds_remaining(self):
"""The number of seconds of execution time remaining before the watchdog timer goes off.
The watchdog timer goes off 1 second before runguard kills the job (as determined by the 'timeout' parameter).
"""
t_elapsed = (datetime.now() - self.start_time).total_seconds()
return self.params['totaltimeout'] - t_elapsed - WATCHDOG_FREEBOARD

def set_code(self, code, error_message_offset=0):
"""Set the code to be used for subsequent compiling and running. The optional error_message_offset
is a number to be subtracted from any error messages generated by compile and run_code calls.
Exactly how (or even 'if') it is used is language dependent.
"""
self.code = code
self.error_message_offset = error_message_offset

def compile(self, make_executable=False):
"""Compile the currently set code, either to an object file or
to an executable file depending on the given make_executable parameter.
Adjust any error message by subtracting error_message_offset.
Raise CompileError if the code does not
compile, with the compilation error message within the exception
and also recorded in self.compile_error_message.
No return value.
"""
raise NotImplementedError("compile not implemented by concrete class")

def discard_executable(self):
"""Called if something breaks in the executable and it will need rebuilding
(with different source, presumably)
"""
self.executable_built = False

def run_code(self, standard_input=None, bash_command=None):
"""Run the code in the executable program that a call to compile is assumed
to have created, using the given standard input.
If a bash_command is supplied it used as given.
Otherwise the command to be executed is the compiled executable.
Returns a tuple of the output from the
run and a stderr (or a derivative thereof) string. Those two values
are also recorded in self.stdout and self.stderr respectively.
"""
raise NotImplementedError("run_code not implemented by concrete class")
Loading

0 comments on commit 7967d3b

Please sign in to comment.