<s> """<STR_LIT>""" <EOL> import inspect <EOL> import operator <EOL> import weakref <EOL> from . . sql import expression <EOL> from . . import util , exc as sa_exc <EOL> from . import base <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> __instrumentation_mutex = util . threading . Lock ( ) <EOL> class _PlainColumnGetter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cols ) : <EOL> self . cols = cols <EOL> self . composite = len ( cols ) > <NUM_LIT:1> <EOL> def __reduce__ ( self ) : <EOL> return _SerializableColumnGetterV2 . _reduce_from_cols ( self . cols ) <EOL> def _cols ( self , mapper ) : <EOL> return self . cols <EOL> def __call__ ( self , value ) : <EOL> state = base . instance_state ( value ) <EOL> m = base . _state_mapper ( state ) <EOL> key = [ <EOL> m . _get_state_attr_by_column ( state , state . dict , col ) <EOL> for col in self . _cols ( m ) <EOL> ] <EOL> if self . composite : <EOL> return tuple ( key ) <EOL> else : <EOL> return key [ <NUM_LIT:0> ] <EOL> class _SerializableColumnGetter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colkeys ) : <EOL> self . colkeys = colkeys <EOL> self . composite = len ( colkeys ) > <NUM_LIT:1> <EOL> def __reduce__ ( self ) : <EOL> return _SerializableColumnGetter , ( self . colkeys , ) <EOL> def __call__ ( self , value ) : <EOL> state = base . instance_state ( value ) <EOL> m = base . _state_mapper ( state ) <EOL> key = [ m . _get_state_attr_by_column ( <EOL> state , state . dict , <EOL> m . mapped_table . columns [ k ] ) <EOL> for k in self . colkeys ] <EOL> if self . composite : <EOL> return tuple ( key ) <EOL> else : <EOL> return key [ <NUM_LIT:0> ] <EOL> class _SerializableColumnGetterV2 ( _PlainColumnGetter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , colkeys ) : <EOL> self . colkeys = colkeys <EOL> self . composite = len ( colkeys ) > <NUM_LIT:1> <EOL> def __reduce__ ( self ) : <EOL> return self . __class__ , ( self . colkeys , ) <EOL> @ classmethod <EOL> def _reduce_from_cols ( cls , cols ) : <EOL> def _table_key ( c ) : <EOL> if not isinstance ( c . table , expression . TableClause ) : <EOL> return None <EOL> else : <EOL> return c . table . key <EOL> colkeys = [ ( c . key , _table_key ( c ) ) for c in cols ] <EOL> return _SerializableColumnGetterV2 , ( colkeys , ) <EOL> def _cols ( self , mapper ) : <EOL> cols = [ ] <EOL> metadata = getattr ( mapper . local_table , '<STR_LIT>' , None ) <EOL> for ( ckey , tkey ) in self . colkeys : <EOL> if tkey is None or metadata is None or tkey not in metadata : <EOL> cols . append ( mapper . local_table . c [ ckey ] ) <EOL> else : <EOL> cols . append ( metadata . tables [ tkey ] . c [ ckey ] ) <EOL> return cols <EOL> def column_mapped_collection ( mapping_spec ) : <EOL> """<STR_LIT>""" <EOL> cols = [ expression . _only_column_elements ( q , "<STR_LIT>" ) <EOL> for q in util . to_list ( mapping_spec ) <EOL> ] <EOL> keyfunc = _PlainColumnGetter ( cols ) <EOL> return lambda : MappedCollection ( keyfunc ) <EOL> class _SerializableAttrGetter ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . getter = operator . attrgetter ( name ) <EOL> def __call__ ( self , target ) : <EOL> return self . getter ( target ) <EOL> def __reduce__ ( self ) : <EOL> return _SerializableAttrGetter , ( self . name , ) <EOL> def attribute_mapped_collection ( attr_name ) : <EOL> """<STR_LIT>""" <EOL> getter = _SerializableAttrGetter ( attr_name ) <EOL> return lambda : MappedCollection ( getter ) <EOL> def mapped_collection ( keyfunc ) : <EOL> """<STR_LIT>""" <EOL> return lambda : MappedCollection ( keyfunc ) <EOL> class collection ( object ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def appender ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrument_role = '<STR_LIT>' <EOL> return fn <EOL> @ staticmethod <EOL> def remover ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrument_role = '<STR_LIT>' <EOL> return fn <EOL> @ staticmethod <EOL> def iterator ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrument_role = '<STR_LIT>' <EOL> return fn <EOL> @ staticmethod <EOL> def internally_instrumented ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrumented = True <EOL> return fn <EOL> @ staticmethod <EOL> def linker ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrument_role = '<STR_LIT>' <EOL> return fn <EOL> link = linker <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def converter ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . _sa_instrument_role = '<STR_LIT>' <EOL> return fn <EOL> @ staticmethod <EOL> def adds ( arg ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> fn . _sa_instrument_before = ( '<STR_LIT>' , arg ) <EOL> return fn <EOL> return decorator <EOL> @ staticmethod <EOL> def replaces ( arg ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> fn . _sa_instrument_before = ( '<STR_LIT>' , arg ) <EOL> fn . _sa_instrument_after = '<STR_LIT>' <EOL> return fn <EOL> return decorator <EOL> @ staticmethod <EOL> def removes ( arg ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> fn . _sa_instrument_before = ( '<STR_LIT>' , arg ) <EOL> return fn <EOL> return decorator <EOL> @ staticmethod <EOL> def removes_return ( ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( fn ) : <EOL> fn . _sa_instrument_after = '<STR_LIT>' <EOL> return fn <EOL> return decorator <EOL> collection_adapter = operator . attrgetter ( '<STR_LIT>' ) <EOL> """<STR_LIT>""" <EOL> class CollectionAdapter ( object ) : <EOL> """<STR_LIT>""" <EOL> invalidated = False <EOL> def __init__ ( self , attr , owner_state , data ) : <EOL> self . _key = attr . key <EOL> self . _data = weakref . ref ( data ) <EOL> self . owner_state = owner_state <EOL> self . link_to_self ( data ) <EOL> def _warn_invalidated ( self ) : <EOL> util . warn ( "<STR_LIT>" ) <EOL> @ property <EOL> def data ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _data ( ) <EOL> @ property <EOL> def _referenced_by_owner ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . owner_state . dict [ self . _key ] is self . _data ( ) <EOL> @ util . memoized_property <EOL> def attr ( self ) : <EOL> return self . owner_state . manager [ self . _key ] . impl <EOL> def link_to_self ( self , data ) : <EOL> """<STR_LIT>""" <EOL> data . _sa_adapter = self <EOL> if data . _sa_linker : <EOL> data . _sa_linker ( self ) <EOL> def unlink ( self , data ) : <EOL> """<STR_LIT>""" <EOL> del data . _sa_adapter <EOL> if data . _sa_linker : <EOL> data . _sa_linker ( None ) <EOL> def adapt_like_to_iterable ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> converter = self . _data ( ) . _sa_converter <EOL> if converter is not None : <EOL> return converter ( obj ) <EOL> setting_type = util . duck_type_collection ( obj ) <EOL> receiving_type = util . duck_type_collection ( self . _data ( ) ) <EOL> if obj is None or setting_type != receiving_type : <EOL> given = obj is None and '<STR_LIT:None>' or obj . __class__ . __name__ <EOL> if receiving_type is None : <EOL> wanted = self . _data ( ) . __class__ . __name__ <EOL> else : <EOL> wanted = receiving_type . __name__ <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( <EOL> given , wanted ) ) <EOL> if getattr ( obj , '<STR_LIT>' , None ) is not None : <EOL> return obj . _sa_adapter <EOL> elif setting_type == dict : <EOL> if util . py3k : <EOL> return obj . values ( ) <EOL> else : <EOL> return getattr ( obj , '<STR_LIT>' , obj . values ) ( ) <EOL> else : <EOL> return iter ( obj ) <EOL> def append_with_event ( self , item , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> self . _data ( ) . _sa_appender ( item , _sa_initiator = initiator ) <EOL> def append_without_event ( self , item ) : <EOL> """<STR_LIT>""" <EOL> self . _data ( ) . _sa_appender ( item , _sa_initiator = False ) <EOL> def append_multiple_without_event ( self , items ) : <EOL> """<STR_LIT>""" <EOL> appender = self . _data ( ) . _sa_appender <EOL> for item in items : <EOL> appender ( item , _sa_initiator = False ) <EOL> def remove_with_event ( self , item , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> self . _data ( ) . _sa_remover ( item , _sa_initiator = initiator ) <EOL> def remove_without_event ( self , item ) : <EOL> """<STR_LIT>""" <EOL> self . _data ( ) . _sa_remover ( item , _sa_initiator = False ) <EOL> def clear_with_event ( self , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> remover = self . _data ( ) . _sa_remover <EOL> for item in list ( self ) : <EOL> remover ( item , _sa_initiator = initiator ) <EOL> def clear_without_event ( self ) : <EOL> """<STR_LIT>""" <EOL> remover = self . _data ( ) . _sa_remover <EOL> for item in list ( self ) : <EOL> remover ( item , _sa_initiator = False ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( self . _data ( ) . _sa_iterator ( ) ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( list ( self . _data ( ) . _sa_iterator ( ) ) ) <EOL> def __bool__ ( self ) : <EOL> return True <EOL> __nonzero__ = __bool__ <EOL> def fire_append_event ( self , item , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> if initiator is not False : <EOL> if self . invalidated : <EOL> self . _warn_invalidated ( ) <EOL> return self . attr . fire_append_event ( <EOL> self . owner_state , <EOL> self . owner_state . dict , <EOL> item , initiator ) <EOL> else : <EOL> return item <EOL> def fire_remove_event ( self , item , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> if initiator is not False : <EOL> if self . invalidated : <EOL> self . _warn_invalidated ( ) <EOL> self . attr . fire_remove_event ( <EOL> self . owner_state , <EOL> self . owner_state . dict , <EOL> item , initiator ) <EOL> def fire_pre_remove_event ( self , initiator = None ) : <EOL> """<STR_LIT>""" <EOL> if self . invalidated : <EOL> self . _warn_invalidated ( ) <EOL> self . attr . fire_pre_remove_event ( <EOL> self . owner_state , <EOL> self . owner_state . dict , <EOL> initiator = initiator ) <EOL> def __getstate__ ( self ) : <EOL> return { '<STR_LIT:key>' : self . _key , <EOL> '<STR_LIT>' : self . owner_state , <EOL> '<STR_LIT:data>' : self . data } <EOL> def __setstate__ ( self , d ) : <EOL> self . _key = d [ '<STR_LIT:key>' ] <EOL> self . owner_state = d [ '<STR_LIT>' ] <EOL> self . _data = weakref . ref ( d [ '<STR_LIT:data>' ] ) <EOL> def bulk_replace ( values , existing_adapter , new_adapter ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( values , list ) : <EOL> values = list ( values ) <EOL> idset = util . IdentitySet <EOL> existing_idset = idset ( existing_adapter or ( ) ) <EOL> constants = existing_idset . intersection ( values or ( ) ) <EOL> additions = idset ( values or ( ) ) . difference ( constants ) <EOL> removals = existing_idset . difference ( constants ) <EOL> for member in values or ( ) : <EOL> if member in additions : <EOL> new_adapter . append_with_event ( member ) <EOL> elif member in constants : <EOL> new_adapter . append_without_event ( member ) <EOL> if existing_adapter : <EOL> for member in removals : <EOL> existing_adapter . remove_with_event ( member ) <EOL> def prepare_instrumentation ( factory ) : <EOL> """<STR_LIT>""" <EOL> if factory in __canned_instrumentation : <EOL> factory = __canned_instrumentation [ factory ] <EOL> cls = type ( factory ( ) ) <EOL> if cls in __canned_instrumentation : <EOL> factory = __converting_factory ( cls , factory ) <EOL> cls = factory ( ) <EOL> if __instrumentation_mutex . acquire ( ) : <EOL> try : <EOL> if getattr ( cls , '<STR_LIT>' , None ) != id ( cls ) : <EOL> _instrument_class ( cls ) <EOL> finally : <EOL> __instrumentation_mutex . release ( ) <EOL> return factory <EOL> def __converting_factory ( specimen_cls , original_factory ) : <EOL> """<STR_LIT>""" <EOL> instrumented_cls = __canned_instrumentation [ specimen_cls ] <EOL> def wrapper ( ) : <EOL> collection = original_factory ( ) <EOL> return instrumented_cls ( collection ) <EOL> wrapper . __name__ = "<STR_LIT>" % original_factory . __name__ <EOL> wrapper . __doc__ = original_factory . __doc__ <EOL> return wrapper <EOL> def _instrument_class ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls . __module__ == '<STR_LIT>' : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> roles = { } <EOL> methods = { } <EOL> for supercls in cls . __mro__ : <EOL> for name , method in vars ( supercls ) . items ( ) : <EOL> if not util . callable ( method ) : <EOL> continue <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> role = method . _sa_instrument_role <EOL> assert role in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> roles . setdefault ( role , name ) <EOL> before , after = None , None <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> op , argument = method . _sa_instrument_before <EOL> assert op in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> before = op , argument <EOL> if hasattr ( method , '<STR_LIT>' ) : <EOL> op = method . _sa_instrument_after <EOL> assert op in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> after = op <EOL> if before : <EOL> methods [ name ] = before [ <NUM_LIT:0> ] , before [ <NUM_LIT:1> ] , after <EOL> elif after : <EOL> methods [ name ] = None , None , after <EOL> collection_type = util . duck_type_collection ( cls ) <EOL> if collection_type in __interfaces : <EOL> canned_roles , decorators = __interfaces [ collection_type ] <EOL> for role , name in canned_roles . items ( ) : <EOL> roles . setdefault ( role , name ) <EOL> for method , decorator in decorators . items ( ) : <EOL> fn = getattr ( cls , method , None ) <EOL> if ( fn and method not in methods and <EOL> not hasattr ( fn , '<STR_LIT>' ) ) : <EOL> setattr ( cls , method , decorator ( fn ) ) <EOL> if '<STR_LIT>' not in roles or not hasattr ( cls , roles [ '<STR_LIT>' ] ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> elif ( roles [ '<STR_LIT>' ] not in methods and <EOL> not hasattr ( getattr ( cls , roles [ '<STR_LIT>' ] ) , '<STR_LIT>' ) ) : <EOL> methods [ roles [ '<STR_LIT>' ] ] = ( '<STR_LIT>' , <NUM_LIT:1> , None ) <EOL> if '<STR_LIT>' not in roles or not hasattr ( cls , roles [ '<STR_LIT>' ] ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> elif ( roles [ '<STR_LIT>' ] not in methods and <EOL> not hasattr ( getattr ( cls , roles [ '<STR_LIT>' ] ) , '<STR_LIT>' ) ) : <EOL> methods [ roles [ '<STR_LIT>' ] ] = ( '<STR_LIT>' , <NUM_LIT:1> , None ) <EOL> if '<STR_LIT>' not in roles or not hasattr ( cls , roles [ '<STR_LIT>' ] ) : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> for method_name , ( before , argument , after ) in methods . items ( ) : <EOL> setattr ( cls , method_name , <EOL> _instrument_membership_mutator ( getattr ( cls , method_name ) , <EOL> before , argument , after ) ) <EOL> for role , method_name in roles . items ( ) : <EOL> setattr ( cls , '<STR_LIT>' % role , getattr ( cls , method_name ) ) <EOL> cls . _sa_adapter = None <EOL> if not hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . _sa_linker = None <EOL> if not hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . _sa_converter = None <EOL> cls . _sa_instrumented = id ( cls ) <EOL> def _instrument_membership_mutator ( method , before , argument , after ) : <EOL> """<STR_LIT>""" <EOL> if before : <EOL> fn_args = list ( util . flatten_iterator ( inspect . getargspec ( method ) [ <NUM_LIT:0> ] ) ) <EOL> if isinstance ( argument , int ) : <EOL> pos_arg = argument <EOL> named_arg = len ( fn_args ) > argument and fn_args [ argument ] or None <EOL> else : <EOL> if argument in fn_args : <EOL> pos_arg = fn_args . index ( argument ) <EOL> else : <EOL> pos_arg = None <EOL> named_arg = argument <EOL> del fn_args <EOL> def wrapper ( * args , ** kw ) : <EOL> if before : <EOL> if pos_arg is None : <EOL> if named_arg not in kw : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" % argument ) <EOL> value = kw [ named_arg ] <EOL> else : <EOL> if len ( args ) > pos_arg : <EOL> value = args [ pos_arg ] <EOL> elif named_arg in kw : <EOL> value = kw [ named_arg ] <EOL> else : <EOL> raise sa_exc . ArgumentError ( <EOL> "<STR_LIT>" % argument ) <EOL> initiator = kw . pop ( '<STR_LIT>' , None ) <EOL> if initiator is False : <EOL> executor = None <EOL> else : <EOL> executor = args [ <NUM_LIT:0> ] . _sa_adapter <EOL> if before and executor : <EOL> getattr ( executor , before ) ( value , initiator ) <EOL> if not after or not executor : <EOL> return method ( * args , ** kw ) <EOL> else : <EOL> res = method ( * args , ** kw ) <EOL> if res is not None : <EOL> getattr ( executor , after ) ( res , initiator ) <EOL> return res <EOL> wrapper . _sa_instrumented = True <EOL> if hasattr ( method , "<STR_LIT>" ) : <EOL> wrapper . _sa_instrument_role = method . _sa_instrument_role <EOL> wrapper . __name__ = method . __name__ <EOL> wrapper . __doc__ = method . __doc__ <EOL> return wrapper <EOL> def __set ( collection , item , _sa_initiator = None ) : <EOL> """<STR_LIT>""" <EOL> if _sa_initiator is not False : <EOL> executor = collection . _sa_adapter <EOL> if executor : <EOL> item = executor . fire_append_event ( item , _sa_initiator ) <EOL> return item <EOL> def __del ( collection , item , _sa_initiator = None ) : <EOL> """<STR_LIT>""" <EOL> if _sa_initiator is not False : <EOL> executor = collection . _sa_adapter <EOL> if executor : <EOL> executor . fire_remove_event ( item , _sa_initiator ) <EOL> def __before_delete ( collection , _sa_initiator = None ) : <EOL> """<STR_LIT>""" <EOL> executor = collection . _sa_adapter <EOL> if executor : <EOL> executor . fire_pre_remove_event ( _sa_initiator ) <EOL> def _list_decorators ( ) : <EOL> """<STR_LIT>""" <EOL> def _tidy ( fn ) : <EOL> fn . _sa_instrumented = True <EOL> fn . __doc__ = getattr ( list , fn . __name__ ) . __doc__ <EOL> def append ( fn ) : <EOL> def append ( self , item , _sa_initiator = None ) : <EOL> item = __set ( self , item , _sa_initiator ) <EOL> fn ( self , item ) <EOL> _tidy ( append ) <EOL> return append <EOL> def remove ( fn ) : <EOL> def remove ( self , value , _sa_initiator = None ) : <EOL> __before_delete ( self , _sa_initiator ) <EOL> fn ( self , value ) <EOL> __del ( self , value , _sa_initiator ) <EOL> _tidy ( remove ) <EOL> return remove <EOL> def insert ( fn ) : <EOL> def insert ( self , index , value ) : <EOL> value = __set ( self , value ) <EOL> fn ( self , index , value ) <EOL> _tidy ( insert ) <EOL> return insert <EOL> def __setitem__ ( fn ) : <EOL> def __setitem__ ( self , index , value ) : <EOL> if not isinstance ( index , slice ) : <EOL> existing = self [ index ] <EOL> if existing is not None : <EOL> __del ( self , existing ) <EOL> value = __set ( self , value ) <EOL> fn ( self , index , value ) <EOL> else : <EOL> step = index . step or <NUM_LIT:1> <EOL> start = index . start or <NUM_LIT:0> <EOL> if start < <NUM_LIT:0> : <EOL> start += len ( self ) <EOL> if index . stop is not None : <EOL> stop = index . stop <EOL> else : <EOL> stop = len ( self ) <EOL> if stop < <NUM_LIT:0> : <EOL> stop += len ( self ) <EOL> if step == <NUM_LIT:1> : <EOL> for i in range ( start , stop , step ) : <EOL> if len ( self ) > start : <EOL> del self [ start ] <EOL> for i , item in enumerate ( value ) : <EOL> self . insert ( i + start , item ) <EOL> else : <EOL> rng = list ( range ( start , stop , step ) ) <EOL> if len ( value ) != len ( rng ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( len ( value ) , <EOL> len ( rng ) ) ) <EOL> for i , item in zip ( rng , value ) : <EOL> self . __setitem__ ( i , item ) <EOL> _tidy ( __setitem__ ) <EOL> return __setitem__ <EOL> def __delitem__ ( fn ) : <EOL> def __delitem__ ( self , index ) : <EOL> if not isinstance ( index , slice ) : <EOL> item = self [ index ] <EOL> __del ( self , item ) <EOL> fn ( self , index ) <EOL> else : <EOL> for item in self [ index ] : <EOL> __del ( self , item ) <EOL> fn ( self , index ) <EOL> _tidy ( __delitem__ ) <EOL> return __delitem__ <EOL> if util . py2k : <EOL> def __setslice__ ( fn ) : <EOL> def __setslice__ ( self , start , end , values ) : <EOL> for value in self [ start : end ] : <EOL> __del ( self , value ) <EOL> values = [ __set ( self , value ) for value in values ] <EOL> fn ( self , start , end , values ) <EOL> _tidy ( __setslice__ ) <EOL> return __setslice__ <EOL> def __delslice__ ( fn ) : <EOL> def __delslice__ ( self , start , end ) : <EOL> for value in self [ start : end ] : <EOL> __del ( self , value ) <EOL> fn ( self , start , end ) <EOL> _tidy ( __delslice__ ) <EOL> return __delslice__ <EOL> def extend ( fn ) : <EOL> def extend ( self , iterable ) : <EOL> for value in iterable : <EOL> self . append ( value ) <EOL> _tidy ( extend ) <EOL> return extend <EOL> def __iadd__ ( fn ) : <EOL> def __iadd__ ( self , iterable ) : <EOL> for value in iterable : <EOL> self . append ( value ) <EOL> return self <EOL> _tidy ( __iadd__ ) <EOL> return __iadd__ <EOL> def pop ( fn ) : <EOL> def pop ( self , index = - <NUM_LIT:1> ) : <EOL> __before_delete ( self ) <EOL> item = fn ( self , index ) <EOL> __del ( self , item ) <EOL> return item <EOL> _tidy ( pop ) <EOL> return pop <EOL> if not util . py2k : <EOL> def clear ( fn ) : <EOL> def clear ( self , index = - <NUM_LIT:1> ) : <EOL> for item in self : <EOL> __del ( self , item ) <EOL> fn ( self ) <EOL> _tidy ( clear ) <EOL> return clear <EOL> l = locals ( ) . copy ( ) <EOL> l . pop ( '<STR_LIT>' ) <EOL> return l <EOL> def _dict_decorators ( ) : <EOL> """<STR_LIT>""" <EOL> def _tidy ( fn ) : <EOL> fn . _sa_instrumented = True <EOL> fn . __doc__ = getattr ( dict , fn . __name__ ) . __doc__ <EOL> Unspecified = util . symbol ( '<STR_LIT>' ) <EOL> def __setitem__ ( fn ) : <EOL> def __setitem__ ( self , key , value , _sa_initiator = None ) : <EOL> if key in self : <EOL> __del ( self , self [ key ] , _sa_initiator ) <EOL> value = __set ( self , value , _sa_initiator ) <EOL> fn ( self , key , value ) <EOL> _tidy ( __setitem__ ) <EOL> return __setitem__ <EOL> def __delitem__ ( fn ) : <EOL> def __delitem__ ( self , key , _sa_initiator = None ) : <EOL> if key in self : <EOL> __del ( self , self [ key ] , _sa_initiator ) <EOL> fn ( self , key ) <EOL> _tidy ( __delitem__ ) <EOL> return __delitem__ <EOL> def clear ( fn ) : <EOL> def clear ( self ) : <EOL> for key in self : <EOL> __del ( self , self [ key ] ) <EOL> fn ( self ) <EOL> _tidy ( clear ) <EOL> return clear <EOL> def pop ( fn ) : <EOL> def pop ( self , key , default = Unspecified ) : <EOL> if key in self : <EOL> __del ( self , self [ key ] ) <EOL> if default is Unspecified : <EOL> return fn ( self , key ) <EOL> else : <EOL> return fn ( self , key , default ) <EOL> _tidy ( pop ) <EOL> return pop <EOL> def popitem ( fn ) : <EOL> def popitem ( self ) : <EOL> __before_delete ( self ) <EOL> item = fn ( self ) <EOL> __del ( self , item [ <NUM_LIT:1> ] ) <EOL> return item <EOL> _tidy ( popitem ) <EOL> return popitem <EOL> def setdefault ( fn ) : <EOL> def setdefault ( self , key , default = None ) : <EOL> if key not in self : <EOL> self . __setitem__ ( key , default ) <EOL> return default <EOL> else : <EOL> return self . __getitem__ ( key ) <EOL> _tidy ( setdefault ) <EOL> return setdefault <EOL> def update ( fn ) : <EOL> def update ( self , __other = Unspecified , ** kw ) : <EOL> if __other is not Unspecified : <EOL> if hasattr ( __other , '<STR_LIT>' ) : <EOL> for key in list ( __other ) : <EOL> if ( key not in self or <EOL> self [ key ] is not __other [ key ] ) : <EOL> self [ key ] = __other [ key ] <EOL> else : <EOL> for key , value in __other : <EOL> if key not in self or self [ key ] is not value : <EOL> self [ key ] = value <EOL> for key in kw : <EOL> if key not in self or self [ key ] is not kw [ key ] : <EOL> self [ key ] = kw [ key ] <EOL> _tidy ( update ) <EOL> return update <EOL> l = locals ( ) . copy ( ) <EOL> l . pop ( '<STR_LIT>' ) <EOL> l . pop ( '<STR_LIT>' ) <EOL> return l <EOL> _set_binop_bases = ( set , frozenset ) <EOL> def _set_binops_check_strict ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( obj , _set_binop_bases + ( self . __class__ , ) ) <EOL> def _set_binops_check_loose ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( obj , _set_binop_bases + ( self . __class__ , ) ) or <EOL> util . duck_type_collection ( obj ) == set ) <EOL> def _set_decorators ( ) : <EOL> """<STR_LIT>""" <EOL> def _tidy ( fn ) : <EOL> fn . _sa_instrumented = True <EOL> fn . __doc__ = getattr ( set , fn . __name__ ) . __doc__ <EOL> Unspecified = util . symbol ( '<STR_LIT>' ) <EOL> def add ( fn ) : <EOL> def add ( self , value , _sa_initiator = None ) : <EOL> if value not in self : <EOL> value = __set ( self , value , _sa_initiator ) <EOL> fn ( self , value ) <EOL> _tidy ( add ) <EOL> return add <EOL> def discard ( fn ) : <EOL> def discard ( self , value , _sa_initiator = None ) : <EOL> if value in self : <EOL> __del ( self , value , _sa_initiator ) <EOL> fn ( self , value ) <EOL> _tidy ( discard ) <EOL> return discard <EOL> def remove ( fn ) : <EOL> def remove ( self , value , _sa_initiator = None ) : <EOL> if value in self : <EOL> __del ( self , value , _sa_initiator ) <EOL> fn ( self , value ) <EOL> _tidy ( remove ) <EOL> return remove <EOL> def pop ( fn ) : <EOL> def pop ( self ) : <EOL> __before_delete ( self ) <EOL> item = fn ( self ) <EOL> __del ( self , item ) <EOL> return item <EOL> _tidy ( pop ) <EOL> return pop <EOL> def clear ( fn ) : <EOL> def clear ( self ) : <EOL> for item in list ( self ) : <EOL> self . remove ( item ) <EOL> _tidy ( clear ) <EOL> return clear <EOL> def update ( fn ) : <EOL> def update ( self , value ) : <EOL> for item in value : <EOL> self . add ( item ) <EOL> _tidy ( update ) <EOL> return update <EOL> def __ior__ ( fn ) : <EOL> def __ior__ ( self , value ) : <EOL> if not _set_binops_check_strict ( self , value ) : <EOL> return NotImplemented <EOL> for item in value : <EOL> self . add ( item ) <EOL> return self <EOL> _tidy ( __ior__ ) <EOL> return __ior__ <EOL> def difference_update ( fn ) : <EOL> def difference_update ( self , value ) : <EOL> for item in value : <EOL> self . discard ( item ) <EOL> _tidy ( difference_update ) <EOL> return difference_update <EOL> def __isub__ ( fn ) : <EOL> def __isub__ ( self , value ) : <EOL> if not _set_binops_check_strict ( self , value ) : <EOL> return NotImplemented <EOL> for item in value : <EOL> self . discard ( item ) <EOL> return self <EOL> _tidy ( __isub__ ) <EOL> return __isub__ <EOL> def intersection_update ( fn ) : <EOL> def intersection_update ( self , other ) : <EOL> want , have = self . intersection ( other ) , set ( self ) <EOL> remove , add = have - want , want - have <EOL> for item in remove : <EOL> self . remove ( item ) <EOL> for item in add : <EOL> self . add ( item ) <EOL> _tidy ( intersection_update ) <EOL> return intersection_update <EOL> def __iand__ ( fn ) : <EOL> def __iand__ ( self , other ) : <EOL> if not _set_binops_check_strict ( self , other ) : <EOL> return NotImplemented <EOL> want , have = self . intersection ( other ) , set ( self ) <EOL> remove , add = have - want , want - have <EOL> for item in remove : <EOL> self . remove ( item ) <EOL> for item in add : <EOL> self . add ( item ) <EOL> return self <EOL> _tidy ( __iand__ ) <EOL> return __iand__ <EOL> def symmetric_difference_update ( fn ) : <EOL> def symmetric_difference_update ( self , other ) : <EOL> want , have = self . symmetric_difference ( other ) , set ( self ) <EOL> remove , add = have - want , want - have <EOL> for item in remove : <EOL> self . remove ( item ) <EOL> for item in add : <EOL> self . add ( item ) <EOL> _tidy ( symmetric_difference_update ) <EOL> return symmetric_difference_update <EOL> def __ixor__ ( fn ) : <EOL> def __ixor__ ( self , other ) : <EOL> if not _set_binops_check_strict ( self , other ) : <EOL> return NotImplemented <EOL> want , have = self . symmetric_difference ( other ) , set ( self ) <EOL> remove , add = have - want , want - have <EOL> for item in remove : <EOL> self . remove ( item ) <EOL> for item in add : <EOL> self . add ( item ) <EOL> return self <EOL> _tidy ( __ixor__ ) <EOL> return __ixor__ <EOL> l = locals ( ) . copy ( ) <EOL> l . pop ( '<STR_LIT>' ) <EOL> l . pop ( '<STR_LIT>' ) <EOL> return l <EOL> class InstrumentedList ( list ) : <EOL> """<STR_LIT>""" <EOL> class InstrumentedSet ( set ) : <EOL> """<STR_LIT>""" <EOL> class InstrumentedDict ( dict ) : <EOL> """<STR_LIT>""" <EOL> __canned_instrumentation = { <EOL> list : InstrumentedList , <EOL> set : InstrumentedSet , <EOL> dict : InstrumentedDict , <EOL> } <EOL> __interfaces = { <EOL> list : ( <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , _list_decorators ( ) <EOL> ) , <EOL> set : ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } , _set_decorators ( ) <EOL> ) , <EOL> dict : ( { '<STR_LIT>' : '<STR_LIT>' } , _dict_decorators ( ) ) if util . py3k <EOL> else ( { '<STR_LIT>' : '<STR_LIT>' } , _dict_decorators ( ) ) , <EOL> } <EOL> class MappedCollection ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keyfunc ) : <EOL> """<STR_LIT>""" <EOL> self . keyfunc = keyfunc <EOL> @ collection . appender <EOL> @ collection . internally_instrumented <EOL> def set ( self , value , _sa_initiator = None ) : <EOL> """<STR_LIT>""" <EOL> key = self . keyfunc ( value ) <EOL> self . __setitem__ ( key , value , _sa_initiator ) <EOL> @ collection . remover <EOL> @ collection . internally_instrumented <EOL> def remove ( self , value , _sa_initiator = None ) : <EOL> """<STR_LIT>""" <EOL> key = self . keyfunc ( value ) <EOL> if self [ key ] != value : <EOL> raise sa_exc . InvalidRequestError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( value , self [ key ] , key ) ) <EOL> self . __delitem__ ( key , _sa_initiator ) <EOL> @ collection . converter <EOL> def _convert ( self , dictlike ) : <EOL> """<STR_LIT>""" <EOL> for incoming_key , value in util . dictlike_iteritems ( dictlike ) : <EOL> new_key = self . keyfunc ( value ) <EOL> if incoming_key != new_key : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> incoming_key , value , new_key ) ) <EOL> yield value <EOL> _instrument_class ( MappedCollection ) <EOL> _instrument_class ( InstrumentedList ) <EOL> _instrument_class ( InstrumentedSet ) </s>
