oh , the "or" won't work as a hybrid, you either need to use the binary op | or 
otherwise run through some filter that will give you expression vs. local 
behavior.





On Aug 26, 2013, at 2:15 PM, lars van gemerden <[email protected]> wrote:

> 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
> 
> 
> 
> 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))
> 
>  
> 
> -- 
> 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.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to