<s> from __future__ import unicode_literals <EOL> import copy <EOL> class Choices ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * choices ) : <EOL> self . _triples = [ ] <EOL> self . _doubles = [ ] <EOL> self . _display_map = { } <EOL> self . _identifier_map = { } <EOL> self . _db_values = set ( ) <EOL> self . _process ( choices ) <EOL> def _store ( self , triple , triple_collector , double_collector ) : <EOL> self . _identifier_map [ triple [ <NUM_LIT:1> ] ] = triple [ <NUM_LIT:0> ] <EOL> self . _display_map [ triple [ <NUM_LIT:0> ] ] = triple [ <NUM_LIT:2> ] <EOL> self . _db_values . add ( triple [ <NUM_LIT:0> ] ) <EOL> triple_collector . append ( triple ) <EOL> double_collector . append ( ( triple [ <NUM_LIT:0> ] , triple [ <NUM_LIT:2> ] ) ) <EOL> def _process ( self , choices , triple_collector = None , double_collector = None ) : <EOL> if triple_collector is None : <EOL> triple_collector = self . _triples <EOL> if double_collector is None : <EOL> double_collector = self . _doubles <EOL> store = lambda c : self . _store ( c , triple_collector , double_collector ) <EOL> for choice in choices : <EOL> if isinstance ( choice , ( list , tuple ) ) : <EOL> if len ( choice ) == <NUM_LIT:3> : <EOL> store ( choice ) <EOL> elif len ( choice ) == <NUM_LIT:2> : <EOL> if isinstance ( choice [ <NUM_LIT:1> ] , ( list , tuple ) ) : <EOL> group_name = choice [ <NUM_LIT:0> ] <EOL> subchoices = choice [ <NUM_LIT:1> ] <EOL> tc = [ ] <EOL> triple_collector . append ( ( group_name , tc ) ) <EOL> dc = [ ] <EOL> double_collector . append ( ( group_name , dc ) ) <EOL> self . _process ( subchoices , tc , dc ) <EOL> else : <EOL> store ( ( choice [ <NUM_LIT:0> ] , choice [ <NUM_LIT:0> ] , choice [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> % len ( choice ) <EOL> ) <EOL> else : <EOL> store ( ( choice , choice , choice ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _doubles ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . _doubles ) <EOL> def __getattr__ ( self , attname ) : <EOL> try : <EOL> return self . _identifier_map [ attname ] <EOL> except KeyError : <EOL> raise AttributeError ( attname ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _display_map [ key ] <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> other = other . _triples <EOL> else : <EOL> other = list ( other ) <EOL> return Choices ( * ( self . _triples + other ) ) <EOL> def __radd__ ( self , other ) : <EOL> other = list ( other ) <EOL> return Choices ( * ( other + self . _triples ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _triples == other . _triples <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( ( "<STR_LIT:%s>" % repr ( i ) for i in self . _triples ) ) <EOL> ) <EOL> def __contains__ ( self , item ) : <EOL> return item in self . _db_values <EOL> def __deepcopy__ ( self , memo ) : <EOL> return self . __class__ ( * copy . deepcopy ( self . _triples , memo ) ) </s>
