<s> """<STR_LIT>""" <EOL> from sqlalchemy import String , Integer , Column , create_engine , ForeignKey <EOL> from sqlalchemy . orm import relationship , Session <EOL> from sqlalchemy . orm . collections import MappedCollection <EOL> from sqlalchemy . ext . declarative import declarative_base <EOL> from sqlalchemy . ext . associationproxy import association_proxy <EOL> import operator <EOL> class Base ( object ) : <EOL> id = Column ( Integer , primary_key = True ) <EOL> Base = declarative_base ( cls = Base ) <EOL> class GenDefaultCollection ( MappedCollection ) : <EOL> def __missing__ ( self , key ) : <EOL> self [ key ] = b = B ( key ) <EOL> return b <EOL> class A ( Base ) : <EOL> __tablename__ = "<STR_LIT:a>" <EOL> associations = relationship ( "<STR_LIT:B>" , <EOL> collection_class = lambda : GenDefaultCollection ( operator . attrgetter ( "<STR_LIT:key>" ) ) <EOL> ) <EOL> collections = association_proxy ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> """<STR_LIT>""" <EOL> class B ( Base ) : <EOL> __tablename__ = "<STR_LIT:b>" <EOL> a_id = Column ( Integer , ForeignKey ( "<STR_LIT>" ) , nullable = False ) <EOL> elements = relationship ( "<STR_LIT:C>" , collection_class = set ) <EOL> key = Column ( String ) <EOL> values = association_proxy ( "<STR_LIT>" , "<STR_LIT:value>" ) <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , values = None ) : <EOL> self . key = key <EOL> if values : <EOL> self . values = values <EOL> class C ( Base ) : <EOL> __tablename__ = "<STR_LIT:c>" <EOL> b_id = Column ( Integer , ForeignKey ( "<STR_LIT>" ) , nullable = False ) <EOL> value = Column ( Integer ) <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> engine = create_engine ( '<STR_LIT>' , echo = True ) <EOL> Base . metadata . create_all ( engine ) <EOL> session = Session ( engine ) <EOL> session . add_all ( [ <EOL> A ( collections = { <EOL> "<STR_LIT:1>" : set ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) , <EOL> } ) <EOL> ] ) <EOL> session . commit ( ) <EOL> a1 = session . query ( A ) . first ( ) <EOL> print ( a1 . collections [ "<STR_LIT:1>" ] ) <EOL> a1 . collections [ "<STR_LIT:1>" ] . add ( <NUM_LIT:4> ) <EOL> session . commit ( ) <EOL> a1 . collections [ "<STR_LIT:2>" ] . update ( [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> ] ) <EOL> session . commit ( ) <EOL> print ( a1 . collections [ "<STR_LIT:2>" ] ) </s>
