Hmm, too bad. I do have the lambda methods in string form so i'll probably have to write a parser and construct the query with unions and intersects (or filter(A.b > 1, A.c<0) like calls instead of intersects).
No how did writing a parser tree go |-)? CL On Monday, August 26, 2013 8:27:16 PM UTC+2, Simon King wrote: > > > On 26 Aug 2013, at 19:15, lars van gemerden > <[email protected]<javascript:>> > wrote: > > > > On Monday, August 26, 2013 5:23:07 PM UTC+2, Michael Bayer wrote: > > > > On Aug 26, 2013, at 11:14 AM, lars van gemerden <[email protected]> > wrote: > > > >> Hi all, > >> > >> This might be a bit of a stretch but here it goes: > >> > >> Say that i have a lambda function that takes a mapped object and > teturns whether it is valid; e.g: > >> > >> lambda person: person.age > 17 or person.length > 1.75 > >> > >> is it possible to use this method to perform an sqla query on the > database? Perhaps by using hybrid_method? > >> > >> Cheers, Lars > >> > >> PS: this might seem strange question, but it would save me a lot of > work and/or would make my internal and user interfaces a lot more > consistent. > >> > >> PPS: i do also have a string representation of the lambda functions, if > that helps > > > > > > I'm not sure if you need these to remain as lambdas, sure you can just > pass it to a hybrid_method: > > > > class MyClass(Base): > > # .... > > > > @hybrid_method > > def run_compare(self, fn, *args): > > return fn(*args) > > > > > > query(MyClass).filter(MyClass.run_compare(some_lambda, > some_other_object)) > > > > > > > > Hi Michael, > > > > So just to be sure, if i understand correctly and i have: > > > > func = lambda person: person.age > 17 or person.length > 1.75 > > > > I can do: > > > > class Person(Base): > > # .... > > > > @hybrid_method > > def run_filter(self, fn): > > return fn(self) > > > > session.query(MyClass).filter(MyClass.run_compare(func)) > > > > to get all objects of class Person where obj.age>17 or obj.length>1.75 > > > > Is that without loading all records and filtering afterwards?? > > > > That's pretty impressive i think (and really helpful to me personally) > > > > Cheers, Lars > > > As long as your function returns something meaningful when called with the > class (Person) rather than an instance, it should be fine. > > Unfortunately I'm not sure your example will work, because "<expr> or > <expr>" isn't something that SQLAlchemy can convert into an SQL statement. > If you were able to use the bitwise "or" operator "|" instead, that would > do the right thing. See the "contains" and "intersects" methods in the > first example on > http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/hybrid.html. > > Hope that helps, > > Simon -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
