lab2.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.append('/home/dotcloud/env/lib/python2.6/site-packages')
  4. from sandbox import Sandbox, SandboxConfig
  5. from os import path
  6. importname = sys.argv[1]
  7. if importname.endswith('.py'):
  8. importname = importname[:len(importname)-3]
  9. else:
  10. print >> sys.stderr, 'Filename did not end with .py for some reason...'
  11. sys.exit(2)
  12. sys.path.insert(0, path.expanduser('~/submissions'))
  13. sys.dont_write_bytecode = True
  14. output = ''
  15. def run():
  16. submission = __import__(importname)
  17. global output
  18. score = 0
  19. list1 = [-9, 20, 501]
  20. list2 = xrange(100)
  21. if submission.total(list1) == sum(list1):
  22. score += 1
  23. else:
  24. output += "Tried total(%s) and got %s\n" % (list1, submission.total(list1))
  25. if submission.total(list2) == sum(list2):
  26. score += 1
  27. else:
  28. output += "Tried total(%s) and got %s\n" % (list2, submission.total(list2))
  29. onestring1 = submission.onestring()
  30. onestring2 = submission.onestring()
  31. if onestring1 == 1:
  32. score += 1
  33. else:
  34. output += "First call to onestring() returned %s\n" % onestring1
  35. if onestring2 == 'ring':
  36. score += 1
  37. else:
  38. output += "Second call to onestring() returned %s\n" % onestring2
  39. return score
  40. config = SandboxConfig()
  41. config.timeout = 5
  42. config.allowModule(importname, 'total', 'onestring')
  43. sandbox = Sandbox(config)
  44. score = sandbox.call(run)
  45. print score
  46. print >> sys.stderr, output