On Nov 24, 2013, at 5:33 PM, Joseph Casale <[email protected]> wrote:

> I am trying to write a convenience wrapper something like:
> 
>     def wrapper(self, arg_a=None, arg_a=None):
> 
>         query = self.session.query(Table_A).\
>             filter(Table_A.col_a == arg_a, Table_A.col_b == arg_b).\
>             all()
> 
>         ...
> 
> The actual query is much longer, none the less to handle the case where
> the optional input is left out, the query needs different logic. Without eval
> and string formatting, how does one safely accomplish this?

conditonals?

def wrapper(self, arg_a=None, arg_b=None):

    query = self.session.query(Table_A)
    if arg_a is not None:
        query = query.filter(Table_A.col_a == arg_a)
    if arg_b is not None:
        query = query.filter(Table_A.col_b == arg_b)
    return query.all()


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

Reply via email to