<s> """<STR_LIT>""" <EOL> import UserDict <EOL> from _weakref import ( <EOL> getweakrefcount , <EOL> getweakrefs , <EOL> ref , <EOL> proxy , <EOL> CallableProxyType , <EOL> ProxyType , <EOL> ReferenceType ) <EOL> from _weakrefset import WeakSet , _IterationGuard <EOL> from exceptions import ReferenceError <EOL> ProxyTypes = ( ProxyType , CallableProxyType ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ] <EOL> class WeakValueDictionary ( UserDict . UserDict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> def remove ( wr , selfref = ref ( self ) ) : <EOL> self = selfref ( ) <EOL> if self is not None : <EOL> if self . _iterating : <EOL> self . _pending_removals . append ( wr ) <EOL> else : <EOL> if self . data . get ( wr . key ) is wr : <EOL> del self . data [ wr . key ] <EOL> self . _remove = remove <EOL> self . _pending_removals = [ ] <EOL> self . _iterating = set ( ) <EOL> UserDict . UserDict . __init__ ( self , * args , ** kw ) <EOL> def _commit_removals ( self ) : <EOL> l = self . _pending_removals <EOL> d = self . data <EOL> while l : <EOL> wr = l . pop ( ) <EOL> if d . get ( wr . key ) is wr : <EOL> del d [ wr . key ] <EOL> def __getitem__ ( self , key ) : <EOL> o = self . data [ key ] ( ) <EOL> if o is None : <EOL> raise KeyError , key <EOL> else : <EOL> return o <EOL> def __delitem__ ( self , key ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> del self . data [ key ] <EOL> def __contains__ ( self , key ) : <EOL> try : <EOL> o = self . data [ key ] ( ) <EOL> except KeyError : <EOL> return False <EOL> return o is not None <EOL> def has_key ( self , key ) : <EOL> try : <EOL> o = self . data [ key ] ( ) <EOL> except KeyError : <EOL> return False <EOL> return o is not None <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % id ( self ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> self . data [ key ] = KeyedRef ( value , self . _remove , key ) <EOL> def clear ( self ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> self . data . clear ( ) <EOL> def copy ( self ) : <EOL> new = WeakValueDictionary ( ) <EOL> for key , wr in self . data . items ( ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> new [ key ] = o <EOL> return new <EOL> __copy__ = copy <EOL> def __deepcopy__ ( self , memo ) : <EOL> from copy import deepcopy <EOL> new = self . __class__ ( ) <EOL> for key , wr in self . data . items ( ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> new [ deepcopy ( key , memo ) ] = o <EOL> return new <EOL> def get ( self , key , default = None ) : <EOL> try : <EOL> wr = self . data [ key ] <EOL> except KeyError : <EOL> return default <EOL> else : <EOL> o = wr ( ) <EOL> if o is None : <EOL> return default <EOL> else : <EOL> return o <EOL> def items ( self ) : <EOL> L = [ ] <EOL> for key , wr in self . data . items ( ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> L . append ( ( key , o ) ) <EOL> return L <EOL> def iteritems ( self ) : <EOL> with _IterationGuard ( self ) : <EOL> for wr in self . data . itervalues ( ) : <EOL> value = wr ( ) <EOL> if value is not None : <EOL> yield wr . key , value <EOL> def iterkeys ( self ) : <EOL> with _IterationGuard ( self ) : <EOL> for k in self . data . iterkeys ( ) : <EOL> yield k <EOL> __iter__ = iterkeys <EOL> def itervaluerefs ( self ) : <EOL> """<STR_LIT>""" <EOL> with _IterationGuard ( self ) : <EOL> for wr in self . data . itervalues ( ) : <EOL> yield wr <EOL> def itervalues ( self ) : <EOL> with _IterationGuard ( self ) : <EOL> for wr in self . data . itervalues ( ) : <EOL> obj = wr ( ) <EOL> if obj is not None : <EOL> yield obj <EOL> def popitem ( self ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> while <NUM_LIT:1> : <EOL> key , wr = self . data . popitem ( ) <EOL> o = wr ( ) <EOL> if o is not None : <EOL> return key , o <EOL> def pop ( self , key , * args ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> try : <EOL> o = self . data . pop ( key ) ( ) <EOL> except KeyError : <EOL> if args : <EOL> return args [ <NUM_LIT:0> ] <EOL> raise <EOL> if o is None : <EOL> raise KeyError , key <EOL> else : <EOL> return o <EOL> def setdefault ( self , key , default = None ) : <EOL> try : <EOL> wr = self . data [ key ] <EOL> except KeyError : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> self . data [ key ] = KeyedRef ( default , self . _remove , key ) <EOL> return default <EOL> else : <EOL> return wr ( ) <EOL> def update ( self , dict = None , ** kwargs ) : <EOL> if self . _pending_removals : <EOL> self . _commit_removals ( ) <EOL> d = self . data <EOL> if dict is not None : <EOL> if not hasattr ( dict , "<STR_LIT>" ) : <EOL> dict = type ( { } ) ( dict ) <EOL> for key , o in dict . items ( ) : <EOL> d [ key ] = KeyedRef ( o , self . _remove , key ) <EOL> if len ( kwargs ) : <EOL> self . update ( kwargs ) <EOL> def valuerefs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data . values ( ) <EOL> def values ( self ) : <EOL> L = [ ] <EOL> for wr in self . data . values ( ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> L . append ( o ) <EOL> return L <EOL> class KeyedRef ( ref ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = "<STR_LIT:key>" , <EOL> def __new__ ( type , ob , callback , key ) : <EOL> self = ref . __new__ ( type , ob , callback ) <EOL> self . key = key <EOL> return self <EOL> def __init__ ( self , ob , callback , key ) : <EOL> super ( KeyedRef , self ) . __init__ ( ob , callback ) <EOL> class WeakKeyDictionary ( UserDict . UserDict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict = None ) : <EOL> self . data = { } <EOL> self . refs = { } <EOL> def remove ( k , selfref = ref ( self ) ) : <EOL> self = selfref ( ) <EOL> if self is not None : <EOL> assert len ( self . data ) == len ( self . refs ) <EOL> if self . _iterating : <EOL> self . _pending_removals . append ( k ) <EOL> else : <EOL> if self . refs . get ( k ) is k : <EOL> del self . data [ k ] <EOL> del self . refs [ k ] <EOL> self . _remove = remove <EOL> self . _pending_removals = [ ] <EOL> self . _iterating = set ( ) <EOL> if dict is not None : <EOL> self . update ( dict ) <EOL> def _commit_removals ( self ) : <EOL> l = self . _pending_removals <EOL> d = self . data <EOL> r = self . refs <EOL> while l : <EOL> try : <EOL> k = l . pop ( ) <EOL> if self . refs . get ( k ) is k : <EOL> del d [ k ] <EOL> del r [ k ] <EOL> except KeyError : <EOL> pass <EOL> def __delitem__ ( self , key ) : <EOL> r = ref ( key ) <EOL> del self . data [ r ] <EOL> del self . refs [ r ] <EOL> def __getitem__ ( self , key ) : <EOL> return self . data [ ref ( key ) ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % id ( self ) <EOL> def __setitem__ ( self , key , value ) : <EOL> r = ref ( key , self . _remove ) <EOL> self . data . pop ( r , None ) <EOL> self . refs . pop ( r , None ) <EOL> self . data [ r ] = value <EOL> self . refs [ r ] = r <EOL> def copy ( self ) : <EOL> new = WeakKeyDictionary ( ) <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> __copy__ = copy <EOL> def __deepcopy__ ( self , memo ) : <EOL> from copy import deepcopy <EOL> new = self . __class__ ( ) <EOL> for key , value in self . data . items ( ) : <EOL> o = key ( ) <EOL> if o is not None : <EOL> new [ o ] = deepcopy ( value , memo ) <EOL> return new <EOL> def get ( self , key , default = None ) : <EOL> return self . data . get ( ref ( key ) , default ) <EOL> def has_key ( self , key ) : <EOL> try : <EOL> wr = ref ( key ) <EOL> except TypeError : <EOL> return <NUM_LIT:0> <EOL> return wr in self . data <EOL> def __contains__ ( self , key ) : <EOL> try : <EOL> wr = ref ( key ) <EOL> except TypeError : <EOL> return <NUM_LIT:0> <EOL> return wr in self . data <EOL> def items ( self ) : <EOL> L = [ ] <EOL> for key , value in 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> with _IterationGuard ( self ) : <EOL> for wr , value in self . data . iteritems ( ) : <EOL> key = wr ( ) <EOL> if key is not None : <EOL> yield key , value <EOL> def iterkeyrefs ( self ) : <EOL> """<STR_LIT>""" <EOL> with _IterationGuard ( self ) : <EOL> for wr in self . data . iterkeys ( ) : <EOL> yield wr <EOL> def iterkeys ( self ) : <EOL> with _IterationGuard ( self ) : <EOL> for wr in self . data . iterkeys ( ) : <EOL> obj = wr ( ) <EOL> if obj is not None : <EOL> yield obj <EOL> __iter__ = iterkeys <EOL> def itervalues ( self ) : <EOL> with _IterationGuard ( self ) : <EOL> for value in self . data . itervalues ( ) : <EOL> yield value <EOL> def keyrefs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data . keys ( ) <EOL> def keys ( self ) : <EOL> L = [ ] <EOL> for wr in self . data . keys ( ) : <EOL> o = wr ( ) <EOL> if o is not None : <EOL> L . append ( o ) <EOL> return L <EOL> def popitem ( self ) : <EOL> while <NUM_LIT:1> : <EOL> _ , key = self . refs . popitem ( ) <EOL> value = self . data . pop ( key ) <EOL> o = key ( ) <EOL> if o is not None : <EOL> return o , value <EOL> def pop ( self , key , * args ) : <EOL> r = ref ( key ) <EOL> self . refs . pop ( r , None ) <EOL> return self . data . pop ( r , * args ) <EOL> def setdefault ( self , key , default = None ) : <EOL> if key not in self : <EOL> self [ key ] = default <EOL> return default <EOL> return self [ key ] <EOL> def update ( self , dict = None , ** kwargs ) : <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> self [ key ] = value <EOL> if len ( kwargs ) : <EOL> self . update ( kwargs ) </s>
