<s> from . compat import zip_longest , string_types , next <EOL> from . backend import CleaverBackend <EOL> from . identity import CleaverIdentityProvider <EOL> from cleaver import util <EOL> class Cleaver ( object ) : <EOL> def __init__ ( self , environ , identity , backend , <EOL> count_humans_only = False , <EOL> human_callback_token = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( identity , CleaverIdentityProvider ) and not callable ( identity ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % identity <EOL> ) <EOL> if not isinstance ( backend , CleaverBackend ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' % backend <EOL> ) <EOL> self . _identity = identity <EOL> self . _backend = backend <EOL> self . _environ = environ <EOL> self . count_humans_only = count_humans_only <EOL> self . human_callback_token = human_callback_token <EOL> def __call__ ( self , * args ) : <EOL> return self . split ( * args ) <EOL> @ property <EOL> def identity ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . _identity , '<STR_LIT>' ) : <EOL> return self . _identity . get_identity ( self . _environ ) <EOL> return self . _identity ( self . _environ ) <EOL> @ property <EOL> def human ( self ) : <EOL> return self . _backend . is_verified_human ( self . identity ) <EOL> def split ( self , experiment_name , * variants ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( experiment_name , string_types ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' % <EOL> experiment_name <EOL> ) <EOL> keys , values , weights = self . _parse_variants ( variants ) <EOL> b = self . _backend <EOL> experiment = b . get_experiment ( experiment_name , keys ) <EOL> if self . count_humans_only is False and self . human is not True : <EOL> b . mark_human ( self . identity ) <EOL> if experiment is None : <EOL> b . save_experiment ( experiment_name , keys ) <EOL> experiment = b . get_experiment ( experiment_name , keys ) <EOL> else : <EOL> if set ( experiment . variants ) != set ( keys ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % experiment_name <EOL> ) <EOL> if experiment . name in self . _environ . get ( '<STR_LIT>' , { } ) : <EOL> variant = self . _environ [ '<STR_LIT>' ] [ experiment . name ] <EOL> else : <EOL> variant = b . get_variant ( self . identity , experiment . name ) <EOL> if variant is None : <EOL> variant = next ( util . random_variant ( keys , weights ) ) <EOL> b . participate ( self . identity , experiment . name , variant ) <EOL> return dict ( zip ( keys , values ) ) [ variant ] <EOL> def score ( self , experiment_name ) : <EOL> """<STR_LIT>""" <EOL> if self . _backend . get_variant ( self . identity , experiment_name ) and self . human is True : <EOL> self . _backend . mark_conversion ( <EOL> experiment_name , <EOL> self . _backend . get_variant ( self . identity , experiment_name ) <EOL> ) <EOL> def _parse_variants ( self , variants ) : <EOL> if not len ( variants ) : <EOL> variants = [ ( '<STR_LIT:True>' , True ) , ( '<STR_LIT:False>' , False ) ] <EOL> if len ( variants ) == <NUM_LIT:1> : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def add_defaults ( v ) : <EOL> if len ( v ) < <NUM_LIT:3> : <EOL> v = tuple ( <EOL> list ( v ) + ( <EOL> [ None , <NUM_LIT:1> ] if len ( v ) == <NUM_LIT:1> else [ <NUM_LIT:1> ] <EOL> ) <EOL> ) <EOL> if not isinstance ( v [ <NUM_LIT:0> ] , string_types ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' % <EOL> v [ <NUM_LIT:0> ] <EOL> ) <EOL> if not isinstance ( v [ <NUM_LIT:2> ] , int ) : <EOL> raise RuntimeError ( <EOL> '<STR_LIT>' % <EOL> v [ <NUM_LIT:2> ] <EOL> ) <EOL> return v <EOL> variants = map ( <EOL> add_defaults , <EOL> variants <EOL> ) <EOL> return zip_longest ( * variants ) <EOL> def humanizing_javascript ( self ) : <EOL> if self . human : <EOL> return '<STR_LIT>' <EOL> return """<STR_LIT>""" % ( self . human_callback_token , "<STR_LIT>" ) </s>
