<s> import hashlib <EOL> from collections import namedtuple <EOL> from stdnet . utils import to_bytes , JSPLITTER <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> lookup_value = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> orderinginfo = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> range_lookup_info = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class ModelDict ( dict ) : <EOL> def from_hash ( self , hash ) : <EOL> return self . get ( hash ) <EOL> def to_hash ( self , model ) : <EOL> return model . _meta . hash <EOL> _model_dict = ModelDict ( ) <EOL> def get_model_from_hash ( hash ) : <EOL> return _model_dict . from_hash ( hash ) <EOL> def get_hash_from_model ( model ) : <EOL> return _model_dict . to_hash ( model ) <EOL> def hashmodel ( model , library = None ) : <EOL> '''<STR_LIT>''' <EOL> library = library or '<STR_LIT>' <EOL> meta = model . _meta <EOL> sha = hashlib . sha1 ( to_bytes ( '<STR_LIT>' . format ( library , meta ) ) ) <EOL> hash = sha . hexdigest ( ) [ : <NUM_LIT:8> ] <EOL> meta . hash = hash <EOL> if hash in _model_dict : <EOL> raise KeyError ( '<STR_LIT>' . format ( meta ) ) <EOL> _model_dict [ hash ] = model <EOL> def _make_id ( target ) : <EOL> if hasattr ( target , '<STR_LIT>' ) : <EOL> return ( id ( target . __self__ ) , id ( target . __func__ ) ) <EOL> return id ( target ) <EOL> class Event : <EOL> def __init__ ( self ) : <EOL> self . callbacks = [ ] <EOL> def bind ( self , callback , sender = None ) : <EOL> '''<STR_LIT>''' <EOL> key = ( _make_id ( callback ) , _make_id ( sender ) ) <EOL> self . callbacks . append ( ( key , callback ) ) <EOL> def fire ( self , sender = None , ** params ) : <EOL> '''<STR_LIT>''' <EOL> keys = ( _make_id ( None ) , _make_id ( sender ) ) <EOL> results = [ ] <EOL> for ( _ , key ) , callback in self . callbacks : <EOL> if key in keys : <EOL> results . append ( callback ( self , sender , ** params ) ) <EOL> return results <EOL> def unbind ( self , callback , sender = None ) : <EOL> key = ( _make_id ( callback ) , _make_id ( sender ) ) <EOL> for index , key_cbk in enumerate ( self . callbacks ) : <EOL> if key == key_cbk [ <NUM_LIT:0> ] : <EOL> del self . callbacks [ index ] <EOL> break </s>
