<s> def itersplit ( p , it ) : <EOL> elems = [ ] <EOL> for x in it : <EOL> if p ( x ) and len ( elems ) > <NUM_LIT:0> : <EOL> yield elems <EOL> elems = [ ] <EOL> else : <EOL> elems . append ( x ) <EOL> if len ( elems ) > <NUM_LIT:0> : <EOL> yield elems <EOL> class Observable ( object ) : <EOL> def __init__ ( self ) : <EOL> self . listeners = [ ] <EOL> def add_listener ( self , listener ) : <EOL> self . listeners . append ( listener ) <EOL> def fire ( self , event , * args , ** kwargs ) : <EOL> for listener in self . listeners : <EOL> method = None <EOL> try : <EOL> method = getattr ( listener , "<STR_LIT>" + event ) <EOL> except AttributeError : <EOL> pass <EOL> if method : <EOL> method ( * args , ** kwargs ) <EOL> def linearized ( items ) : <EOL> result = [ ] <EOL> for item , subitems in items : <EOL> result . append ( item ) <EOL> result += linearized ( subitems ) <EOL> return result <EOL> class PledgerException ( Exception ) : <EOL> def __init__ ( self ) : <EOL> super ( PledgerException , self ) . __init__ ( ) <EOL> self . line_number = None <EOL> self . filename = None <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ , self . filename , self . line_number ) <EOL> class PrefixTree ( object ) : <EOL> def __init__ ( self , words = [ ] ) : <EOL> self . tree = { } <EOL> for word in words : <EOL> self . insert ( word ) <EOL> def insert_aux ( self , word , base , index ) : <EOL> c = word [ index : index + <NUM_LIT:1> ] <EOL> sub = base . get ( c ) <EOL> if sub is None : <EOL> base [ c ] = word <EOL> elif isinstance ( sub , dict ) : <EOL> self . insert_aux ( word , sub , index + <NUM_LIT:1> ) <EOL> elif sub != word : <EOL> base [ c ] = { sub [ index + <NUM_LIT:1> : index + <NUM_LIT:2> ] : sub } <EOL> self . insert_aux ( word , base , index ) <EOL> def insert ( self , word ) : <EOL> self . insert_aux ( word , self . tree , <NUM_LIT:0> ) <EOL> def from_prefix_aux ( self , prefix , base , index ) : <EOL> c = prefix [ index : index + <NUM_LIT:1> ] <EOL> if c == '<STR_LIT>' : <EOL> return self . all_words ( base ) <EOL> sub = base . get ( c ) <EOL> if sub is None : <EOL> return [ ] <EOL> elif isinstance ( sub , dict ) : <EOL> return self . from_prefix_aux ( prefix , sub , index + <NUM_LIT:1> ) <EOL> elif sub . startswith ( prefix ) : <EOL> return [ sub ] <EOL> else : <EOL> return [ ] <EOL> def from_prefix ( self , prefix ) : <EOL> return self . from_prefix_aux ( prefix , self . tree , <NUM_LIT:0> ) <EOL> def all_words ( self , base ) : <EOL> if isinstance ( base , dict ) : <EOL> result = [ ] <EOL> for sub in base . itervalues ( ) : <EOL> result += self . all_words ( sub ) <EOL> return result <EOL> else : <EOL> return [ base ] </s>
