On Feb 23, 2013, at 1:13 AM, Eric Rasmussen <ericrasmus...@gmail.com> wrote:
> But that's just a long-winded way to express a reduce operation*, so for your > example you could also write: > > import sqlalchemy as sa > > criteria = (('male', 35), ('female', 35)) > Useraccount = model.Useraccount > query = session.query(Useraccount) > ands = [sa.and_(Useraccount.gender == gender, Useraccount.age == age) for > gender, age in criteria] > or_clauses = reduce(sa.or_, ands) > query = query.filter(or_clauses) > this is fine but I'd make one offhand note that nesting the conjunctions, i.e. or_(or_(or_(or_(x, y), z), q), b) has the effect of the compiler traversing it in a deep recursion loop, if you're using a lot of values (like, hundreds). We had a user getting recursion overflows due to this. I illustrated a compiler plug in for this user that did a non-recursive "unwrap" of the nested structure first before passing it down to the default compiler, which isn't built in because it adds a good chunk of performance overhead to all conjunctions. Ultimately he went with doing or_(*everything) instead of his original approach of "x |= (y), x |= (q)" etc. -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.