<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from builtins import object <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> from django . utils . functional import Promise <EOL> class ChoiceAttributeMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( cls , Promise ) : <EOL> return super ( ChoiceAttributeMixin , cls ) . __new__ ( cls ) <EOL> return super ( ChoiceAttributeMixin , cls ) . __new__ ( cls , * args [ : <NUM_LIT:1> ] ) <EOL> def __init__ ( self , value , choice_entry ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self , Promise ) : <EOL> super ( ChoiceAttributeMixin , self ) . __init__ ( value . _proxy____args , value . _proxy____kw ) <EOL> else : <EOL> super ( ChoiceAttributeMixin , self ) . __init__ ( ) <EOL> self . original_value = value <EOL> self . choice_entry = choice_entry <EOL> @ property <EOL> def constant ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . choice_entry . constant <EOL> @ property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . choice_entry . value <EOL> @ property <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . choice_entry . display <EOL> @ classmethod <EOL> def get_class_for_value ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> type_ = value . __class__ <EOL> if issubclass ( type_ , ChoiceAttributeMixin ) : <EOL> return type_ <EOL> if type_ not in cls . _classes_by_type : <EOL> class_name = str ( '<STR_LIT>' % type_ . __name__ . capitalize ( ) ) <EOL> cls . _classes_by_type [ type_ ] = type ( class_name , ( cls , type_ ) , { <EOL> '<STR_LIT>' : cls , <EOL> } ) <EOL> return cls . _classes_by_type [ type_ ] <EOL> def __reduce__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> create_choice_attribute , <EOL> ( <EOL> self . creator_type , <EOL> self . original_value , <EOL> self . choice_entry <EOL> ) <EOL> ) <EOL> def __bool__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . original_value ) <EOL> _classes_by_type = { } <EOL> def create_choice_attribute ( creator_type , value , choice_entry ) : <EOL> """<STR_LIT>""" <EOL> klass = creator_type . get_class_for_value ( value ) <EOL> return klass ( value , choice_entry ) <EOL> class ChoiceEntry ( tuple ) : <EOL> """<STR_LIT>""" <EOL> ChoiceAttributeMixin = ChoiceAttributeMixin <EOL> def __new__ ( cls , tuple_ ) : <EOL> """<STR_LIT>""" <EOL> assert <NUM_LIT:3> <= len ( tuple_ ) <= <NUM_LIT:4> , '<STR_LIT>' % ( tuple_ , ) <EOL> obj = super ( ChoiceEntry , cls ) . __new__ ( cls , tuple_ [ : <NUM_LIT:3> ] ) <EOL> obj . constant = obj . _get_choice_attribute ( tuple_ [ <NUM_LIT:0> ] ) <EOL> obj . value = obj . _get_choice_attribute ( tuple_ [ <NUM_LIT:1> ] ) <EOL> obj . display = obj . _get_choice_attribute ( tuple_ [ <NUM_LIT:2> ] ) <EOL> obj . choice = ( obj . value , obj . display ) <EOL> if len ( tuple_ ) == <NUM_LIT:4> : <EOL> for key , value in tuple_ [ <NUM_LIT:3> ] . items ( ) : <EOL> setattr ( obj , key , value ) <EOL> return obj <EOL> def _get_choice_attribute ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return create_choice_attribute ( self . ChoiceAttributeMixin , value , self ) </s>
