A pure python way to compare the solution images
authoramit@thunder
Fri, 25 Jun 2010 03:59:57 +0530
changeset 5 548c6261fa00
parent 4 4d5422e5a45d
child 6 d0f107095b1a
A pure python way to compare the solution images
testappproj/.database.sqlite3
testappproj/testapp/views.py
testappproj/testapp/views.pyc
Binary file testappproj/.database.sqlite3 has changed
--- a/testappproj/testapp/views.py	Mon Jun 14 22:02:14 2010 +0530
+++ b/testappproj/testapp/views.py	Fri Jun 25 03:59:57 2010 +0530
@@ -7,6 +7,11 @@
 import StringIO
 import sys
 import json
+import time
+import sandbox
+import re
+from matplotlib.pyplot import imread
+from numpy import alltrue
 
 
 from django.contrib.auth.models import User 
@@ -29,10 +34,9 @@
 from models import Problem
 from django.contrib.auth.decorators import login_required 
 from models import Score 
-import time
-import sandbox
-import re
 from django.utils.datastructures import MultiValueDictKeyError
+
+
 def respond(request, user,template, params=None):
   """Helper to render a response, passing standard stuff to the response.
 
@@ -72,6 +76,8 @@
 
 
 def plot_change_code(user_code):
+  user_code=re.sub("show\(\)",'',user_code)
+
   return user_code
 
 
@@ -100,58 +106,71 @@
 
 
 def execute_plotting_test_cases(user_code , solution_image,problem_id , username):
-  
-  print  user_code 
-  print "solution"+solution_image
-
-  image_name = username+'_'+str(problem_id)+'_'+time.time()
-  
-  code="""from pylab import *
-  %s
-  show()"""%user_code
+   
 
   solved=False
+
+  if import_check(user_code):
+    print "entered"
+    return solved,None,True
+
+ 
+  user_image_name = str(username)+'_'+str(problem_id)+'_'+str(int(time.time()))+'.png'
+   
+  
+  user_code=plot_change_code(user_code)
+
+
+
+  code="""from pylab import *
+import pylab
+%s
+figure_conf=pylab.gcf()
+figure_conf.set_size_inches(10,10)
+figure_conf.set_dpi(80)
+figure_conf.savefig('%s')"""%(user_code,user_image_name)
+
+  
+  print code
   # create file-like string to capture output
   codeOut = StringIO.StringIO()
   codeErr = StringIO.StringIO()
 
-
+  print str(codeOut)
   
   # capture output and errors
   sys.stdout = codeOut
   sys.stderr = codeErr
 
+ 
   sandbox.execute(code)
 
+ 
+ 
 # restore stdout and stderr
   sys.stdout = sys.__stdout__
   sys.stderr = sys.__stderr__
 
 
-
-  
-  
-  s = codeOut.getvalue()
-  
-  s=unicode(s)
-  
+  new_solution_image='media-root/'+str((solution_image))
+  solution_image_matrix=imread(new_solution_image)
+  print str(solution_image_matrix)
+  print user_image_name
+  user_image_matrix=imread(user_image_name)
 
-  print s.strip()
-  print solution.strip()
-  
- 
-     
-  if solution.strip() == s.strip():
-    solved =True
+
 
-  errors=codeErr.getvalue()
-  
-  
+  if solution_image_matrix==user_image_matrix:
+    solved=True
+
+  errors=codeErr.getvalue()  
 
   codeOut.close()
   codeErr.close()
 
-  return solved,errors
+
+  print solved
+  return solved,errors,False
 
 def execute_test_cases(code , solution):
 
@@ -201,6 +220,9 @@
 
   errors=codeErr.getvalue()
   
+
+
+  
   
 
   codeOut.close()
@@ -274,7 +296,7 @@
 def run(request):
 
    user = request.user 
-   print user
+
    problem_id = request.POST.get('problem_id')
   
    if problem_id:
@@ -295,11 +317,18 @@
 
    errors = ''
   
-  
+   print problem.problem_type
 
 
-   solved,errors,import_test= execute_test_cases(user_code,problem.solution)
+   if unicode(problem.problem_type)==unicode('S'):
+     solved,errors,import_test= execute_test_cases(user_code,problem.solution)
+   else :
+     solved,errors,import_test= execute_plotting_test_cases(user_code,problem.solution_image,problem_id,user)
    
+
+
+   print solved
+
    if import_test==True:
      
      return http.HttpResponse("DO NOT try to import or your exam and account will invalidated")
@@ -325,7 +354,7 @@
 
 
    else:
-     http.HttpResponse('Wrong Answer')
+     return http.HttpResponse('Wrong Answer')
      
  
    return http.HttpResponse("Right Answer")
Binary file testappproj/testapp/views.pyc has changed