
/Qc           @   s   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l Z d d d     YZ	 d e
 f d     YZ d S(	   s   Common code for autogradersiN(   t   defaultdictt   Gradesc           B   s   e  Z d  Z e e d  Z d   Z i  d  Z d   Z d   Z d   Z	 e d  Z
 d   Z d	   Z d
   Z d e d  Z e d  Z d   Z RS(   sO   A data structure for project grades, along with formatting code to display themc         C   s   g  | D] } | d ^ q |  _  t |  |  _ t   |  _ t g  |  j  D] } | g  f ^ qH  |  _ | |  _ t j   d d !|  _	 t
 |  _ d |  _ | |  _ | |  _ t t  |  _ d |  j	 GHd S(   s   
    Defines the grading scheme for a project
      projectName: project name
      questionsAndMaxesDict: a list of (question name, max points per question)
    i    i   i   s!   Starting on %d-%d at %d:%02d:%02dN(   t	   questionst   dictt   maxest   Countert   pointst   messagest   projectt   timet	   localtimet   startt   Truet   sanet   Nonet   currentQuestiont	   edxOutputt   muteR    t   sett   prereqs(   t   selft   projectNamet   questionsAndMaxesListR   t
   muteOutputt   elt   q(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   __init__   s     +					c         C   s   |  j  | j |  d  S(   N(   R   t   add(   R   t   questiont   prereq(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt	   addPrereq,   s    c         C   s#  t  g   } xz|  j D]o} d | GHd d t |  GHH| |  _ |  j | j |  } t |  d k r | j   } d | | | | f GHq n  |  j r t j	   n  zz y# t j
 t | |  d  |   WnP t k
 r} |  j | | t  |  j | | | d  n |  j d  n XWd	 |  j r:t j   n  X|  j | |  j | k re| j |  n  d
 | |  j | |  j | f GHq Wd t j   d d !GHd GHx1 |  j D]& } d | |  j | |  j | f GHqWd GHd |  j j   t |  j j    f GHd GH|  j r|  j   n  d	 S(   s   
    Grades each question
      gradingModule: the module with all the grading functions (pass in with sys.modules[__name__])
    s   
Question %st   =i	   i    s   *** NOTE: Make sure to complete Question %s before working on Question %s,
*** because Question %s builds upon your answer for Question %s.
i,  i   s)   FAIL: Terminated with a string exception.Ns   
### Question %s: %d/%d ###
s   
Finished at %d:%02d:%02di   i   s&   
Provisional grades
==================s   Question %s: %d/%ds   ------------------s   Total: %d/%ds  
Your grades are NOT yet registered.  To register your grades you must
submit your files to the edX website.  The grades obtained through the
edX website are your final grades unless your submission was not in
the spirit of the course,  such as if your submission simply hardcoded
the answers to the tests.   We will screen for this after the deadline.

*If you worked with a partner, you must both submit separately.*
(   R   R   t   lenR   R   t
   differencet   popR   t   utilt	   mutePrintt   TimeoutFunctiont   getattrt	   Exceptiont   addExceptionMessaget	   tracebackt   addErrorHintst   failt   unmutePrintR   R   R   R	   R
   t
   totalCountt   sumt   valuesR   t   produceOutput(   R   t   gradingModulet   exceptionMapt   completedQuestionsR   t   incompletedR   t   inst(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   grade/   sF    			  #	 $$'		c         C   sO   |  j  d |  |  j d  x* | j   j d  D] } |  j |  q4 Wd S(   s   
    Method to format the exception message, this is more complicated because
    we need to cgi.escape the traceback but wrap the exception in a <pre> tag
    s   FAIL: Exception raised: %st    s   
N(   R+   t
   addMessaget
   format_exct   split(   R   R   R5   R)   t   line(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR(   k   s    c   	      C   s   t  t |   } d | } d } | j |  rd | j |  } | j |  rd | j |  } qd n  | j |  r | j |  } n  | s d Sx$ | j d  D] } |  j |  q Wd  S(   NR   R7   s   
(   t   strt   typet   getR:   R8   (	   R   R2   t   errorInstancet   questionNumt   typeOft   questionNamet	   errorHintt   questionMapR;   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR*   u   s    
c   
      C   s  t  d d  } | j d  t |  j j    } t |  j j    } d } | | k ra d } n  d j d | d | d	 |  } | j |  x |  j D] } t |  d
 k r | d } n | } d } |  j | |  j | k r d } n  d d j	 |  j
 |  } d j d | d |  j | d | d	 | d |  j |  }	 | j |	  q W| j d  | j   t  d d  } | j t |  j j     | j   d  S(   Ns   edx_response.htmlt   ws   <div>s   <span class="incorrect"/>s   <span class="correct"/>s[   
        <h3>
            Total score ({total_score} / {total_possible})
        </h3>
    t   total_scoret   total_possiblet   checkOrXi   i   s   <pre>%s</pre>s   
s  
        <div class="test">
          <section>
          <div class="shortform">
            Question {q} ({points}/{max}) {checkOrX}
          </div>
        <div class="longform">
          {messages}
        </div>
        </section>
      </div>
      R   t   maxR   R   s   </div>t	   edx_grade(   t   opent   writeR.   R   R/   R   t   formatR   R    t   joinR   t   closeR<   R-   (
   R   R   RG   RF   RH   t   headerR   t   nameR   t   output(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR0      s<    			
c         C   s'   t  |  _ |  j   |  j | |  d S(   s4   Sets sanity check bit to false and outputs a messageN(   t   FalseR   t   assignZeroCreditR8   (   R   t   messaget   raw(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR+      s    	
c         C   s   d |  j  |  j <d  S(   Ni    (   R   R   (   R   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyRT      s    c         C   s   |  j  |  j c | 7<d  S(   N(   R   R   (   R   t   amt(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt	   addPoints   s    c         C   s   |  j  |  j c | 8<d  S(   N(   R   R   (   R   RW   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   deductPoints   s    R7   c         C   s=   |  j  |  j |  j |  j <| d k r9 |  j | |  n  d  S(   NR7   (   R   R   R   R8   (   R   RU   RV   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   assignFullCredit   s    c         C   sh   | sM |  j  r t j   n  d | GH|  j  r; t j   n  t j |  } n  |  j |  j j |  d  S(   Ns   *** (	   R   R#   R,   R$   t   cgit   escapeR   R   t   append(   R   RU   RV   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR8      s    	 		 c         C   s'   d | GHx | j  d  D] } q Wd  S(   Ns.   WARNING**** addMessageToEmail is deprecated %ss   
(   R:   (   R   RU   R;   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   addMessageToEmail   s    	(   t   __name__t
   __module__t   __doc__RS   R   R   R6   R(   R*   R0   R+   RT   RX   RY   RZ   R8   R^   (    (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR      s   	<	
		8				R   c           B   s    e  Z d  Z d   Z d   Z RS(   s   
  Dict with default 0
  c         C   s-   y t  j |  |  SWn t k
 r( d SXd  S(   Ni    (   R   t   __getitem__t   KeyError(   R   t   idx(    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyRb      s    c         C   s   t  |  j    S(   s1   
    Returns the sum of counts for all keys.
    (   R.   R/   (   R   (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR-      s    (   R_   R`   Ra   Rb   R-   (    (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyR      s   	(    (   Ra   R[   R	   t   sysR)   t   pdbt   collectionsR    R#   R   R   R   (    (    (    s_   H:\My Documents\Nicholas\Studies\02 Berkeleyx cs188x\05 Projects\01 Project 1\search\grading.pyt   <module>   s   