<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import types <EOL> try : <EOL> import numpy <EOL> HAS_NUMPY = True <EOL> except : <EOL> HAS_NUMPY = False <EOL> try : <EOL> import builtins <EOL> except ImportError : <EOL> import __builtin__ as builtins <EOL> memo = { } <EOL> id_to_obj = { } <EOL> builtins_types = set ( ( str , list , dict , set , frozenset , int ) ) <EOL> dont_memo = set ( id ( i ) for i in ( memo , sys . modules , sys . path_importer_cache , <EOL> os . environ , id_to_obj ) ) <EOL> def get_attrs ( obj ) : <EOL> """<STR_LIT>""" <EOL> if type ( obj ) in builtins_types or type ( obj ) is type and obj in builtins_types : <EOL> return <EOL> try : <EOL> return obj . __dict__ <EOL> except : <EOL> return <EOL> def get_seq ( obj , cache = { str : False , frozenset : False , list : True , set : True , <EOL> dict : True , tuple : True , type : False , <EOL> types . ModuleType : False , types . FunctionType : False , <EOL> types . BuiltinFunctionType : False } ) : <EOL> """<STR_LIT>""" <EOL> o_type = type ( obj ) <EOL> hsattr = hasattr <EOL> if o_type in cache : <EOL> if cache [ o_type ] : <EOL> if hsattr ( obj , "<STR_LIT>" ) : <EOL> return obj . copy ( ) <EOL> return obj <EOL> elif HAS_NUMPY and o_type in ( numpy . ndarray , numpy . ma . core . MaskedConstant ) : <EOL> if obj . shape and obj . size : <EOL> return obj <EOL> else : <EOL> return [ ] <EOL> elif hsattr ( obj , "<STR_LIT>" ) and hsattr ( obj , "<STR_LIT>" ) and hsattr ( obj , "<STR_LIT>" ) and hsattr ( o_type , "<STR_LIT>" ) and hsattr ( o_type , "<STR_LIT>" ) and hsattr ( o_type , "<STR_LIT>" ) : <EOL> cache [ o_type ] = True <EOL> if hsattr ( obj , "<STR_LIT>" ) : <EOL> return obj . copy ( ) <EOL> return obj <EOL> else : <EOL> cache [ o_type ] = False <EOL> return None <EOL> def memorise ( obj , force = False ) : <EOL> """<STR_LIT>""" <EOL> obj_id = id ( obj ) <EOL> if obj_id in memo and not force or obj_id in dont_memo : <EOL> return <EOL> id_ = id <EOL> g = get_attrs ( obj ) <EOL> if g is None : <EOL> attrs_id = None <EOL> else : <EOL> attrs_id = dict ( ( key , id_ ( value ) ) for key , value in g . items ( ) ) <EOL> s = get_seq ( obj ) <EOL> if s is None : <EOL> seq_id = None <EOL> elif hasattr ( s , "<STR_LIT>" ) : <EOL> seq_id = dict ( ( id_ ( key ) , id_ ( value ) ) for key , value in s . items ( ) ) <EOL> else : <EOL> seq_id = [ id_ ( i ) for i in s ] <EOL> memo [ obj_id ] = attrs_id , seq_id <EOL> id_to_obj [ obj_id ] = obj <EOL> mem = memorise <EOL> if g is not None : <EOL> [ mem ( value ) for key , value in g . items ( ) ] <EOL> if s is not None : <EOL> if hasattr ( s , "<STR_LIT>" ) : <EOL> [ ( mem ( key ) , mem ( item ) ) <EOL> for key , item in s . items ( ) ] <EOL> else : <EOL> [ mem ( item ) for item in s ] <EOL> def release_gone ( ) : <EOL> itop , mp , src = id_to_obj . pop , memo . pop , sys . getrefcount <EOL> [ ( itop ( id_ ) , mp ( id_ ) ) for id_ , obj in list ( id_to_obj . items ( ) ) <EOL> if src ( obj ) < <NUM_LIT:4> ] <EOL> def whats_changed ( obj , seen = None , simple = False , first = True ) : <EOL> """<STR_LIT>""" <EOL> if first : <EOL> if "<STR_LIT:_>" in builtins . __dict__ : <EOL> del builtins . _ <EOL> if seen is None : <EOL> seen = { } <EOL> obj_id = id ( obj ) <EOL> if obj_id in seen : <EOL> if simple : <EOL> return any ( seen [ obj_id ] ) <EOL> return seen [ obj_id ] <EOL> if obj_id in dont_memo : <EOL> seen [ obj_id ] = [ { } , False ] <EOL> if simple : <EOL> return False <EOL> return seen [ obj_id ] <EOL> elif obj_id not in memo : <EOL> if simple : <EOL> return True <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" + str ( obj ) ) <EOL> seen [ obj_id ] = ( { } , False ) <EOL> chngd = whats_changed <EOL> id_ = id <EOL> attrs = get_attrs ( obj ) <EOL> if attrs is None : <EOL> changed = { } <EOL> else : <EOL> obj_attrs = memo [ obj_id ] [ <NUM_LIT:0> ] <EOL> obj_get = obj_attrs . get <EOL> changed = dict ( ( key , None ) for key in obj_attrs if key not in attrs ) <EOL> for key , o in attrs . items ( ) : <EOL> if id_ ( o ) != obj_get ( key , None ) or chngd ( o , seen , True , False ) : <EOL> changed [ key ] = o <EOL> items = get_seq ( obj ) <EOL> seq_diff = False <EOL> if items is not None : <EOL> obj_seq = memo [ obj_id ] [ <NUM_LIT:1> ] <EOL> if len ( items ) != len ( obj_seq ) : <EOL> seq_diff = True <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> obj_get = obj_seq . get <EOL> for key , item in items . items ( ) : <EOL> if id_ ( item ) != obj_get ( id_ ( key ) ) or chngd ( key , seen , True , False ) or chngd ( item , seen , True , False ) : <EOL> seq_diff = True <EOL> break <EOL> else : <EOL> for i , j in zip ( items , obj_seq ) : <EOL> if id_ ( i ) != j or chngd ( i , seen , True , False ) : <EOL> seq_diff = True <EOL> break <EOL> seen [ obj_id ] = changed , seq_diff <EOL> if simple : <EOL> return changed or seq_diff <EOL> return changed , seq_diff <EOL> def has_changed ( * args , ** kwds ) : <EOL> kwds [ '<STR_LIT>' ] = True <EOL> return whats_changed ( * args , ** kwds ) <EOL> __import__ = __import__ <EOL> def _imp ( * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> before = set ( sys . modules . keys ( ) ) <EOL> mod = __import__ ( * args , ** kwds ) <EOL> after = set ( sys . modules . keys ( ) ) . difference ( before ) <EOL> for m in after : <EOL> memorise ( sys . modules [ m ] ) <EOL> return mod <EOL> builtins . __import__ = _imp <EOL> if hasattr ( builtins , "<STR_LIT:_>" ) : <EOL> del builtins . _ <EOL> for mod in sys . modules . values ( ) : <EOL> memorise ( mod ) <EOL> release_gone ( ) </s>
