On Jan 17, 2010, at 9:20 AM, Adrian wrote:
> Hi,
>
> is there an easy way to apply a function to the items returned by
> association_proxy? Currently, I have a setup like this: A->B->C, both
> one-to-many relations; A.C (association_proxy('B','C')) returns a list
> of lists but I would like to apply a function (list(chain.from_iterable
> (x))) that flattens it to a simple list. What what be the best way to
> implement this?
you should be able to pass proxy_factory to your association_proxy:
foo = association_proxy("B", "C", proxy_factory=lambda coll, creator, value:
list(chain.from_iterable(coll)))
that returned list though won't interact in the other direction, i.e. when you
append or remove items from it no events will propagate up to the C or B since
above we're not adding handlers for that (I'm assuming this isn't needed since
you can't determine that from a flattened list anyway).
In that case association_proxy here wouldn't even be needed, a read-only
version is just:
class A(object):
@property
def foo(self):
reutrn list(chain.from_iterable(b.C for b in self.B))
>
> Cheers,
>
> Adrian
> --
> 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.
>
>
--
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.