<s> """<STR_LIT>""" <EOL> import copy <EOL> import operator <EOL> from core . platform import models <EOL> ( stats_models , ) = models . Registry . import_models ( [ models . NAMES . statistics ] ) <EOL> class StateRuleAnswerLog ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , answers ) : <EOL> self . answers = copy . deepcopy ( answers ) <EOL> @ property <EOL> def total_answer_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return sum ( self . answers . values ( ) ) <EOL> @ classmethod <EOL> def get_multi ( cls , exploration_id , rule_data ) : <EOL> """<STR_LIT>""" <EOL> answer_log_models = ( <EOL> stats_models . StateRuleAnswerLogModel . get_or_create_multi ( <EOL> exploration_id , rule_data ) ) <EOL> return [ cls ( answer_log_model . answers ) <EOL> for answer_log_model in answer_log_models ] <EOL> @ classmethod <EOL> def get ( cls , exploration_id , state_name , rule_str ) : <EOL> return cls . get_multi ( exploration_id , [ { <EOL> '<STR_LIT>' : state_name , <EOL> '<STR_LIT>' : rule_str <EOL> } ] ) [ <NUM_LIT:0> ] <EOL> def get_top_answers ( self , num_answers_to_return ) : <EOL> """<STR_LIT>""" <EOL> return sorted ( <EOL> self . answers . iteritems ( ) , key = operator . itemgetter ( <NUM_LIT:1> ) , <EOL> reverse = True ) [ : num_answers_to_return ] </s>
