<s> import weakref <EOL> from six . moves import intern <EOL> dict_name = intern ( '<STR_LIT>' ) <EOL> class Internable ( object ) : <EOL> """<STR_LIT>""" <EOL> class Stats ( object ) : <EOL> def __init__ ( self ) : <EOL> self . d = { } <EOL> def incr ( self , name ) : <EOL> self . d [ name ] = self . d . get ( name , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> def __repr__ ( self ) : <EOL> return repr ( self . d ) <EOL> def __str__ ( self ) : <EOL> return str ( self . d ) <EOL> @ classmethod <EOL> def _internable_init ( kls ) : <EOL> kls . _internable_dict = weakref . WeakKeyDictionary ( ) <EOL> kls . _internable_stats = Internable . Stats ( ) <EOL> @ classmethod <EOL> def intern_stats ( kls ) : <EOL> return kls . _internable_stats <EOL> def intern ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _internable_stats . incr ( '<STR_LIT>' ) <EOL> kls = self . __class__ <EOL> if not hasattr ( kls , dict_name ) : <EOL> kls . _internable_init ( ) <EOL> obj = kls . _internable_dict . get ( self ) <EOL> if ( obj ) : <EOL> kls . _internable_stats . incr ( '<STR_LIT>' ) <EOL> return obj <EOL> def object_collected ( obj ) : <EOL> kls . _internable_stats . incr ( '<STR_LIT>' ) <EOL> pass <EOL> ref = weakref . ref ( self , object_collected ) <EOL> kls . _internable_dict [ self ] = ref <EOL> self . _interned = True <EOL> kls . _internable_stats . incr ( '<STR_LIT>' ) <EOL> return self </s>
