<s> '''<STR_LIT>''' <EOL> import json <EOL> from . . helpers import random_str <EOL> def beta_estimate ( m , N , a_prior = <NUM_LIT:1.0> , b_prior = <NUM_LIT:1.0> ) : <EOL> numerator = alpha = m + a_prior <EOL> l = N - m <EOL> denominator = m + a_prior + l + b_prior <EOL> beta = denominator - alpha <EOL> p_success = float ( numerator ) / float ( denominator ) <EOL> return ( alpha , beta , p_success ) <EOL> class ScorableThingError ( Exception ) : <EOL> pass <EOL> class ScorableThing ( object ) : <EOL> def __init__ ( self , key = None , a = None , b = None , uniques_only = True ) : <EOL> '''<STR_LIT>''' <EOL> if not key : <EOL> self . key = '<STR_LIT>' + random_str ( <NUM_LIT:8> ) <EOL> else : <EOL> self . key = key <EOL> self . a = a or <NUM_LIT:1> <EOL> self . b = b or <NUM_LIT:1> <EOL> self . uniques_only = uniques_only <EOL> self . seen = { } <EOL> self . successes = <NUM_LIT:0> <EOL> self . tries = <NUM_LIT:0> <EOL> self . probability = beta_estimate ( <NUM_LIT:0> , <NUM_LIT:0> , self . a , self . b ) [ <NUM_LIT:2> ] <EOL> def __repr__ ( self ) : <EOL> return self . key <EOL> def record_failure ( self , tries = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> self . record_tries ( tries ) <EOL> def record_tries ( self , tries = <NUM_LIT:0> ) : <EOL> self . record_result ( <NUM_LIT:0> , tries ) <EOL> def record_success ( self , key , tries = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> is_new = False <EOL> try : <EOL> self . seen [ key ] += <NUM_LIT:1> <EOL> except KeyError : <EOL> self . seen [ key ] = <NUM_LIT:1> <EOL> is_new = True <EOL> if is_new or not self . uniques_only : <EOL> self . record_result ( <NUM_LIT:1> , tries ) <EOL> else : <EOL> self . record_failure ( tries ) <EOL> def record_result ( self , successes = <NUM_LIT:0> , tries = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> self . successes += successes <EOL> self . tries += tries <EOL> self . update ( m = successes , N = tries ) <EOL> def update ( self , m = <NUM_LIT:0> , N = <NUM_LIT:0> ) : <EOL> a , b , p = beta_estimate ( m , N , self . a , self . b ) <EOL> self . a = a <EOL> self . b = b <EOL> self . probability = p <EOL> def doubt ( self , factor = None ) : <EOL> '''<STR_LIT>''' <EOL> import math <EOL> if not factor : <EOL> _factor = float ( self . a ) <EOL> else : <EOL> _factor = float ( min ( ( factor , self . a ) ) ) <EOL> self . a = int ( math . ceil ( self . a / _factor ) ) <EOL> self . b = int ( math . ceil ( self . b / _factor ) ) <EOL> def __getstate__ ( self ) : <EOL> return self . __dict__ . copy ( ) <EOL> def to_json ( self , sort_keys = True , indent = None ) : <EOL> state = self . __getstate__ ( ) <EOL> return json . dumps ( state , sort_keys = sort_keys , indent = indent ) </s>
