<s> class Proclaim ( object ) : <EOL> def __init__ ( self , redis ) : <EOL> self . redis = redis <EOL> self . groups = { "<STR_LIT:all>" : [ ] } <EOL> def activate_group ( self , feature , group ) : <EOL> if group in self . groups : <EOL> self . redis . sadd ( _group_key ( feature ) , group ) <EOL> def deactivate_group ( self , feature , group ) : <EOL> self . redis . srem ( _group_key ( feature ) , group ) <EOL> def deactivate_all ( self , feature ) : <EOL> self . redis . delete ( _group_key ( feature ) ) <EOL> self . redis . delete ( _user_key ( feature ) ) <EOL> self . redis . delete ( _percentage_key ( feature ) ) <EOL> def activate_user ( self , feature , user ) : <EOL> self . redis . sadd ( _user_key ( feature ) , user . id ) <EOL> def deactivate_user ( self , feature , user ) : <EOL> self . redis . srem ( _user_key ( feature ) , user . id ) <EOL> def define_group ( self , group , * users ) : <EOL> self . groups [ group ] = [ ] <EOL> for user in users : <EOL> self . groups [ group ] . append ( user . id ) <EOL> def is_active ( self , feature , user ) : <EOL> if self . _user_in_active_group ( feature , user ) : <EOL> return True <EOL> if self . _user_active ( feature , user ) : <EOL> return True <EOL> if self . _user_within_active_percentage ( feature , user ) : <EOL> return True <EOL> return False <EOL> def activate_percentage ( self , feature , percentage ) : <EOL> self . redis . set ( _percentage_key ( feature ) , percentage ) <EOL> def deactivate_percentage ( self , feature , percentage ) : <EOL> self . redis . delete ( _percentage_key ( feature ) , percentage ) <EOL> def _user_in_active_group ( self , feature , user ) : <EOL> if self . redis . exists ( _group_key ( feature ) ) : <EOL> active_groups = self . redis . smembers ( _group_key ( feature ) ) <EOL> if active_groups : <EOL> for grp in active_groups : <EOL> if user . id in self . groups [ grp ] : <EOL> return True <EOL> return False <EOL> def _user_active ( self , feature , user ) : <EOL> if self . redis . sismember ( _user_key ( feature ) , user . id ) : <EOL> return True <EOL> return False <EOL> def _user_within_active_percentage ( self , feature , user ) : <EOL> if self . redis . exists ( _percentage_key ( feature ) ) : <EOL> percentage = self . redis . get ( _percentage_key ( feature ) ) <EOL> if int ( user . id ) % <NUM_LIT:10> < int ( percentage ) / <NUM_LIT:10> : <EOL> return True <EOL> return False <EOL> def _key ( name ) : <EOL> return "<STR_LIT>" % name <EOL> def _group_key ( name ) : <EOL> return "<STR_LIT>" % ( _key ( name ) ) <EOL> def _user_key ( name ) : <EOL> return "<STR_LIT>" % ( _key ( name ) ) <EOL> def _percentage_key ( name ) : <EOL> return "<STR_LIT>" % ( _key ( name ) ) </s>
