diff options
-rwxr-xr-x | graders/lab2.py | 53 | ||||
-rw-r--r-- | static/lab2.py | 32 |
2 files changed, 85 insertions, 0 deletions
diff --git a/graders/lab2.py b/graders/lab2.py new file mode 100755 index 0000000..fa4c056 --- /dev/null +++ b/graders/lab2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import sys +sys.path.append('/home/dotcloud/env/lib/python2.6/site-packages') +from sandbox import Sandbox, SandboxConfig +from os import path + +importname = sys.argv[1] +if importname.endswith('.py'): + importname = importname[:len(importname)-3] +else: + print >> sys.stderr, 'Filename did not end with .py for some reason...' + sys.exit(2) + +sys.path.insert(0, path.expanduser('~/submissions')) +sys.dont_write_bytecode = True + +output = '' +def run(): + exec("import %s as submission" % importname) + global output + score = 0 + + list1 = [-9, 20, 501] + list2 = xrange(100) + if submission.total(list1) == sum(list1): + score += 1 + else: + output += "Tried total(%s) and got %s\n" % (list1, submission.total(list1)) + if submission.total(list2) == sum(list2): + score += 1 + else: + output += "Tried total(%s) and got %s\n" % (list2, submission.total(list2)) + + onestring1 = submission.onestring() + onestring2 = submission.onestring() + if onestring1 == 1: + score += 1 + else: + output += "First call to onestring() returned %s\n" % onestring1 + if onestring2 == 'ring': + score += 1 + else: + output += "Second call to onestring() returned %s\n" % onestring2 + return score + +config = SandboxConfig() +config.timeout = 5 +config.allowModule(importname, 'total', 'onestring') +sandbox = Sandbox(config) +score = sandbox.call(run) +print score +print >> sys.stderr, output diff --git a/static/lab2.py b/static/lab2.py new file mode 100644 index 0000000..a7dd654 --- /dev/null +++ b/static/lab2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +# Normally, there won't be two assignments overlapping, but we're still testing +# so whatever. + +# total should return the sum of the numbers passed in +def total(numbers): + return numbers[0] + +# onestring should return 1 the first time it's called and 'ring' the second time +def onestring(): + return 1 + +# Your code will be run in a sandbox, so print statements are not allowed in +# the required functions. The below code gets executed when you run the python +# file; this should aid you in debugging/testing. +if __name__ == '__main__': + list1 = [1, 2, 3] + list2 = [-10, -10, -30] + print 'sum of list1:', total(list1) + print 'sum of list2:', total(list2) + + print # blank line + + if onestring() == 1: + print 'got 1 the first time!' + else: + print 'onestring didn\'t return 1 the first time :(' + if onestring() == 'ring': + print "got 'ring' the second time!" + else: + print "onestring didn't print 'ring' the second time D:" |