On 9/10/15 8:48 AM, Pavel S wrote:
Let's say, I have declarative classes A, B, C, D.

A is the parent
B has FK&relationship to A
C has FK&relationship to B,
D has FK&relationship to C etc...

I'd like to implement _generic method_ walk(obj) which will recursively yield dependent/related objects of obj (which is instance of A).

I know that there is introspection interface inspect(), however I'm don't really understand how to use it properly in my use case.

Shall I do inspect(obj) or rather inspect(obj.__class__) and then somehow apply inspection to obj?

Are there an examples and best practices?
right now you can kind of get this effect using cascade_iterator: http://docs.sqlalchemy.org/en/rel_1_0/orm/mapping_api.html?highlight=cascade_iterator#sqlalchemy.orm.mapper.Mapper.cascade_iterator

the limitation is that right now its based on relationship cascade settings, as that's what it was intended for, so you'd probably want to use "save-update":

insp = inspect(my_object)
for obj in insp.mapper.cascade_iterator("save-update", insp):
   # ...

to implement your own system, the graph of objects is strictly based on relationship. so walk() is pretty simple:

def walk(obj):
    yield obj
    insp = inspect(obj)
    for relationship in insp.mapper.relationships:
        related = getattr(obj, relationship.key)
        if relationship.uselist:
            for collection_member in related:
                for walk_related in walk(collection_member):
                    yield walk_related
        elif related is not None:
            for walk_related in walk(related):
                yield walk_related






--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to