> class MyObject(object):
>     myObjects = association_proxy('myPropertyProxy', 'myProperty',
> creator=createAssociationObject)

u mean, this one line there is 'burden' ? put all that in separate 
function in another file, and just import and call it...
how about all the other table, mapper, etc stuff?
if u really need less verbosity, use syntax sugars, e.g. declarative, 
elixir, dbcook. in general, it may or may not make your life easier.

On Friday 19 December 2008 21:11:07 Arn Vollebregt wrote:
> Hi,
>
> I am wondering if there is a way to completely decouple the
> association proxy declaration from my Python classes, and contain
> this to a database class? My goal is not to 'burden' developers
> looking at the main part of my code with SQLAlchemy when there is
> no need to directly interact with the database. This in order to
> heighten the readability/extendability of my code. All the examples
> I have seen thus far declare the association proxy directly within
> the class definition, which I would like to avoid.
>
> Below I have attached a piece of generalized code to which I would
> like to apply this principle (let's hope the indentation survives
> the email...). In reality the class declarations would be located
> within a different file and imported in the database class to
> decouple these sections of the code. In case your wondering: the
> goal of the code is a self-referential n-n relation where the
> association class has an extra attribute which needs to be
> accessible from inside the program. Most code has been borrowed
> from the broker/stocks example[1].
>
> Any ideas? Or is this simply not possible at this time? This is my
> first encounter with SQLAlchemy, and perhaps I have missed the
> obvious somehow...
>
> Regards,
>
> Arn Vollebregt
>
> [1]
> http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.ht
>ml#building-complex-views
>
> <test.py>
> from sqlalchemy import create_engine, MetaData, Table, Column,
> Integer, String, ForeignKey
> from sqlalchemy.orm import sessionmaker, mapper, relation
> from sqlalchemy.ext.associationproxy import association_proxy
> from sqlalchemy.orm.collections import attribute_mapped_collection
>
> engine = create_engine('sqlite:///test.db', echo=True)
> metadata = MetaData()
> Session = sessionmaker(bind=engine)
> session = Session()
>
> def createAssociationObject(otherMyObject, myProperty):
>     return AssociationObject(otherMyObject=otherMyObject,
> myProperty=myProperty)
>
> class MyObject(object):
>     myObjects = association_proxy('myPropertyProxy', 'myProperty',
> creator=createAssociationObject)
>
>     def __init__(self, name):
>         self.name = name
>
>     def __str__(self):
>         # potential infinit recursion
>         #return "<myObject(name=%s, myObjects=%s)>" % (self.name,
> self.myObjects)
>         return "<myObject(name=%s, #myObjects=%i)>" % (self.name,
> len (self.myObjects))
>
>     def __repr__(self):
>         return self.__str__()
>
> class AssociationObject(object):
>     def __init__(self, myObject=None, otherMyObject=None,
> myProperty=0):
>         self.myObject = myObject
>         self.otherMyObject = otherMyObject
>         self.myProperty = myProperty
>
> myObjectsTable = Table("myObjects", metadata,
>    Column('id', Integer, primary_key=True),
>    Column('name', String(25), nullable=False),
> )
>
> AssociationObjectsTable = Table("associationObjects", metadata,
>   Column('myObjectID', Integer, ForeignKey('myObjects.id'),
> primary_key=True),
>   Column('otherMyObjectID', Integer, ForeignKey('myObjects.id'),
> primary_key=True),
>   Column('myProperty', Integer),
> )
>
> mapper(MyObject, myObjectsTable, properties={
>     'myPropertyProxy': relation(AssociationObject,
>         collection_class=attribute_mapped_collection('myProperty'),
>
> primaryjoin=AssociationObjectsTable.c.myObjectID==myObjectsTable.c.
>id,) })
>
> mapper(AssociationObject, AssociationObjectsTable, properties={
>     'myObject': relation(MyObject,
>
> primaryjoin=AssociationObjectsTable.c.myObjectID==myObjectsTable.c.
>id,), 'otherMyObject': relation(MyObject,
>
> primaryjoin=AssociationObjectsTable.c.otherMyObjectID==myObjectsTab
>le.c.id,) })
>
> metadata.create_all(engine)
>
> myObject1 = MyObject('testObject1')
> myObject2 = MyObject('testObject2')
> myObject3 = MyObject('testObject3')
>
> myObject1.myObjects[myObject2] = 1
> myObject2.myObjects[myObject1] = 1
> myObject3.myObjects[myObject2] = 1
> myObject3.myObjects[myObject1] = 2
>
> session.add(myObject1)
> session.add(myObject2)
> session.add(myObject3)
> session.commit()
>
> for myObject in session.query(MyObject).order_by(MyObject.id):
>     print myObject
>     for otherMyObject, myProperty in myObject.myObjects.items():
>         print "\t" + str(otherMyObject), str(myProperty)
> </test.py>
>
> 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to