<s> """<STR_LIT>""" <EOL> import abc <EOL> from pyalgotrade import dispatchprio <EOL> class Event ( object ) : <EOL> def __init__ ( self ) : <EOL> self . __handlers = [ ] <EOL> self . __toSubscribe = [ ] <EOL> self . __toUnsubscribe = [ ] <EOL> self . __emitting = False <EOL> def __applyChanges ( self ) : <EOL> if len ( self . __toSubscribe ) : <EOL> for handler in self . __toSubscribe : <EOL> if handler not in self . __handlers : <EOL> self . __handlers . append ( handler ) <EOL> self . __toSubscribe = [ ] <EOL> if len ( self . __toUnsubscribe ) : <EOL> for handler in self . __toUnsubscribe : <EOL> self . __handlers . remove ( handler ) <EOL> self . __toUnsubscribe = [ ] <EOL> def subscribe ( self , handler ) : <EOL> if self . __emitting : <EOL> self . __toSubscribe . append ( handler ) <EOL> elif handler not in self . __handlers : <EOL> self . __handlers . append ( handler ) <EOL> def unsubscribe ( self , handler ) : <EOL> if self . __emitting : <EOL> self . __toUnsubscribe . append ( handler ) <EOL> else : <EOL> self . __handlers . remove ( handler ) <EOL> def emit ( self , * args , ** kwargs ) : <EOL> try : <EOL> self . __emitting = True <EOL> for handler in self . __handlers : <EOL> handler ( * args , ** kwargs ) <EOL> finally : <EOL> self . __emitting = False <EOL> self . __applyChanges ( ) <EOL> class Subject ( object ) : <EOL> __metaclass__ = abc . ABCMeta <EOL> def __init__ ( self ) : <EOL> self . __dispatchPrio = dispatchprio . LAST <EOL> @ abc . abstractmethod <EOL> def start ( self ) : <EOL> pass <EOL> @ abc . abstractmethod <EOL> def stop ( self ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def join ( self ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def eof ( self ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def dispatch ( self ) : <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def peekDateTime ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def getDispatchPriority ( self ) : <EOL> return self . __dispatchPrio <EOL> def setDispatchPriority ( self , dispatchPrio ) : <EOL> self . __dispatchPrio = dispatchPrio <EOL> def onDispatcherRegistered ( self , dispatcher ) : <EOL> pass </s>
