<s> '''<STR_LIT>''' <EOL> import weakref <EOL> import collections <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class IdentityRef ( weakref . ref ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , thing , callback = None ) : <EOL> weakref . ref . __init__ ( self , thing , callback ) <EOL> self . _hash = id ( thing ) <EOL> def __hash__ ( self ) : <EOL> return self . _hash <EOL> class WeakKeyIdentityDict ( collections . MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict_ = None ) : <EOL> self . data = { } <EOL> def remove ( k , selfref = weakref . ref ( self ) ) : <EOL> self = selfref ( ) <EOL> if self is not None : <EOL> del self . data [ k ] <EOL> self . _remove = remove <EOL> if dict_ is not None : self . update ( dict_ ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . data [ IdentityRef ( key ) ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . data [ IdentityRef ( key ) ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % id ( self ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . data [ IdentityRef ( key , self . _remove ) ] = value <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> new = WeakKeyIdentityDict ( ) <EOL> for key , value in self . data . items ( ) : <EOL> o = key ( ) <EOL> if o is not None : <EOL> new [ o ] = value <EOL> return new <EOL> def get ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . data . get ( IdentityRef ( key ) , default ) <EOL> def __contains__ ( self , key ) : <EOL> try : <EOL> wr = IdentityRef ( key ) <EOL> except TypeError : <EOL> return <NUM_LIT:0> <EOL> return wr in self . data <EOL> has_key = __contains__ <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> L = [ ] <EOL> for key , value in list ( self . data . items ( ) ) : <EOL> o = key ( ) <EOL> if o is not None : <EOL> L . append ( ( o , value ) ) <EOL> return L <EOL> def iteritems ( self ) : <EOL> """<STR_LIT>""" <EOL> for wr , value in self . data . items ( ) : <EOL> key = wr ( ) <EOL> if key is not None : <EOL> yield key , value <EOL> def iterkeyrefs ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . data . keys ( ) ) <EOL> def iterkeys ( self ) : <EOL> """<STR_LIT>""" <EOL> for wr in self . data . keys ( ) : <EOL> obj = wr ( ) <EOL> if obj is not None : <EOL> yield obj <EOL> def __iter__ ( self ) : <EOL> return iter ( self . keys ( ) ) <EOL> def itervalues ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . data . values ( ) ) <EOL> def keyrefs ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . data . keys ( ) ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> L = [ ] <EOL> for wr in list ( self . data . keys ( ) ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> L . append ( o ) <EOL> return L <EOL> def popitem ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> key , value = self . data . popitem ( ) <EOL> o = key ( ) <EOL> if o is not None : <EOL> return o , value <EOL> def pop ( self , key , * args ) : <EOL> """<STR_LIT>""" <EOL> return self . data . pop ( IdentityRef ( key ) , * args ) <EOL> def setdefault ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> return self . data . setdefault ( IdentityRef ( key , self . _remove ) , default ) <EOL> def update ( self , dict = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> d = self . data <EOL> if dict is not None : <EOL> if not hasattr ( dict , "<STR_LIT>" ) : <EOL> dict = type ( { } ) ( dict ) <EOL> for key , value in dict . items ( ) : <EOL> d [ IdentityRef ( key , self . _remove ) ] = value <EOL> if len ( kwargs ) : <EOL> self . update ( kwargs ) <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) </s>
