<s> import unittest <EOL> from rdflib import events <EOL> class AddedEvent ( events . Event ) : pass <EOL> class RemovedEvent ( events . Event ) : pass <EOL> def subscribe_to ( source , target ) : <EOL> target . subscribe ( AddedEvent , source . _add_handler ) <EOL> target . subscribe ( RemovedEvent , source . _remove_handler ) <EOL> def subscribe_all ( caches ) : <EOL> for cache in caches : <EOL> for other in caches : <EOL> if other != cache : <EOL> subscribe_to ( cache , other ) <EOL> class Cache ( events . Dispatcher ) : <EOL> def __init__ ( self , data = None ) : <EOL> if data is None : data = { } <EOL> self . _data = data <EOL> self . subscribe ( AddedEvent , self . _add_handler ) <EOL> self . subscribe ( RemovedEvent , self . _remove_handler ) <EOL> def _add_handler ( self , event ) : <EOL> self . _data [ event . key ] = event . value <EOL> def _remove_handler ( self , event ) : <EOL> del self . _data [ event . key ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . _data [ key ] <EOL> def __setitem__ ( self , key , value ) : <EOL> self . dispatch ( AddedEvent ( key = key , value = value ) ) <EOL> def __delitem__ ( self , key ) : <EOL> self . dispatch ( RemovedEvent ( key = key ) ) <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _data <EOL> has_key = __contains__ <EOL> class EventTestCase ( unittest . TestCase ) : <EOL> def testEvents ( self ) : <EOL> c1 = Cache ( ) <EOL> c2 = Cache ( ) <EOL> c3 = Cache ( ) <EOL> subscribe_all ( [ c1 , c2 , c3 ] ) <EOL> c1 [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert c2 [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert c3 [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> del c3 [ '<STR_LIT>' ] <EOL> assert c1 . has_key ( '<STR_LIT>' ) == False <EOL> assert c2 . has_key ( '<STR_LIT>' ) == False <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
